Skip to content

Commit

Permalink
Refactor mochi#184 for less redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Dec 19, 2016
1 parent 6384fa9 commit f6e40f7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
8 changes: 4 additions & 4 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
% -*- mode: erlang -*-
{erl_opts, [debug_info,
{platform_define, "R15", 'gen_tcp_r15b_workaround'},
{platform_define, "(R14|R15|R16B-)", 'crypto_compatibility'},
{platform_define, "(R14|R15|R16B|17)", 'rand_mod_unavailable'},
{platform_define, "(17|18|19)", 'map_available'}]}.
{platform_define, "^R15", 'gen_tcp_r15b_workaround'},
{platform_define, "^(R14|R15|R16B-)", 'crypto_compatibility'},
{platform_define, "^(R14|R15|R16B|17)", 'rand_mod_unavailable'},
{platform_define, "^(R14|R15|R16)", 'map_unavailable'}]}.
{cover_enabled, true}.
{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
{dialyzer_opts, [{warnings, [no_return,
Expand Down
54 changes: 15 additions & 39 deletions src/mochijson2.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
-define(IS_WHITESPACE(C),
(C =:= $\s orelse C =:= $\t orelse C =:= $\r orelse C =:= $\n)).

-ifdef(map_unavailable).
-define(IS_MAP(_), false).
-else.
-define(IS_MAP(X), is_map(X)).
-endif.

%% @type json_string() = atom | binary()
%% @type json_number() = integer() | float()
%% @type json_array() = [json_term()]
Expand Down Expand Up @@ -150,7 +156,6 @@ parse_decoder_options([{format, Format} | Rest], State)
parse_decoder_options(Rest, State#decoder{object_hook=Format}).


-ifdef(map_available).
json_encode(true, _State) ->
<<"true">>;
json_encode(false, _State) ->
Expand All @@ -177,48 +182,14 @@ json_encode(Array, State) when is_list(Array) ->
json_encode_array(Array, State);
json_encode({array, Array}, State) when is_list(Array) ->
json_encode_array(Array, State);
json_encode(M, State) when is_map(M) ->
json_encode(M, State) when ?IS_MAP(M) ->
json_encode_map(M, State);
json_encode({json, IoList}, _State) ->
IoList;
json_encode(Bad, #encoder{handler=null}) ->
exit({json_encode, {bad_term, Bad}});
json_encode(Bad, State=#encoder{handler=Handler}) ->
json_encode(Handler(Bad), State).
-else.
json_encode(true, _State) ->
<<"true">>;
json_encode(false, _State) ->
<<"false">>;
json_encode(null, _State) ->
<<"null">>;
json_encode(I, _State) when is_integer(I) ->
integer_to_list(I);
json_encode(F, _State) when is_float(F) ->
mochinum:digits(F);
json_encode(S, State) when is_binary(S); is_atom(S) ->
json_encode_string(S, State);
json_encode([{K, _}|_] = Props, State) when (K =/= struct andalso
K =/= array andalso
K =/= json) ->
json_encode_proplist(Props, State);
json_encode({struct, Props}, State) when is_list(Props) ->
json_encode_proplist(Props, State);
json_encode({Props}, State) when is_list(Props) ->
json_encode_proplist(Props, State);
json_encode({}, State) ->
json_encode_proplist([], State);
json_encode(Array, State) when is_list(Array) ->
json_encode_array(Array, State);
json_encode({array, Array}, State) when is_list(Array) ->
json_encode_array(Array, State);
json_encode({json, IoList}, _State) ->
IoList;
json_encode(Bad, #encoder{handler=null}) ->
exit({json_encode, {bad_term, Bad}});
json_encode(Bad, State=#encoder{handler=Handler}) ->
json_encode(Handler(Bad), State).
-endif.

json_encode_array([], _State) ->
<<"[]">>;
Expand All @@ -240,7 +211,11 @@ json_encode_proplist(Props, State) ->
[$, | Acc1] = lists:foldl(F, "{", Props),
lists:reverse([$\} | Acc1]).

-ifdef(map_available).
-ifdef(map_unavailable).
json_encode_map(Bad, _State) ->
%% IS_MAP definition guarantees that this branch is dead
exit({json_encode, {bad_term, Bad}}).
-else.
json_encode_map(Map, _State) when map_size(Map) =:= 0 ->
<<"{}">>;
json_encode_map(Map, State) ->
Expand Down Expand Up @@ -990,11 +965,12 @@ utf8_non_character_test_() ->
[{"roundtrip escaped", ?_assertEqual(S, decode(encode(S)))},
{"roundtrip utf8", ?_assertEqual(S, decode((encoder([{utf8, true}]))(S)))}].

% iolist_to_binary(mochijson2:encode(#{a => 1, b => #{ c => 2}})).
-ifdef(map_available).
-ifndef(map_unavailable).

encode_map_test() ->
M = <<"{\"a\":1,\"b\":{\"c\":2}}">>,
?assertEqual(M, iolist_to_binary(encode(#{a => 1, b => #{ c => 2}}))).

encode_empty_map_test() ->
?assertEqual(<<"{}">>, encode(#{})).

Expand Down

0 comments on commit f6e40f7

Please sign in to comment.