reworked interface and internals, encoder broken but needs wholesale changes to match new interface
This commit is contained in:
parent
738c6667f1
commit
14277e4972
6 changed files with 553 additions and 499 deletions
38
src/gen_json.erl
Normal file
38
src/gen_json.erl
Normal file
|
@ -0,0 +1,38 @@
|
|||
%% The MIT License
|
||||
|
||||
%% Copyright (c) 2011 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
||||
|
||||
%% Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
%% of this software and associated documentation files (the "Software"), to deal
|
||||
%% in the Software without restriction, including without limitation the rights
|
||||
%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
%% copies of the Software, and to permit persons to whom the Software is
|
||||
%% furnished to do so, subject to the following conditions:
|
||||
|
||||
%% The above copyright notice and this permission notice shall be included in
|
||||
%% all copies or substantial portions of the Software.
|
||||
|
||||
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
%% THE SOFTWARE.
|
||||
|
||||
|
||||
-module(gen_json).
|
||||
|
||||
-export([behaviour_info/1]).
|
||||
-export([parser/2, parser/3]).
|
||||
|
||||
|
||||
behaviour_info(callbacks) -> [{init, 0}, {handle_event, 2}];
|
||||
behaviour_info(_) -> undefined.
|
||||
|
||||
|
||||
parser(Mod, Args) -> parser(Mod, Args, []).
|
||||
|
||||
parser(Mod, Args, Opts) -> fun(Input) when is_list(Input) -> (jsx:encoder(Mod, Args, Opts))(Input)
|
||||
; (Input) when is_binary(Input) -> (jsx:decoder(Mod, Args, Opts))(Input)
|
||||
end.
|
|
@ -4,6 +4,7 @@
|
|||
{vsn, "0.10.0"},
|
||||
{modules, [
|
||||
jsx,
|
||||
gen_json,
|
||||
jsx_encoder,
|
||||
jsx_decoder,
|
||||
jsx_format,
|
||||
|
|
118
src/jsx.erl
118
src/jsx.erl
|
@ -23,11 +23,17 @@
|
|||
|
||||
-module(jsx).
|
||||
|
||||
-export([scanner/0, scanner/1]).
|
||||
-export([encoder/0, encoder/1]).
|
||||
-export([decoder/0, decoder/1]).
|
||||
-export([fold/3, fold/4]).
|
||||
-export([format/1, format/2]).
|
||||
-export([decoder/2, decoder/3]).
|
||||
%% shims for jsx_format, jsx_terms, jsx_verify
|
||||
-export([to_json/1, to_json/2]).
|
||||
-export([to_term/1, to_term/2]).
|
||||
-export([is_json/1, is_json/2]).
|
||||
|
||||
%% test handler
|
||||
-ifdef(TEST).
|
||||
-export([init/1, handle_event/2]).
|
||||
-endif.
|
||||
|
||||
|
||||
%% various semi-useful types with nowhere else to hang out
|
||||
|
@ -52,28 +58,16 @@
|
|||
-type opts() :: [opt()].
|
||||
-type opt() :: loose_unicode | escape_forward_slashes | explicit_end.
|
||||
|
||||
-type scanner() :: decoder() | encoder().
|
||||
|
||||
|
||||
-spec scanner() -> scanner().
|
||||
-spec scanner(OptsList::opts()) -> scanner().
|
||||
|
||||
scanner() -> scanner([]).
|
||||
|
||||
scanner(OptsList) when is_list(OptsList) ->
|
||||
fun(JSON) when is_binary(JSON) -> (decoder(OptsList))(JSON)
|
||||
; (Terms) when is_list(Terms) -> (encoder(OptsList))(Terms)
|
||||
end.
|
||||
|
||||
|
||||
-type decoder() :: fun((binary()) -> {ok, events()} | {incomplete, decoder()}).
|
||||
|
||||
-spec decoder() -> decoder().
|
||||
-spec decoder(OptsList::opts()) -> decoder().
|
||||
-spec decoder(Mod::module(), Args::any()) -> decoder().
|
||||
-spec decoder(Mod::module(), Args::any(), OptsList::opts()) -> decoder().
|
||||
|
||||
decoder() -> decoder([]).
|
||||
decoder(Mod, Args) -> decoder(Mod, Args, []).
|
||||
|
||||
decoder(OptsList) when is_list(OptsList) -> jsx_decoder:decoder(OptsList).
|
||||
decoder(Mod, Args, OptsList) when is_list(OptsList) -> jsx_decoder:decoder(Mod, Args, OptsList).
|
||||
|
||||
|
||||
-type encoder() :: fun((list()) ->
|
||||
|
@ -87,40 +81,29 @@ encoder() -> encoder([]).
|
|||
encoder(OptsList) when is_list(OptsList) -> jsx_encoder:encoder(OptsList).
|
||||
|
||||
|
||||
-spec fold(fun((Elem::event(), AccIn::any()) -> AccOut::any()),
|
||||
AccInitial::any(),
|
||||
Source::binary()) ->
|
||||
{ok, AccFinal::any()} | {incomplete, decoder()}
|
||||
; (fun((Elem::event(), AccIn::any()) -> AccOut::any()),
|
||||
AccInitial::any(),
|
||||
Source::list()) ->
|
||||
{ok, AccFinal::any()} | {incomplete, encoder()}.
|
||||
-spec fold(fun((Elem::event(), AccIn::any()) -> AccOut::any()),
|
||||
AccInitial::any(),
|
||||
Source::binary(),
|
||||
OptsLists::opts()) ->
|
||||
{ok, AccFinal::any()} | {incomplete, decoder()}
|
||||
; (fun((Elem::event(), AccIn::any()) -> AccOut::any()),
|
||||
AccInitial::any(),
|
||||
Source::list(),
|
||||
OptsList::opts()) ->
|
||||
{ok, AccFinal::any()} | {incomplete, encoder()}.
|
||||
|
||||
fold(Fun, Acc, Source) -> fold(Fun, Acc, Source, []).
|
||||
-spec to_json(Source::binary() | list()) -> binary().
|
||||
-spec to_json(Source::binary() | list(), Opts::jsx_format:opts()) -> binary().
|
||||
|
||||
fold(Fun, Acc, Source, Opts) ->
|
||||
case (scanner(Opts))(Source) of
|
||||
{ok, Events} -> lists:foldl(Fun, Acc, Events)
|
||||
; {incomplete, F} -> {incomplete, F}
|
||||
end.
|
||||
to_json(Source) -> to_json(Source, []).
|
||||
|
||||
to_json(Source, Opts) -> jsx_format:to_json(Source, Opts).
|
||||
|
||||
|
||||
-spec format(Source::binary()) -> binary().
|
||||
-spec format(Source::binary() | list(), Opts::jsx_format:opts()) -> binary().
|
||||
-spec to_term(Source::binary() | list()) -> list().
|
||||
-spec to_term(Source::binary() | list(), Opts::jsx_terms:opts()) -> list().
|
||||
|
||||
format(Source) -> format(Source, []).
|
||||
to_term(Source) -> to_term(Source, []).
|
||||
|
||||
format(Source, Opts) -> jsx_format:format(Source, Opts).
|
||||
to_term(Source, Opts) -> jsx_terms:to_term(Source, Opts).
|
||||
|
||||
|
||||
-spec is_json(Source::binary() | list()) -> true | false.
|
||||
-spec is_json(Source::binary() | list(), Opts::jsx_verify:opts()) -> true | false.
|
||||
|
||||
is_json(Source) -> is_json(Source, []).
|
||||
|
||||
is_json(Source, Opts) -> jsx_verify:is_json(Source, Opts).
|
||||
|
||||
|
||||
|
||||
|
@ -135,9 +118,9 @@ jsx_decoder_test_() ->
|
|||
encoder_decoder_equiv_test_() ->
|
||||
[
|
||||
{"encoder/decoder equivalency",
|
||||
?_assert(begin {ok, X} = (jsx:decoder())(
|
||||
?_assert((jsx:decoder())(
|
||||
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>
|
||||
), X end =:= begin {ok, Y} = (jsx:encoder())(
|
||||
) =:= (jsx:encoder())(
|
||||
[start_array,
|
||||
{string, <<"a">>},
|
||||
{integer, 17},
|
||||
|
@ -151,22 +134,21 @@ encoder_decoder_equiv_test_() ->
|
|||
end_array,
|
||||
end_array,
|
||||
end_json]
|
||||
), Y end
|
||||
)
|
||||
)
|
||||
}
|
||||
].
|
||||
|
||||
|
||||
fold_test_() ->
|
||||
[{"fold test",
|
||||
?_assert(fold(fun(_, true) -> true end,
|
||||
true,
|
||||
<<"[\"a\", 17, 3.14, true, {\"k\":false}, []]">>,
|
||||
[]
|
||||
))
|
||||
}].
|
||||
|
||||
|
||||
%% test handler
|
||||
init([]) -> [].
|
||||
|
||||
handle_event(end_json, State) -> lists:reverse([end_json] ++ State);
|
||||
handle_event(Event, State) -> [Event] ++ State.
|
||||
|
||||
|
||||
|
||||
jsx_decoder_gen([]) -> [];
|
||||
jsx_decoder_gen([Test|Rest]) ->
|
||||
Name = proplists:get_value(name, Test),
|
||||
|
@ -216,13 +198,13 @@ parse_tests([], _Dir, Acc) ->
|
|||
|
||||
decode(JSON, Flags) ->
|
||||
try
|
||||
case (jsx:scanner(Flags))(JSON) of
|
||||
{ok, Events} -> Events
|
||||
; {incomplete, More} ->
|
||||
case (gen_json:parser(?MODULE, [], Flags))(JSON) of
|
||||
{incomplete, More} ->
|
||||
case More(<<" ">>) of
|
||||
{ok, Events} -> Events
|
||||
; _ -> {error, badjson}
|
||||
{incomplete, _} -> {error, badjson}
|
||||
; Events -> Events
|
||||
end
|
||||
; Events -> Events
|
||||
end
|
||||
catch
|
||||
error:badarg -> {error, badjson}
|
||||
|
@ -230,15 +212,15 @@ decode(JSON, Flags) ->
|
|||
|
||||
|
||||
incremental_decode(<<C:1/binary, Rest/binary>>, Flags) ->
|
||||
P = jsx:scanner(Flags ++ [explicit_end]),
|
||||
P = gen_json:parser(?MODULE, [], Flags ++ [explicit_end]),
|
||||
try incremental_decode_loop(P(C), Rest)
|
||||
catch error:badarg -> io:format("~p~n", [erlang:get_stacktrace()]), {error, badjson}
|
||||
end.
|
||||
|
||||
incremental_decode_loop({incomplete, More}, <<>>) ->
|
||||
case More(end_stream) of
|
||||
{ok, X} -> X
|
||||
; _ -> {error, badjson}
|
||||
{incomplete, _} -> {error, badarg}
|
||||
; X -> X
|
||||
end;
|
||||
incremental_decode_loop({incomplete, More}, <<C:1/binary, Rest/binary>>) ->
|
||||
incremental_decode_loop(More(C), Rest).
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -130,7 +130,7 @@ maybe_done([], T, Stack, Opts) -> ?incomplete(maybe_done, T, Stack, Opts);
|
|||
maybe_done(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
||||
|
||||
|
||||
done([], T, [], _Opts) -> {ok, lists:reverse(T)};
|
||||
done([], T, [], _Opts) -> lists:reverse(T);
|
||||
done(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
||||
|
||||
|
||||
|
@ -140,7 +140,7 @@ done(Forms, T, Stack, Opts) -> ?error([Forms, T, Stack, Opts]).
|
|||
|
||||
encode(Terms) ->
|
||||
try case (jsx:encoder([]))(Terms) of
|
||||
{ok, Terms} -> true
|
||||
Terms -> true
|
||||
end
|
||||
catch
|
||||
error:badarg -> false
|
||||
|
|
|
@ -23,7 +23,8 @@
|
|||
|
||||
-module(jsx_format).
|
||||
|
||||
-export([format/2]).
|
||||
-export([to_json/2]).
|
||||
-export([init/1, handle_event/2]).
|
||||
|
||||
|
||||
-record(opts, {
|
||||
|
@ -35,15 +36,15 @@
|
|||
-type opts() :: [].
|
||||
|
||||
|
||||
-spec format(Source::(binary() | list()), Opts::opts()) -> binary().
|
||||
-spec to_json(Source::(binary() | list()), Opts::opts()) -> binary().
|
||||
|
||||
format(Source, Opts) when (is_binary(Source) andalso is_list(Opts))
|
||||
to_json(Source, Opts) when (is_binary(Source) andalso is_list(Opts))
|
||||
orelse (is_list(Source) andalso is_list(Opts)) ->
|
||||
jsx:fold(fun fold/2,
|
||||
{start, [], parse_opts(Opts)},
|
||||
Source,
|
||||
extract_opts(Opts)
|
||||
).
|
||||
(gen_json:parser(?MODULE, Opts, extract_opts(Opts)))(Source).
|
||||
|
||||
|
||||
|
||||
init(Opts) -> {start, [], parse_opts(Opts)}.
|
||||
|
||||
|
||||
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
|
||||
|
@ -89,13 +90,13 @@ extract_parser_opts([K|Rest], Acc) ->
|
|||
-define(newline, <<"\n">>).
|
||||
|
||||
|
||||
fold(Event, {start, Acc, Opts}) ->
|
||||
handle_event(Event, {start, Acc, Opts}) ->
|
||||
case Event of
|
||||
{Type, Value} -> {[], [Acc, encode(Type, Value)], Opts}
|
||||
; start_object -> {[object_start], [Acc, ?start_object], Opts}
|
||||
; start_array -> {[array_start], [Acc, ?start_array], Opts}
|
||||
end;
|
||||
fold(Event, {[object_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
||||
handle_event(Event, {[object_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
||||
Opts = OldOpts#opts{depth = Depth + 1},
|
||||
case Event of
|
||||
{key, Key} ->
|
||||
|
@ -103,7 +104,7 @@ fold(Event, {[object_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
|||
; end_object ->
|
||||
{Stack, [Acc, ?end_object], OldOpts}
|
||||
end;
|
||||
fold(Event, {[object_value|Stack], Acc, Opts}) ->
|
||||
handle_event(Event, {[object_value|Stack], Acc, Opts}) ->
|
||||
case Event of
|
||||
{Type, Value} when Type == string; Type == literal;
|
||||
Type == integer; Type == float ->
|
||||
|
@ -111,7 +112,7 @@ fold(Event, {[object_value|Stack], Acc, Opts}) ->
|
|||
; start_object -> {[object_start, key|Stack], [Acc, ?start_object], Opts}
|
||||
; start_array -> {[array_start, key|Stack], [Acc, ?start_array], Opts}
|
||||
end;
|
||||
fold(Event, {[key|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
||||
handle_event(Event, {[key|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
||||
case Event of
|
||||
{key, Key} ->
|
||||
{[object_value|Stack], [Acc, ?comma, indent_or_space(Opts), encode(string, Key), ?colon, space(Opts)], Opts}
|
||||
|
@ -119,7 +120,7 @@ fold(Event, {[key|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
|||
NewOpts = Opts#opts{depth = Depth - 1},
|
||||
{Stack, [Acc, indent(NewOpts), ?end_object], NewOpts}
|
||||
end;
|
||||
fold(Event, {[array_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
||||
handle_event(Event, {[array_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
||||
Opts = OldOpts#opts{depth = Depth + 1},
|
||||
case Event of
|
||||
{Type, Value} when Type == string; Type == literal;
|
||||
|
@ -129,7 +130,7 @@ fold(Event, {[array_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
|||
; start_array -> {[array_start, array|Stack], [Acc, indent(Opts), ?start_array], Opts}
|
||||
; end_array -> {Stack, [Acc, ?end_array], OldOpts}
|
||||
end;
|
||||
fold(Event, {[array|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
||||
handle_event(Event, {[array|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
||||
case Event of
|
||||
{Type, Value} when Type == string; Type == literal;
|
||||
Type == integer; Type == float ->
|
||||
|
@ -140,7 +141,7 @@ fold(Event, {[array|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
|||
; start_object -> {[object_start, array|Stack], [Acc, ?comma, indent_or_space(Opts), ?start_object], Opts}
|
||||
; start_array -> {[array_start, array|Stack], [Acc, ?comma, indent_or_space(Opts), ?start_array], Opts}
|
||||
end;
|
||||
fold(end_json, {[], Acc, _Opts}) -> unicode:characters_to_binary(Acc, utf8).
|
||||
handle_event(end_json, {[], Acc, _Opts}) -> unicode:characters_to_binary(Acc, utf8).
|
||||
|
||||
|
||||
encode(string, String) ->
|
||||
|
@ -184,6 +185,8 @@ indent_or_space(Opts) ->
|
|||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
format(Source, Opts) -> to_json(Source, Opts).
|
||||
|
||||
basic_test_() ->
|
||||
[
|
||||
{"empty object", ?_assert(format(<<"{}">>, []) =:= <<"{}">>)},
|
||||
|
@ -290,17 +293,5 @@ ext_opts_test_() ->
|
|||
) =:= <<"[]">>
|
||||
)}
|
||||
].
|
||||
|
||||
terms_test_() ->
|
||||
[
|
||||
{"terms",
|
||||
?_assert(format([start_object,
|
||||
{key, <<"key">>},
|
||||
{string, <<"value">>},
|
||||
end_object,
|
||||
end_json
|
||||
], []) =:= <<"{\"key\":\"value\"}">>
|
||||
)}
|
||||
].
|
||||
|
||||
-endif.
|
Loading…
Add table
Add a link
Reference in a new issue