2011-10-24 22:51:39 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 alisdair sullivan <alisdairsullivan@yahoo.ca>
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
%% 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.
|
|
|
|
|
|
|
|
|
2011-11-28 17:30:55 -08:00
|
|
|
-module(jsx_to_json).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
-export([to_json/2, format/2]).
|
2011-11-23 20:54:10 -08:00
|
|
|
-export([init/1, handle_event/2]).
|
2013-10-26 14:45:36 +00:00
|
|
|
-export([start_object/1, start_array/1, insert/2, insert/3, finish/1]).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-record(config, {
|
2011-10-26 12:39:28 -07:00
|
|
|
space = 0,
|
|
|
|
indent = 0,
|
|
|
|
depth = 0
|
2011-10-24 22:51:39 -07:00
|
|
|
}).
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-type config() :: list().
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-spec to_json(Source::any(), Config::config()) -> binary().
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
to_json(Source, Config) when is_list(Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
(jsx:encoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source).
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-spec format(Source::binary(), Config::config()) -> binary().
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
format(Source, Config) when is_binary(Source) andalso is_list(Config) ->
|
2013-06-04 01:14:11 +00:00
|
|
|
(jsx:decoder(?MODULE, Config, jsx_config:extract_config(Config ++ [escaped_strings])))(Source);
|
|
|
|
format(_, _) -> erlang:error(badarg).
|
2012-03-04 18:40:00 -08:00
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config(Config) -> parse_config(Config, #config{}).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([{space, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
|
|
|
|
parse_config(Rest, Config#config{space = Val});
|
|
|
|
parse_config([space|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{space = 1});
|
|
|
|
parse_config([{indent, Val}|Rest], Config) when is_integer(Val), Val > 0 ->
|
|
|
|
parse_config(Rest, Config#config{indent = Val});
|
|
|
|
parse_config([indent|Rest], Config) ->
|
|
|
|
parse_config(Rest, Config#config{indent = 1});
|
|
|
|
parse_config([{K, _}|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> parse_config(Rest, Config)
|
|
|
|
; false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([K|Rest] = Options, Config) ->
|
2013-03-06 01:37:08 -08:00
|
|
|
case lists:member(K, jsx_config:valid_flags()) of
|
2013-02-12 11:54:42 -08:00
|
|
|
true -> parse_config(Rest, Config)
|
|
|
|
; false -> erlang:error(badarg, [Options, Config])
|
2013-02-04 23:08:44 -08:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
parse_config([], Config) ->
|
|
|
|
Config.
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
-define(start_object, <<"{">>).
|
|
|
|
-define(start_array, <<"[">>).
|
|
|
|
-define(end_object, <<"}">>).
|
|
|
|
-define(end_array, <<"]">>).
|
|
|
|
-define(colon, <<":">>).
|
|
|
|
-define(comma, <<",">>).
|
|
|
|
-define(quote, <<"\"">>).
|
|
|
|
-define(space, <<" ">>).
|
|
|
|
-define(newline, <<"\n">>).
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
init(Config) -> {start, [], parse_config(Config)}.
|
2011-11-29 19:39:01 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {start, Acc, Config}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
2013-02-12 11:54:42 -08:00
|
|
|
{Type, Value} -> {[], [Acc, encode(Type, Value, Config)], Config}
|
|
|
|
; start_object -> {[object_start], [Acc, ?start_object], Config}
|
|
|
|
; start_array -> {[array_start], [Acc, ?start_array], Config}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {[object_start|Stack], Acc, OldConfig = #config{depth = Depth}}) ->
|
|
|
|
Config = OldConfig#config{depth = Depth + 1},
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{key, Key} ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{[object_value|Stack], [Acc, indent(Config), encode(string, Key, Config), ?colon, space(Config)], Config}
|
2011-10-24 22:51:39 -07:00
|
|
|
; end_object ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{Stack, [Acc, ?end_object], OldConfig}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {[object_value|Stack], Acc, Config}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{Type, Value} when Type == string; Type == literal;
|
|
|
|
Type == integer; Type == float ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{[key|Stack], [Acc, encode(Type, Value, Config)], Config}
|
|
|
|
; start_object -> {[object_start, key|Stack], [Acc, ?start_object], Config}
|
|
|
|
; start_array -> {[array_start, key|Stack], [Acc, ?start_array], Config}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {[key|Stack], Acc, Config = #config{depth = Depth}}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{key, Key} ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{[object_value|Stack], [Acc, ?comma, indent_or_space(Config), encode(string, Key, Config), ?colon, space(Config)], Config}
|
2011-10-24 22:51:39 -07:00
|
|
|
; end_object ->
|
2013-02-12 11:54:42 -08:00
|
|
|
NewConfig = Config#config{depth = Depth - 1},
|
|
|
|
{Stack, [Acc, indent(NewConfig), ?end_object], NewConfig}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {[array_start|Stack], Acc, OldConfig = #config{depth = Depth}}) ->
|
|
|
|
Config = OldConfig#config{depth = Depth + 1},
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{Type, Value} when Type == string; Type == literal;
|
|
|
|
Type == integer; Type == float ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{[array|Stack], [Acc, indent(Config), encode(Type, Value, Config)], Config}
|
|
|
|
; start_object -> {[object_start, array|Stack], [Acc, indent(Config), ?start_object], Config}
|
|
|
|
; start_array -> {[array_start, array|Stack], [Acc, indent(Config), ?start_array], Config}
|
|
|
|
; end_array -> {Stack, [Acc, ?end_array], OldConfig}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(Event, {[array|Stack], Acc, Config = #config{depth = Depth}}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{Type, Value} when Type == string; Type == literal;
|
|
|
|
Type == integer; Type == float ->
|
2013-02-12 11:54:42 -08:00
|
|
|
{[array|Stack], [Acc, ?comma, indent_or_space(Config), encode(Type, Value, Config)], Config}
|
2011-10-26 12:39:28 -07:00
|
|
|
; end_array ->
|
2013-02-12 11:54:42 -08:00
|
|
|
NewConfig = Config#config{depth = Depth - 1},
|
|
|
|
{Stack, [Acc, indent(NewConfig), ?end_array], NewConfig}
|
|
|
|
; start_object -> {[object_start, array|Stack], [Acc, ?comma, indent_or_space(Config), ?start_object], Config}
|
|
|
|
; start_array -> {[array_start, array|Stack], [Acc, ?comma, indent_or_space(Config), ?start_array], Config}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2013-02-12 11:54:42 -08:00
|
|
|
handle_event(end_json, {[], Acc, _Config}) -> unicode:characters_to_binary(Acc, utf8).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(string, String, _Config) ->
|
2012-03-24 19:42:00 -07:00
|
|
|
[?quote, String, ?quote];
|
2013-10-26 14:45:36 +00:00
|
|
|
encode(key, Key, _Config) ->
|
|
|
|
[?quote, Key, ?quote];
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(literal, Literal, _Config) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
erlang:atom_to_list(Literal);
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(integer, Integer, _Config) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
erlang:integer_to_list(Integer);
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(float, Float, _Config) ->
|
2012-03-31 12:34:29 -07:00
|
|
|
[Output] = io_lib:format("~p", [Float]), Output.
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
space(Config) ->
|
|
|
|
case Config#config.space of
|
2011-10-26 06:33:53 -07:00
|
|
|
0 -> []
|
2013-02-04 23:30:28 -08:00
|
|
|
; X when X > 0 -> binary:copy(?space, X)
|
2011-10-26 06:33:53 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(Config) ->
|
|
|
|
case Config#config.indent of
|
2011-10-26 12:39:28 -07:00
|
|
|
0 -> []
|
|
|
|
; X when X > 0 ->
|
2013-02-04 23:30:28 -08:00
|
|
|
Indent = binary:copy(?space, X),
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(Indent, Config#config.depth, [?newline])
|
2011-10-26 12:39:28 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
indent(_Indent, 0, Acc) -> Acc;
|
|
|
|
indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]).
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
indent_or_space(Config) ->
|
|
|
|
case Config#config.indent > 0 of
|
|
|
|
true -> indent(Config)
|
|
|
|
; false -> space(Config)
|
2011-10-26 12:39:28 -07:00
|
|
|
end.
|
|
|
|
|
|
|
|
|
2013-10-26 14:45:36 +00:00
|
|
|
%% internal state is a stack of in progress objects/arrays
|
|
|
|
%% `[Current, Parent, Grandparent,...OriginalAncestor]`
|
|
|
|
%% an object has the representation on the stack of
|
|
|
|
%% `{object, Object}`
|
|
|
|
%% of if there's a key with a yet to be matched value
|
|
|
|
%% `{object, Key, Object}`
|
|
|
|
%% an array looks like
|
|
|
|
%% `{array, Array}`
|
|
|
|
%% `Object` and `Array` are utf8 encoded binaries
|
|
|
|
|
|
|
|
%% allocate a new object on top of the stack
|
|
|
|
start_object(Stack) -> [{object, ?start_object}] ++ Stack.
|
|
|
|
|
|
|
|
%% allocate a new array on top of the stack
|
|
|
|
start_array(Stack) -> [{array, ?start_array}] ++ Stack.
|
|
|
|
|
|
|
|
%% finish an object or array and insert it into the parent object if it exists
|
|
|
|
finish([{object, Object}]) -> <<Object/binary, ?end_object/binary>>;
|
|
|
|
finish([{object, Object}|Rest]) -> insert(<<Object/binary, ?end_object/binary>>, Rest);
|
|
|
|
finish([{array, Array}]) -> <<Array/binary, ?end_array/binary>>;
|
|
|
|
finish([{array, Array}|Rest]) -> insert(<<Array/binary, ?end_array/binary>>, Rest);
|
|
|
|
finish(_) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
%% insert a value when there's no parent object or array
|
|
|
|
insert(Value, []) when is_binary(Value) -> Value;
|
|
|
|
%% insert a key or value into an object or array, autodetects the 'right' thing
|
|
|
|
insert(Key, [{object, Object}|Rest]) when is_binary(Key) -> [{object, Key, Object}] ++ Rest;
|
|
|
|
insert(Value, [{object, Key, ?start_object}|Rest]) when is_binary(Value) ->
|
|
|
|
[{object, <<?start_object/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(Value, [{object, Key, Object}|Rest]) when is_binary(Value) ->
|
|
|
|
[{object, <<Object/binary, ?comma/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(Value, [{array, ?start_array}|Rest]) when is_binary(Value) ->
|
|
|
|
[{array, <<?start_array/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(Value, [{array, Array}|Rest]) when is_binary(Value) ->
|
|
|
|
[{array, <<Array/binary, ?comma/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(_, _) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
%% insert a key/value pair into an object
|
|
|
|
insert(Key, Value, [{object, ?start_object}|Rest]) when is_binary(Key), is_binary(Value) ->
|
|
|
|
[{object, <<?start_object/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(Key, Value, [{object, Object}|Rest]) when is_binary(Key), is_binary(Value) ->
|
|
|
|
[{object, <<Object/binary, ?comma/binary, Key/binary, ?colon/binary, Value/binary>>}] ++ Rest;
|
|
|
|
insert(_, _, _) -> erlang:error(badarg).
|
|
|
|
|
|
|
|
|
2011-10-24 22:51:39 -07:00
|
|
|
%% eunit tests
|
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2012-03-05 19:38:17 -08:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
config_test_() ->
|
2011-10-26 06:33:53 -07:00
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"empty config", ?_assertEqual(#config{}, parse_config([]))},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"unspecified indent/space", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{space=1, indent=1},
|
|
|
|
parse_config([space, indent])
|
2012-03-16 15:34:57 -07:00
|
|
|
)},
|
2013-02-04 23:28:17 -08:00
|
|
|
{"specific indent", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{indent=4},
|
|
|
|
parse_config([{indent, 4}])
|
2012-03-16 15:34:57 -07:00
|
|
|
)},
|
2013-02-04 23:28:17 -08:00
|
|
|
{"specific space", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{space=2},
|
|
|
|
parse_config([{space, 2}])
|
2012-03-16 15:34:57 -07:00
|
|
|
)},
|
2013-02-04 23:28:17 -08:00
|
|
|
{"specific space and indent", ?_assertEqual(
|
2013-02-12 11:54:42 -08:00
|
|
|
#config{space=2, indent=2},
|
|
|
|
parse_config([{space, 2}, {indent, 2}])
|
2012-03-31 12:33:19 -07:00
|
|
|
)},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"invalid opt flag", ?_assertError(badarg, parse_config([error]))},
|
|
|
|
{"invalid opt tuple", ?_assertError(badarg, parse_config([{error, true}]))}
|
2011-10-26 06:33:53 -07:00
|
|
|
].
|
|
|
|
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2013-02-04 23:51:02 -08:00
|
|
|
space_test_() ->
|
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"no space", ?_assertEqual([], space(#config{space=0}))},
|
|
|
|
{"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))},
|
|
|
|
{"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))}
|
2013-02-04 23:51:02 -08:00
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
indent_test_() ->
|
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"no indent", ?_assertEqual([], indent(#config{indent=0, depth=1}))},
|
2013-02-04 23:51:02 -08:00
|
|
|
{"indent 1 depth 1", ?_assertEqual(
|
|
|
|
[[?newline], ?space],
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(#config{indent=1, depth=1})
|
2013-02-04 23:51:02 -08:00
|
|
|
)},
|
|
|
|
{"indent 1 depth 2", ?_assertEqual(
|
|
|
|
[[[?newline], ?space], ?space],
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(#config{indent=1, depth=2})
|
2013-02-04 23:51:02 -08:00
|
|
|
)},
|
|
|
|
{"indent 4 depth 1", ?_assertEqual(
|
|
|
|
[[?newline], <<" ">>],
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(#config{indent=4, depth=1})
|
2013-02-04 23:51:02 -08:00
|
|
|
)},
|
|
|
|
{"indent 4 depth 2", ?_assertEqual(
|
|
|
|
[[[?newline], <<" ">>], <<" ">>],
|
2013-02-12 11:54:42 -08:00
|
|
|
indent(#config{indent=4, depth=2})
|
2013-02-04 23:51:02 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
|
|
|
indent_or_space_test_() ->
|
|
|
|
[
|
|
|
|
{"no indent so space", ?_assertEqual(
|
|
|
|
<<" ">>,
|
2013-02-12 11:54:42 -08:00
|
|
|
indent_or_space(#config{space=1, indent=0, depth=1})
|
2013-02-04 23:51:02 -08:00
|
|
|
)},
|
|
|
|
{"indent so no space", ?_assertEqual(
|
|
|
|
[[?newline], ?space],
|
2013-02-12 11:54:42 -08:00
|
|
|
indent_or_space(#config{space=1, indent=1, depth=1})
|
2013-02-04 23:51:02 -08:00
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-09 15:50:52 -08:00
|
|
|
format_test_() ->
|
|
|
|
[
|
2013-02-12 11:54:42 -08:00
|
|
|
{"0.0", ?_assert(encode(float, 0.0, #config{}) =:= "0.0")},
|
|
|
|
{"1.0", ?_assert(encode(float, 1.0, #config{}) =:= "1.0")},
|
|
|
|
{"-1.0", ?_assert(encode(float, -1.0, #config{}) =:= "-1.0")},
|
2013-02-09 15:50:52 -08:00
|
|
|
{"3.1234567890987654321",
|
|
|
|
?_assert(
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(float, 3.1234567890987654321, #config{}) =:= "3.1234567890987655")
|
2013-02-09 15:50:52 -08:00
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
{"1.0e23", ?_assert(encode(float, 1.0e23, #config{}) =:= "1.0e23")},
|
|
|
|
{"0.3", ?_assert(encode(float, 3.0/10.0, #config{}) =:= "0.3")},
|
|
|
|
{"0.0001", ?_assert(encode(float, 0.0001, #config{}) =:= "0.0001")},
|
|
|
|
{"0.00001", ?_assert(encode(float, 0.00001, #config{}) =:= "1.0e-5")},
|
|
|
|
{"0.00000001", ?_assert(encode(float, 0.00000001, #config{}) =:= "1.0e-8")},
|
|
|
|
{"1.0e-323", ?_assert(encode(float, 1.0e-323, #config{}) =:= "1.0e-323")},
|
|
|
|
{"1.0e308", ?_assert(encode(float, 1.0e308, #config{}) =:= "1.0e308")},
|
2013-02-09 15:50:52 -08:00
|
|
|
{"min normalized float",
|
|
|
|
?_assert(
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(float, math:pow(2, -1022), #config{}) =:= "2.2250738585072014e-308"
|
2013-02-09 15:50:52 -08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
{"max normalized float",
|
|
|
|
?_assert(
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(float, (2 - math:pow(2, -52)) * math:pow(2, 1023), #config{})
|
2013-02-09 15:50:52 -08:00
|
|
|
=:= "1.7976931348623157e308"
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"min denormalized float",
|
2013-02-12 11:54:42 -08:00
|
|
|
?_assert(encode(float, math:pow(2, -1074), #config{}) =:= "5.0e-324")
|
2013-02-09 15:50:52 -08:00
|
|
|
},
|
|
|
|
{"max denormalized float",
|
|
|
|
?_assert(
|
2013-02-12 11:54:42 -08:00
|
|
|
encode(float, (1 - math:pow(2, -52)) * math:pow(2, -1022), #config{})
|
2013-02-09 15:50:52 -08:00
|
|
|
=:= "2.225073858507201e-308"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-10-26 14:45:36 +00:00
|
|
|
rep_manipulation_test_() ->
|
|
|
|
[
|
|
|
|
{"allocate a new object on an empty stack", ?_assertEqual(
|
|
|
|
[{object, <<"{">>}],
|
|
|
|
start_object([])
|
|
|
|
)},
|
|
|
|
{"allocate a new object on a stack", ?_assertEqual(
|
|
|
|
[{object, <<"{">>}, {object, <<"{">>}],
|
|
|
|
start_object([{object, <<"{">>}])
|
|
|
|
)},
|
|
|
|
{"allocate a new array on an empty stack", ?_assertEqual(
|
|
|
|
[{array, <<"[">>}],
|
|
|
|
start_array([])
|
|
|
|
)},
|
|
|
|
{"allocate a new array on a stack", ?_assertEqual(
|
|
|
|
[{array, <<"[">>}, {object, <<"{">>}],
|
|
|
|
start_array([{object, <<"{">>}])
|
|
|
|
)},
|
|
|
|
{"insert a key into an object", ?_assertEqual(
|
|
|
|
[{object, <<"\"key\"">>, <<"{">>}],
|
|
|
|
insert(<<"\"key\"">>, [{object, <<"{">>}])
|
|
|
|
)},
|
|
|
|
{"insert a value into an object", ?_assertEqual(
|
|
|
|
[{object, <<"{\"key\":true">>}],
|
|
|
|
insert(<<"true">>, [{object, <<"\"key\"">>, <<"{">>}])
|
|
|
|
)},
|
|
|
|
{"insert a value into an array", ?_assertEqual(
|
|
|
|
[{array, <<"[true">>}],
|
|
|
|
insert(<<"true">>, [{array, <<"[">>}])
|
|
|
|
)},
|
|
|
|
{"insert a key/value pair into an object", ?_assertEqual(
|
|
|
|
[{object, <<"{\"x\":true,\"y\":false">>}],
|
|
|
|
insert(<<"\"y\"">>, <<"false">>, [{object, <<"{\"x\":true">>}])
|
|
|
|
)},
|
|
|
|
{"finish an object with no ancestor", ?_assertEqual(
|
|
|
|
<<"{\"x\":true,\"y\":false}">>,
|
|
|
|
finish([{object, <<"{\"x\":true,\"y\":false">>}])
|
|
|
|
)},
|
|
|
|
{"finish an empty object", ?_assertEqual(
|
|
|
|
<<"{}">>,
|
|
|
|
finish([{object, <<"{">>}])
|
|
|
|
)},
|
|
|
|
{"finish an object with an ancestor", ?_assertEqual(
|
|
|
|
[{object, <<"{\"a\":[],\"b\":{\"x\":true,\"y\":false}">>}],
|
|
|
|
finish([{object, <<"{\"x\":true,\"y\":false">>}, {object, <<"\"b\"">>, <<"{\"a\":[]">>}])
|
|
|
|
)},
|
|
|
|
{"finish an array with no ancestor", ?_assertEqual(
|
|
|
|
<<"[true,false,null]">>,
|
|
|
|
finish([{array, <<"[true,false,null">>}])
|
|
|
|
)},
|
|
|
|
{"finish an array with an ancestor", ?_assertEqual(
|
|
|
|
[{array, <<"[1,2,3,[true,false,null]">>}],
|
|
|
|
finish([{array, <<"[true,false,null">>}, {array, <<"[1,2,3">>}])
|
|
|
|
)}
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2013-02-10 19:39:42 -08:00
|
|
|
handle_event_test_() ->
|
2013-02-13 19:13:50 -08:00
|
|
|
Data = jsx:test_cases(),
|
2013-02-10 19:39:42 -08:00
|
|
|
[
|
|
|
|
{
|
|
|
|
Title, ?_assertEqual(
|
|
|
|
JSON,
|
2013-10-25 01:35:39 +00:00
|
|
|
lists:foldl(fun handle_event/2, init([]), Events ++ [end_json])
|
2013-02-10 19:39:42 -08:00
|
|
|
)
|
|
|
|
} || {Title, JSON, _, Events} <- Data
|
|
|
|
].
|
|
|
|
|
|
|
|
|
2011-10-24 22:51:39 -07:00
|
|
|
-endif.
|