Skip to content

Commit

Permalink
Fix non suported map comprehension
Browse files Browse the repository at this point in the history
  • Loading branch information
williamthome committed Aug 4, 2024
1 parent a62ad91 commit 4b419a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ encode_map(Map, Encode) when is_map(Map) ->
do_encode_map(Map, Encode).

do_encode_map(Map, Encode) when is_function(Encode, 2) ->
encode_object([[$,, key(Key, Encode), $: | Encode(Value, Encode)] || Key := Value <- Map]).
Proplist = maps:to_list(Map),
encode_object([[$,, key(Key, Encode), $: | Encode(Value, Encode)] || {Key, Value} <- Proplist]).

%% @doc
%% Encoder for maps as JSON objects.
Expand Down
22 changes: 20 additions & 2 deletions test/json_polyfill_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@ all() ->
[encode, decode].

encode(Config) when is_list(Config) ->
?assertEqual([$", <<"foo">>, $"], json:encode(foo)).
?assertEqual(
[91,<<"null">>,44,
["{",
[[34,<<"foo">>,34],58,34,<<"bar">>,34],
[[44,[34,<<"bar">>,34],58,34,<<"baz">>,34]],
"}"],
93],
json:encode([null, #{foo => bar, bar => baz}])
).

decode(Config) when is_list(Config) ->
?assertEqual(<<"foo">>, json:decode(<<"\"foo\"">>)).
?assertEqual(
[null,#{<<"bar">> => <<"baz">>,<<"foo">> => <<"bar">>}],
json:decode(iolist_to_binary(
[91,<<"null">>,44,
["{",
[[34,<<"foo">>,34],58,34,<<"bar">>,34],
[[44,[34,<<"bar">>,34],58,34,<<"baz">>,34]],
"}"],
93]
))
).

0 comments on commit 4b419a2

Please sign in to comment.