Skip to content

Commit

Permalink
Merge pull request #14 from williamthome/wt-upgrade-euneus
Browse files Browse the repository at this point in the history
Use euneus options to encode proplists
  • Loading branch information
mworrell authored Sep 9, 2024
2 parents 636263b + a9ef748 commit 605ccb7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{require_min_otp_vsn, "21"}.

{deps, [
{euneus, "~>2.3"},
{euneus, "~>2.4"},
{json_polyfill, "0.1.4"}
]}.

Expand Down
6 changes: 3 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{"1.2.0",
[{<<"euneus">>,{pkg,<<"euneus">>,<<"2.3.0">>},0},
[{<<"euneus">>,{pkg,<<"euneus">>,<<"2.4.0">>},0},
{<<"json_polyfill">>,{pkg,<<"json_polyfill">>,<<"0.1.4">>},0}]}.
[
{pkg_hash,[
{<<"euneus">>, <<"4F9B77EFD1A5C7DD9C3CA0AB0C51172035BC39BFF28B37AA9789C173D1DD397A">>},
{<<"euneus">>, <<"E58178FB3B2C6FE139C863C4B347574E4C599BF1B9530ED3BAE7A10E2C7F870A">>},
{<<"json_polyfill">>, <<"ED9AD7A8CBDB8D1F1E59E22CDAE23A153A5BB93B5529AEBAEB189CA4B4C536C8">>}]},
{pkg_hash_ext,[
{<<"euneus">>, <<"335E8A4D6A7F822E4FEA72CA1D3DD56A456F4E13427AF2C0B77B872584CF8048">>},
{<<"euneus">>, <<"200EEFCE320314831AD79D77F03B109BC035E0A282B6B71FBCD3A7982804184D">>},
{<<"json_polyfill">>, <<"48C397EE2547FA459EDE01A30EC0E85717ABED3010867A63EEAAC5F203274303">>}]}
].
26 changes: 13 additions & 13 deletions src/jsxrecord.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ encode_json(Term) ->
codecs => [ timestamp, datetime ],
nulls => [undefined, null],
skip_values => [],
proplists => true,
encode_list => fun encode_list/2,
key_to_binary => fun key_to_binary/1,
proplists => {true, fun is_proplist/1},
encode_tuple => fun encode_tuple/2,
encode_pid => fun encode_unknown/2,
encode_port => fun encode_unknown/2,
Expand All @@ -113,17 +113,17 @@ decode_json(B) ->
},
euneus:decode(B, Options).

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) ->
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.
key_to_binary(Bin) when is_binary(Bin) ->
Bin;
key_to_binary(Atom) when is_atom(Atom) ->
atom_to_binary(Atom, utf8);
key_to_binary(Int) when is_integer(Int) ->
integer_to_binary(Int, 10).

is_proplist([{K, _} | _]) when ?IS_PROPLIST_KEY(K) ->
true;
is_proplist(_List) ->
false.

encode_tuple({struct, MochiJSON}, Opts) ->
Map = mochijson_to_map(MochiJSON),
Expand Down

0 comments on commit 605ccb7

Please sign in to comment.