diff --git a/src/hocon_pp.erl b/src/hocon_pp.erl index 3f0cd5b..858182e 100644 --- a/src/hocon_pp.erl +++ b/src/hocon_pp.erl @@ -332,6 +332,10 @@ maybe_quote_key(K0) -> K0 end. +is_quote_key("null") -> + %% `null' is a primitive value in hocon, so if a `"null"' string is used as a map key, + %% it must be quoted to avoid confusion with the `null' primitive. + true; is_quote_key(K) -> case io_lib:printable_latin1_list(K) of true -> diff --git a/test/hocon_pp_tests.erl b/test/hocon_pp_tests.erl index 58f643e..8e66324 100644 --- a/test/hocon_pp_tests.erl +++ b/test/hocon_pp_tests.erl @@ -358,3 +358,21 @@ crlf_multiline_test_() -> ?_assertEqual({ok, Value}, hocon:binary(Expected)), ?_assertEqual({ok, Value}, hocon:binary(Variant)) ]. + +%% Checks that if the string `"null"' is used as a map key, it's correctly serialized and +%% then deserialized. +null_map_key_test_() -> + Roundtrip = fun(NVal) -> + Value = #{<<"root">> => #{NVal => 1}}, + IOList = hocon_pp:do(Value, #{}), + ?assertEqual({ok, Value}, hocon:binary(IOList)) + end, + [ + ?_test(Roundtrip(Value)) + || Value <- [ + <<"null">>, + <<"Null">>, + <<"NULL">>, + <<"nUlL">> + ] + ].