2011-07-17 00:37:24 -07:00
|
|
|
%% 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(jsx_encoder).
|
|
|
|
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
-export([encoder/1]).
|
2011-07-17 00:37:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
-include("jsx_common.hrl").
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
-spec encoder(Opts::jsx_opts()) -> jsx_encoder().
|
2011-07-17 00:37:24 -07:00
|
|
|
|
2011-07-21 06:14:48 -07:00
|
|
|
encoder(_) -> fun(Forms) -> start(Forms) end.
|
2011-07-17 00:37:24 -07:00
|
|
|
|
|
|
|
|
|
|
|
-define(ENDJSON,
|
|
|
|
{event, end_json, fun() ->
|
|
|
|
{incomplete, fun(Forms) -> {error, {badjson, Forms}} end}
|
|
|
|
end}
|
|
|
|
).
|
|
|
|
|
|
|
|
|
|
|
|
start({string, String}) when is_list(String) ->
|
|
|
|
{event, {string, String}, fun() -> ?ENDJSON end};
|
|
|
|
start({float, Float}) when is_list(Float) ->
|
|
|
|
{event, {float, Float}, fun() -> ?ENDJSON end};
|
|
|
|
start({integer, Int}) when is_list(Int) ->
|
|
|
|
{event, {integer, Int}, fun() -> ?ENDJSON end};
|
|
|
|
start({literal, Atom}) when Atom == true; Atom == false; Atom == null ->
|
|
|
|
{event, {literal, Atom}, fun() -> ?ENDJSON end};
|
|
|
|
%% second parameter is a stack to match end_foos to start_foos
|
|
|
|
start(Forms) -> list_or_object(Forms, []).
|
|
|
|
|
|
|
|
|
|
|
|
list_or_object([start_object|Forms], Stack) ->
|
|
|
|
{event, start_object, fun() -> key(Forms, [object] ++ Stack) end};
|
|
|
|
list_or_object([start_array|Forms], Stack) ->
|
|
|
|
{event, start_array, fun() -> value(Forms, [array] ++ Stack) end};
|
2011-07-17 17:50:03 -07:00
|
|
|
list_or_object([], Stack) ->
|
|
|
|
{incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, []}}
|
|
|
|
; (Stream) ->
|
|
|
|
list_or_object(Stream, Stack)
|
|
|
|
end};
|
2011-07-17 00:37:24 -07:00
|
|
|
list_or_object(Forms, _) -> {error, {badjson, Forms}}.
|
|
|
|
|
|
|
|
|
|
|
|
key([{key, Key}|Forms], Stack) when is_list(Key) ->
|
|
|
|
{event, {key, Key}, fun() -> value(Forms, Stack) end};
|
|
|
|
key([end_object|Forms], [object|Stack]) ->
|
|
|
|
{event, end_object, fun() -> maybe_done(Forms, Stack) end};
|
2011-07-17 17:50:03 -07:00
|
|
|
key([], Stack) ->
|
|
|
|
{incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, []}}
|
|
|
|
; (Stream) ->
|
|
|
|
key(Stream, Stack)
|
|
|
|
end};
|
2011-07-17 00:37:24 -07:00
|
|
|
key(Forms, _) -> {error, {badjson, Forms}}.
|
|
|
|
|
|
|
|
|
|
|
|
value([{string, S}|Forms], Stack) when is_list(S) ->
|
|
|
|
{event, {string, S}, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
value([{float, F}|Forms], Stack) when is_list(F) ->
|
|
|
|
{event, {float, F}, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
value([{integer, I}|Forms], Stack) when is_list(I) ->
|
|
|
|
{event, {integer, I}, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
value([{literal, L}|Forms], Stack) when L == true; L == false; L == null ->
|
|
|
|
{event, {literal, L}, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
value([start_object|Forms], Stack) ->
|
|
|
|
{event, start_object, fun() -> key(Forms, [object] ++ Stack) end};
|
|
|
|
value([start_array|Forms], Stack) ->
|
|
|
|
{event, start_array, fun() -> value(Forms, [array] ++ Stack) end};
|
|
|
|
value([end_array|Forms], [array|Stack]) ->
|
|
|
|
{event, end_array, fun() -> maybe_done(Forms, Stack) end};
|
2011-07-17 17:50:03 -07:00
|
|
|
value([], Stack) ->
|
|
|
|
{incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, []}}
|
|
|
|
; (Stream) ->
|
|
|
|
value(Stream, Stack)
|
|
|
|
end};
|
2011-07-17 00:37:24 -07:00
|
|
|
value(Forms, _) -> {error, {badjson, Forms}}.
|
|
|
|
|
|
|
|
|
|
|
|
maybe_done([], []) -> ?ENDJSON;
|
|
|
|
maybe_done([end_object|Forms], [object|Stack]) ->
|
|
|
|
{event, end_object, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
maybe_done([end_array|Forms], [array|Stack]) ->
|
|
|
|
{event, end_array, fun() -> maybe_done(Forms, Stack) end};
|
|
|
|
maybe_done(Forms, [object|_] = Stack) -> key(Forms, Stack);
|
|
|
|
maybe_done(Forms, [array|_] = Stack) -> value(Forms, Stack);
|
2011-07-17 17:50:03 -07:00
|
|
|
maybe_done([], Stack) ->
|
|
|
|
{incomplete, fun(end_stream) ->
|
|
|
|
{error, {badjson, []}}
|
|
|
|
; (Stream) ->
|
|
|
|
maybe_done(Stream, Stack)
|
|
|
|
end};
|
2011-07-17 00:37:24 -07:00
|
|
|
maybe_done(Forms, _) -> {error, {badjson, Forms}}.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
|
|
|
|
2011-07-20 07:14:59 -07:00
|
|
|
|
|
|
|
encode(Terms) -> encode_whole(Terms) andalso encode_incremental(Terms).
|
|
|
|
|
|
|
|
|
|
|
|
encode_whole(Terms) ->
|
2011-07-21 06:14:48 -07:00
|
|
|
case loop((encoder([]))(Terms), []) of
|
2011-07-17 00:37:24 -07:00
|
|
|
%% unwrap naked values
|
2011-07-20 07:14:59 -07:00
|
|
|
{ok, [Terms]} -> true
|
|
|
|
; {ok, Terms} -> true
|
|
|
|
; _ -> false
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
encode_incremental(Terms) when is_list(Terms) ->
|
2011-07-21 06:14:48 -07:00
|
|
|
encode_incremental(Terms, encoder([]), Terms, []);
|
2011-07-20 07:14:59 -07:00
|
|
|
%% we could feed naked terms to the regular encoder, but we already do that, so
|
|
|
|
%% cheat instead
|
|
|
|
encode_incremental(_) -> true.
|
|
|
|
|
|
|
|
encode_incremental([Term], F, Expected, Acc) ->
|
|
|
|
case loop(F([Term]), []) of
|
|
|
|
{ok, R} -> Expected =:= Acc ++ R
|
|
|
|
; _ -> false
|
|
|
|
end;
|
|
|
|
encode_incremental([Term|Terms], F, Expected, Acc) ->
|
|
|
|
case loop(F([Term]), []) of
|
|
|
|
{incomplete, Next, R} ->
|
|
|
|
encode_incremental(Terms, Next, Expected, Acc ++ R)
|
|
|
|
; _ ->
|
|
|
|
false
|
2011-07-17 00:37:24 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-07-20 07:14:59 -07:00
|
|
|
loop({error, _}, _) -> error;
|
|
|
|
loop({incomplete, Next}, Acc) -> {incomplete, Next, lists:reverse(Acc)};
|
2011-07-17 00:37:24 -07:00
|
|
|
loop({event, end_json, Next}, Acc) ->
|
|
|
|
{incomplete, F} = Next(),
|
|
|
|
{error, {badjson, []}} = F([]),
|
|
|
|
{ok, lists:reverse(Acc)};
|
|
|
|
loop({event, Event, Next}, Acc) -> loop(Next(), [Event] ++ Acc).
|
|
|
|
|
|
|
|
|
|
|
|
encode_test_() ->
|
|
|
|
[
|
2011-07-20 07:14:59 -07:00
|
|
|
{"empty object", ?_assert(encode([start_object, end_object]))},
|
2011-07-17 00:37:24 -07:00
|
|
|
{"empty array", ?_assert(encode([start_array, end_array]) =:= true)},
|
|
|
|
{"nested empty objects", ?_assert(encode([start_object,
|
|
|
|
{key, "empty object"},
|
|
|
|
start_object,
|
|
|
|
{key, "empty object"},
|
|
|
|
start_object,
|
|
|
|
end_object,
|
|
|
|
end_object,
|
|
|
|
end_object
|
2011-07-20 07:14:59 -07:00
|
|
|
]))},
|
2011-07-17 00:37:24 -07:00
|
|
|
{"nested empty arrays", ?_assert(encode([start_array,
|
|
|
|
start_array,
|
|
|
|
start_array,
|
|
|
|
end_array,
|
|
|
|
end_array,
|
|
|
|
end_array
|
2011-07-20 07:14:59 -07:00
|
|
|
]))},
|
2011-07-17 00:37:24 -07:00
|
|
|
{"simple object", ?_assert(encode([start_object,
|
|
|
|
{key, "a"},
|
|
|
|
{string, "hello"},
|
|
|
|
{key, "b"},
|
|
|
|
{integer, "1"},
|
|
|
|
{key, "c"},
|
|
|
|
{float, "1.0"},
|
|
|
|
{key, "d"},
|
|
|
|
{literal, true},
|
|
|
|
end_object
|
2011-07-20 07:14:59 -07:00
|
|
|
]))},
|
2011-07-17 00:37:24 -07:00
|
|
|
{"simple array", ?_assert(encode([start_array,
|
|
|
|
{string, "hello"},
|
|
|
|
{integer, "1"},
|
|
|
|
{float, "1.0"},
|
|
|
|
{literal, true},
|
|
|
|
end_array
|
2011-07-20 07:14:59 -07:00
|
|
|
]))},
|
|
|
|
{"unbalanced array", ?_assertNot(encode([start_array,
|
2011-07-17 00:37:24 -07:00
|
|
|
end_array,
|
|
|
|
end_array
|
2011-07-20 07:14:59 -07:00
|
|
|
]))},
|
|
|
|
{"naked string", ?_assert(encode({string, "hello"}))},
|
|
|
|
{"naked literal", ?_assert(encode({literal, true}))},
|
|
|
|
{"naked integer", ?_assert(encode({integer, "1"}))},
|
|
|
|
{"naked float", ?_assert(encode({float, "1.0"}))}
|
2011-07-17 00:37:24 -07:00
|
|
|
].
|
|
|
|
|
|
|
|
-endif.
|
|
|
|
|