allows atoms in terms passed to encoder
This commit is contained in:
parent
6dfaa61bfa
commit
fb1b21c67c
1 changed files with 13 additions and 1 deletions
|
@ -73,7 +73,7 @@ list_or_object(List, {Handler, State}, Opts) ->
|
|||
|
||||
object([{Key, Value}|Rest], {Handler, State}, Opts) ->
|
||||
object(Rest, {Handler,
|
||||
value(Value, {Handler, Handler:handle_event({key, Key}, State)}, Opts)
|
||||
value(Value, {Handler, Handler:handle_event({key, fix_key(Key, Opts)}, State)}, Opts)
|
||||
}, Opts);
|
||||
object([], {Handler, State}, _Opts) -> Handler:handle_event(end_object, State);
|
||||
object(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
|
@ -85,6 +85,11 @@ list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State);
|
|||
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
||||
|
||||
|
||||
fix_key(Key, Opts) when is_binary(Key) ->
|
||||
jsx_utils:json_escape(Key, Opts);
|
||||
fix_key(Key, Opts) when is_atom(Key) ->
|
||||
jsx_utils:json_escape(atom_to_binary(Key, utf8), Opts).
|
||||
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
@ -154,6 +159,13 @@ encode_test_() ->
|
|||
end_object,
|
||||
end_object,
|
||||
end_json])
|
||||
},
|
||||
{"atom keys", ?_assert(encode([{key, <<"value">>}])
|
||||
=:= [start_object,
|
||||
{key, <<"key">>},
|
||||
{string, <<"value">>},
|
||||
end_object,
|
||||
end_json])
|
||||
}
|
||||
].
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue