formatting
This commit is contained in:
parent
c7ea7a6f08
commit
2cf20cf809
1 changed files with 33 additions and 35 deletions
68
src/jsx.erl
68
src/jsx.erl
|
@ -27,6 +27,38 @@
|
|||
-export([decoder/0, decoder/1, decoder/2]).
|
||||
|
||||
|
||||
decoder() ->
|
||||
decoder([]).
|
||||
|
||||
decoder(Opts) ->
|
||||
F = fun(end_of_stream, State) -> lists:reverse(State) ;(Event, State) -> [Event] ++ State end,
|
||||
decoder({F, []}, Opts).
|
||||
|
||||
decoder({F, _} = Callbacks, OptsList) when is_list(OptsList), is_function(F) ->
|
||||
Opts = parse_opts(OptsList),
|
||||
decoder(Callbacks, Opts);
|
||||
decoder({{Mod, Fun}, State}, OptsList) when is_list(OptsList), is_atom(Mod), is_atom(Fun) ->
|
||||
Opts = parse_opts(OptsList),
|
||||
decoder({fun(E, S) -> Mod:Fun(E, S) end, State}, Opts);
|
||||
decoder(Callbacks, Opts) ->
|
||||
fun(Stream) -> try start(Stream, [], Callbacks, Opts) catch error:function_clause -> {error, badjson} ;error:badjson -> {error, badjson} end end.
|
||||
|
||||
|
||||
parse_opts(Opts) ->
|
||||
parse_opts(Opts, {false, codepoint}).
|
||||
|
||||
parse_opts([], Opts) ->
|
||||
Opts;
|
||||
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode}) ->
|
||||
true = lists:member(Value, [true, false]),
|
||||
parse_opts(Rest, {Value, EscapedUnicode});
|
||||
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode}) ->
|
||||
true = lists:member(Value, [ascii, codepoint, none]),
|
||||
parse_opts(Rest, {Comments, Value});
|
||||
parse_opts([_UnknownOpt|Rest], Opts) ->
|
||||
parse_opts(Rest, Opts).
|
||||
|
||||
|
||||
%% option flags
|
||||
|
||||
-define(comments_true(X), {true, _} = X).
|
||||
|
@ -87,38 +119,6 @@
|
|||
).
|
||||
|
||||
|
||||
decoder() ->
|
||||
decoder([]).
|
||||
|
||||
decoder(Opts) ->
|
||||
F = fun(end_of_stream, State) -> lists:reverse(State) ;(Event, State) -> [Event] ++ State end,
|
||||
decoder({F, []}, Opts).
|
||||
|
||||
decoder({F, _} = Callbacks, OptsList) when is_list(OptsList), is_function(F) ->
|
||||
Opts = parse_opts(OptsList),
|
||||
decoder(Callbacks, Opts);
|
||||
decoder({{Mod, Fun}, State}, OptsList) when is_list(OptsList), is_atom(Mod), is_atom(Fun) ->
|
||||
Opts = parse_opts(OptsList),
|
||||
decoder({fun(E, S) -> Mod:Fun(E, S) end, State}, Opts);
|
||||
decoder(Callbacks, Opts) ->
|
||||
fun(Stream) -> try start(Stream, [], Callbacks, Opts) catch error:function_clause -> {error, badjson} ;error:badjson -> {error, badjson} end end.
|
||||
|
||||
|
||||
parse_opts(Opts) ->
|
||||
parse_opts(Opts, {false, codepoint}).
|
||||
|
||||
parse_opts([], Opts) ->
|
||||
Opts;
|
||||
parse_opts([{comments, Value}|Rest], {_Comments, EscapedUnicode}) ->
|
||||
true = lists:member(Value, [true, false]),
|
||||
parse_opts(Rest, {Value, EscapedUnicode});
|
||||
parse_opts([{escaped_unicode, Value}|Rest], {Comments, _EscapedUnicode}) ->
|
||||
true = lists:member(Value, [ascii, codepoint, none]),
|
||||
parse_opts(Rest, {Comments, Value});
|
||||
parse_opts([_UnknownOpt|Rest], Opts) ->
|
||||
parse_opts(Rest, Opts).
|
||||
|
||||
|
||||
%% this code is mostly autogenerated and mostly ugly. apologies. for more insight on
|
||||
%% Callbacks or Opts, see the comments accompanying decoder/2 (in jsx.erl). Stack
|
||||
%% is a stack of flags used to track depth and to keep track of whether we are
|
||||
|
@ -274,9 +274,7 @@ string(<<?rsolidus, Rest/binary>>, Stack, Callbacks, Opts, Acc) ->
|
|||
string(<<S/utf8, Rest/binary>>, Stack, Callbacks, Opts, Acc) when ?is_noncontrol(S) ->
|
||||
string(Rest, Stack, Callbacks, Opts, [S] ++ Acc);
|
||||
string(Bin, Stack, Callbacks, Opts, Acc) ->
|
||||
{incomplete, fun(Stream) -> string(<<Bin/binary, Stream/binary>>, Stack, Callbacks, Opts, Acc) end};
|
||||
string(<<>>, Stack, Callbacks, Opts, Acc) ->
|
||||
{incomplete, fun(Stream) -> string(Stream, Stack, Callbacks, Opts, Acc) end}.
|
||||
{incomplete, fun(Stream) -> string(<<Bin/binary, Stream/binary>>, Stack, Callbacks, Opts, Acc) end}.
|
||||
|
||||
|
||||
%% only thing to note here is the additional accumulator passed to escaped_unicode used
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue