Skip to content

Commit

Permalink
Merge pull request #13 from zotonic/atom-lists
Browse files Browse the repository at this point in the history
Encode lists of atoms as a list of strings
  • Loading branch information
mworrell authored Sep 6, 2024
2 parents 6e45194 + f1fb78c commit 636263b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

strategy:
matrix:
otp_version: [24,25,26,27]
otp_version: [25,26,27]
os: [ubuntu-latest]

container:
Expand Down
8 changes: 7 additions & 1 deletion src/jsxrecord.erl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ encode_list([{K, _} | _] = Proplist, Opts) when ?IS_PROPLIST_KEY(K) ->
Map = proplists:to_map(Proplist),
euneus_encoder:encode_map(Map, Opts);
encode_list(List, Opts) ->
euneus_encoder:encode_list(List, Opts).
case lists:all(fun is_atom/1, List) of
true ->
List1 = [ atom_to_binary(A, utf8) || A <- List ],
euneus_encoder:encode_list(List1, Opts);
false ->
euneus_encoder:encode_list(List, Opts)
end.

encode_tuple({struct, MochiJSON}, Opts) ->
Map = mochijson_to_map(MochiJSON),
Expand Down
5 changes: 5 additions & 0 deletions test/jsxrecord_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ all() ->
dates,
times,
proplist,
atom_list,
record_proplist,
mixed_list,
unknown_term
Expand Down Expand Up @@ -98,6 +99,10 @@ proplist(_Config) ->
<<"{\"a\":1}">> = encode([ {a, 1} ]),
ok.

atom_list(_Config) ->
<<"[\"a\",\"b\"]">> = encode([ a, b ]),
ok.

record_proplist(_Config) ->
Tr = #trans{ tr = [ {en, <<"hello">>} ]},
Json = encode(Tr),
Expand Down

0 comments on commit 636263b

Please sign in to comment.