Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#20): escape only the characters that need to be escaped #22

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(#20): escape only the characters that need to be escaped
closes #20
yagogarea committed Aug 9, 2024
commit 00ed8cdeb6752bc489fc315e9712cf2c14319f66
4 changes: 0 additions & 4 deletions src/njson_decoder.erl
Original file line number Diff line number Diff line change
@@ -310,10 +310,6 @@ empty_object(<<Bin/binary>>, Original, Skip, [?KEY, Map | Next]) ->
OK :: {ok, Json},
Json :: njson:t(),
Error :: njson:decode_error().
number(<<$+, Bin/binary>>, Original, Skip, Next, Len) ->
number(Bin, Original, Skip, Next, Len + 1);
number(<<$-, Bin/binary>>, Original, Skip, Next, Len) ->
number(Bin, Original, Skip, Next, Len + 1);
number(<<$., Bin/binary>>, Original, Skip, Next, Len) ->
fraction(Bin, Original, Skip, Next, Len + 1);
number(<<D, Bin/binary>>, Original, Skip, Next, Len) when D >= $0, D =< $9 ->
5 changes: 4 additions & 1 deletion src/njson_encoder.erl
Original file line number Diff line number Diff line change
@@ -217,7 +217,10 @@ escape_char($\f) ->
<<"\\f">>;
escape_char($\r) ->
<<"\\r">>;
escape_char(C) when C >= 16#0E andalso C =< 16#1F ->
escape_char(C) when C =:= 16#0E orelse C =:= 16#0F ->
Bin = integer_to_binary(C, 16),
<<"\\u000", Bin/binary>>;
escape_char(C) when C >= 16#10 andalso C =< 16#1F ->
Bin = integer_to_binary(C, 16),
<<"\\u00", Bin/binary>>;
escape_char($") ->
23 changes: 15 additions & 8 deletions test/njson_SUITE.erl
Original file line number Diff line number Diff line change
@@ -119,7 +119,8 @@ json_encode(_Conf) ->
end,
lists:foreach(Test2, json_encode_only_cases() ++ json_cases()),
{ok, _Json} = njson:encode(#{<<"key2">> => <<"Val2">>}, true),
{ok, _Json2} = njson:encode([<<"val1">>, <<"val2">>], true).
{ok, _Json2} = njson:encode([<<"val1">>, <<"val2">>], true),
{ok, _Json3} = njson:encode(<<"val1">>, true).

json_decode_only_cases() ->
[
@@ -128,23 +129,23 @@ json_decode_only_cases() ->
{<<"+1.0E+3">>, 1.0e3},
{<<"+1E+3">>, 1.0e3},
{<<"false", $\s, $\t, $\r, $\n>>, false},
{<<"\"\\\\, \\b, \\f, \\r, \\n, \\u000b \"">>, <<"\\, \b, \f, \r, \n, \v ">>},
{<<"[true, false \t \n \r \s]">>, [true, false]},
{<<"\s\t\r\n { \r\n \"currency\" \t\r\n: \"\\u20AC\"}">>, #{
<<"currency">> => <<"€"/utf8>>
}},
{<<"{\"currency\": \"\\u20ac\"}">>, #{<<"currency">> => <<"€"/utf8>>}},
{<<"{\"key1\":\"Val1\", \"key2\":null, \"key3\":\"Val3\"}">>, #{
<<"key1">> => <<"Val1">>, <<"key2">> => null, <<"key3">> => <<"Val3">>
}},

{<<"{\"key1\":\"Val1\"\t \n \r \s, \t \n \r \s}">>, #{
<<"key1">> => <<"Val1">>
}},
{<<"{\"d\":\"a6/\"}">>, #{<<"d">> => <<"a6/">>}},
{<<"null">>, null}
].

json_encode_only_cases() ->
[
{<<"\"\\\\, \\t, \\b, \\f, \\r, \\n \"">>, <<"\\, \t, \b, \f, \r, \n ">>},
{<<"\" \\u0000 \\u000B \\u001D \"">>, <<" \x00 \v \x1D ">>},
{<<"{\"listKey\":\"a6\"}">>, #{<<"listKey">> => <<"a6">>}},
{<<"{\"binaryKey\":\"a6\"}">>, #{<<"binaryKey">> => <<"a6">>}},
{<<"{}">>, #{}},
@@ -172,6 +173,10 @@ json_encode_only_cases() ->

json_cases() ->
[
{<<"\"\\\\, \\\", \\t, \\b, \\f, \\r, \\n \"">>, <<"\\, \", \t, \b, \f, \r, \n ">>},
{<<"\" \\u0000, \\t, \\u000B, \\r, \\u000E \"">>, <<" \x00, \t, \v, \x0D, \x0E ">>},
{<<"\" \\u000F, \\u001D, \\u001F, ¹, ï \"">>, <<" \x0F, \x1D, \x1F, \xb9, \xef ">>},
{<<"\" \\u0019, \\u000B, \\u000E, \\u000F \"">>, <<" \x19, \x0b, \x0e, \x0f ">>},
{<<"true">>, true},
{<<"false">>, false},
{<<"">>, undefined},
@@ -253,14 +258,16 @@ json_emoji() ->
[{userdata, [{doc, "Properly decoding json with emojis"}]}].

json_emoji(_Conf) ->
HoFBin = <<"{\"text\":{\"body\":\"\\u2764\\u200d\\ud83d\\udd25\"}}">>,
HoFBin = <<"{\"text\":{\"body\":\"\\u2764\\u200d\\ud83d\\udd25\\u0bef\"}}">>,
DecodedHoF = #{
<<"text">> => #{<<"body">> => <<226, 157, 164, 226, 128, 141, 240, 159, 148, 165>>}
<<"text">> => #{
<<"body">> => <<226, 157, 164, 226, 128, 141, 240, 159, 148, 165, 224, 175, 175>>
}
},
?assertEqual({ok, DecodedHoF}, njson:decode(HoFBin)),
EncodedHoF =
<<123, 34, 116, 101, 120, 116, 34, 58, 123, 34, 98, 111, 100, 121, 34, 58, 34, 226, 157,
164, 226, 128, 141, 240, 159, 148, 165, 34, 125, 125>>,
164, 226, 128, 141, 240, 159, 148, 165, 224, 175, 175, 34, 125, 125>>,
?assertEqual({ok, EncodedHoF}, njson:encode(DecodedHoF)),
io:format("~tp~n", [DecodedHoF]),
io:format("~ts~n", [EncodedHoF]).