Skip to content

Commit f04aeeb

Browse files
committed
change nil handling in jsx mode: fixes #27
1 parent db8a0c0 commit f04aeeb

4 files changed

Lines changed: 18 additions & 2 deletions

File tree

src/msgpack.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ enable_str_test() ->
173173
?assertEqual(<<167:8, (<<"saitama">>)/binary >>,
174174
msgpack:pack(<<"saitama">>, [{enable_str, false}])),
175175
?assertEqual(<<196,7,115,97,105,116,97,109,97>>,
176-
msgpack:pack(<<"saitama">>, [{enable_str, true}])).
176+
msgpack:pack(<<"saitama">>, [{enable_str, true}])).
177177

178178
basic_test()->
179179
Tests = test_data(),

src/msgpack_packer.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ pack(I, _) when is_integer(I) ->
3232
pack_uint(I);
3333
pack(F, _) when is_float(F) ->
3434
pack_double(F);
35-
pack(nil, _) ->
35+
pack(null, _Opt = ?OPTION{interface=jsx}) ->
36+
<< 16#C0:8 >>;
37+
pack(nil, _Opt = ?OPTION{interface=Interface})
38+
when Interface =/= jsx ->
3639
<< 16#C0:8 >>;
3740
pack(false, _) ->
3841
<< 16#C2:8 >>;

src/msgpack_unpacker.erl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
%% unpack them all
2929
-spec unpack_stream(Bin::binary(), msgpack_option()) -> {msgpack:object(), binary()} | no_return().
3030
%% ATOMS
31+
unpack_stream(<<16#C0, Rest/binary>>, _Opt = ?OPTION{interface=jsx}) ->
32+
{null, Rest};
3133
unpack_stream(<<16#C0, Rest/binary>>, _) ->
3234
{nil, Rest};
3335
unpack_stream(<<16#C2, Rest/binary>>, _) ->

test/msgpack_test.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ issue_jiffy_5_test() ->
146146
?assertEqual(Term, Decoded).
147147

148148

149+
issue_27_test_() ->
150+
[?_assertEqual({ok, null},
151+
msgpack:unpack(msgpack:pack(nil), [{format,jsx}])),
152+
?_assertEqual({ok, nil},
153+
msgpack:unpack(msgpack:pack(null, [{format,jsx}]))),
154+
?_assertEqual({ok, <<"null">>},
155+
msgpack:unpack(msgpack:pack(null, [{allow_atom,pack}]))),
156+
?_assertEqual({ok, <<"nil">>},
157+
msgpack:unpack(msgpack:pack(nil,
158+
[{format,jsx},{allow_atom,pack}])))].
159+
149160
string_test() ->
150161
{ok, CWD} = file:get_cwd(),
151162
Path = CWD ++ "/../test/utf8.txt",

0 commit comments

Comments
 (0)