2011-10-24 22:51:39 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
|
|
|
%% Copyright (c) 2010 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.
|
|
|
|
|
|
|
|
|
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]).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
|
|
|
-record(opts, {
|
2011-10-26 12:39:28 -07:00
|
|
|
space = 0,
|
|
|
|
indent = 0,
|
|
|
|
depth = 0
|
2011-10-24 22:51:39 -07:00
|
|
|
}).
|
|
|
|
|
2011-11-24 06:33:15 -08:00
|
|
|
-type opts() :: list().
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
-spec to_json(Source::any(), Opts::opts()) -> binary().
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
to_json(Source, Opts) when is_list(Opts) ->
|
2012-03-31 21:58:18 -07:00
|
|
|
(jsx:encoder(?MODULE, Opts, jsx_utils:extract_opts(Opts ++ [escaped_strings])))(Source).
|
2011-11-23 20:54:10 -08:00
|
|
|
|
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
-spec format(Source::binary(), Opts::opts()) -> binary().
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2012-03-04 18:40:00 -08:00
|
|
|
format(Source, Opts) when is_binary(Source) andalso is_list(Opts) ->
|
2012-03-31 21:58:18 -07:00
|
|
|
(jsx:decoder(?MODULE, Opts, jsx_utils:extract_opts(Opts ++ [escaped_strings])))(Source).
|
2012-03-04 18:40:00 -08:00
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
|
2011-10-24 22:51:39 -07:00
|
|
|
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
|
|
|
|
|
2011-10-26 12:39:28 -07:00
|
|
|
parse_opts([{space, Val}|Rest], Opts) when is_integer(Val), Val > 0 ->
|
2011-10-26 06:33:53 -07:00
|
|
|
parse_opts(Rest, Opts#opts{space = Val});
|
|
|
|
parse_opts([space|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{space = 1});
|
2011-10-26 12:39:28 -07:00
|
|
|
parse_opts([{indent, Val}|Rest], Opts) when is_integer(Val), Val > 0 ->
|
|
|
|
parse_opts(Rest, Opts#opts{indent = Val});
|
|
|
|
parse_opts([indent|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts#opts{indent = 1});
|
2011-10-24 22:51:39 -07:00
|
|
|
parse_opts([_|Rest], Opts) ->
|
|
|
|
parse_opts(Rest, Opts);
|
|
|
|
parse_opts([], Opts) ->
|
|
|
|
Opts.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-define(start_object, <<"{">>).
|
|
|
|
-define(start_array, <<"[">>).
|
|
|
|
-define(end_object, <<"}">>).
|
|
|
|
-define(end_array, <<"]">>).
|
|
|
|
-define(colon, <<":">>).
|
|
|
|
-define(comma, <<",">>).
|
|
|
|
-define(quote, <<"\"">>).
|
|
|
|
-define(space, <<" ">>).
|
|
|
|
-define(newline, <<"\n">>).
|
|
|
|
|
|
|
|
|
2011-11-29 19:39:01 -08:00
|
|
|
|
|
|
|
init(Opts) -> {start, [], parse_opts(Opts)}.
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {start, Acc, Opts}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
2011-12-01 22:49:20 -08:00
|
|
|
{Type, Value} -> {[], [Acc, encode(Type, Value, Opts)], Opts}
|
2011-10-25 20:08:46 -07:00
|
|
|
; start_object -> {[object_start], [Acc, ?start_object], Opts}
|
2011-10-24 22:51:39 -07:00
|
|
|
; start_array -> {[array_start], [Acc, ?start_array], Opts}
|
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {[object_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
2011-10-26 12:39:28 -07:00
|
|
|
Opts = OldOpts#opts{depth = Depth + 1},
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{key, Key} ->
|
2011-12-01 22:49:20 -08:00
|
|
|
{[object_value|Stack], [Acc, indent(Opts), encode(string, Key, Opts), ?colon, space(Opts)], Opts}
|
2011-10-24 22:51:39 -07:00
|
|
|
; end_object ->
|
2011-10-26 12:39:28 -07:00
|
|
|
{Stack, [Acc, ?end_object], OldOpts}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {[object_value|Stack], Acc, Opts}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{Type, Value} when Type == string; Type == literal;
|
|
|
|
Type == integer; Type == float ->
|
2011-12-01 22:49:20 -08:00
|
|
|
{[key|Stack], [Acc, encode(Type, Value, Opts)], Opts}
|
2011-10-24 22:51:39 -07:00
|
|
|
; start_object -> {[object_start, key|Stack], [Acc, ?start_object], Opts}
|
|
|
|
; start_array -> {[array_start, key|Stack], [Acc, ?start_array], Opts}
|
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {[key|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{key, Key} ->
|
2011-12-01 22:49:20 -08:00
|
|
|
{[object_value|Stack], [Acc, ?comma, indent_or_space(Opts), encode(string, Key, Opts), ?colon, space(Opts)], Opts}
|
2011-10-24 22:51:39 -07:00
|
|
|
; end_object ->
|
2011-10-26 12:39:28 -07:00
|
|
|
NewOpts = Opts#opts{depth = Depth - 1},
|
|
|
|
{Stack, [Acc, indent(NewOpts), ?end_object], NewOpts}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {[array_start|Stack], Acc, OldOpts = #opts{depth = Depth}}) ->
|
2011-10-26 12:39:28 -07:00
|
|
|
Opts = OldOpts#opts{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 ->
|
2011-12-01 22:49:20 -08:00
|
|
|
{[array|Stack], [Acc, indent(Opts), encode(Type, Value, Opts)], Opts}
|
2011-10-26 12:39:28 -07:00
|
|
|
; start_object -> {[object_start, array|Stack], [Acc, indent(Opts), ?start_object], Opts}
|
|
|
|
; start_array -> {[array_start, array|Stack], [Acc, indent(Opts), ?start_array], Opts}
|
|
|
|
; end_array -> {Stack, [Acc, ?end_array], OldOpts}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(Event, {[array|Stack], Acc, Opts = #opts{depth = Depth}}) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
case Event of
|
|
|
|
{Type, Value} when Type == string; Type == literal;
|
|
|
|
Type == integer; Type == float ->
|
2011-12-01 22:49:20 -08:00
|
|
|
{[array|Stack], [Acc, ?comma, indent_or_space(Opts), encode(Type, Value, Opts)], Opts}
|
2011-10-26 12:39:28 -07:00
|
|
|
; end_array ->
|
|
|
|
NewOpts = Opts#opts{depth = Depth - 1},
|
|
|
|
{Stack, [Acc, indent(NewOpts), ?end_array], NewOpts}
|
|
|
|
; 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}
|
2011-10-24 22:51:39 -07:00
|
|
|
end;
|
2011-11-23 20:54:10 -08:00
|
|
|
handle_event(end_json, {[], Acc, _Opts}) -> unicode:characters_to_binary(Acc, utf8).
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2012-03-24 19:42:00 -07:00
|
|
|
encode(string, String, _Opts) ->
|
|
|
|
[?quote, String, ?quote];
|
2011-12-01 22:49:20 -08:00
|
|
|
encode(literal, Literal, _Opts) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
erlang:atom_to_list(Literal);
|
2011-12-01 22:49:20 -08:00
|
|
|
encode(integer, Integer, _Opts) ->
|
2011-10-24 22:51:39 -07:00
|
|
|
erlang:integer_to_list(Integer);
|
2011-12-01 22:49:20 -08:00
|
|
|
encode(float, Float, _Opts) ->
|
2012-03-31 12:34:29 -07:00
|
|
|
[Output] = io_lib:format("~p", [Float]), Output.
|
2011-10-24 22:51:39 -07:00
|
|
|
|
|
|
|
|
2011-10-26 06:33:53 -07:00
|
|
|
space(Opts) ->
|
|
|
|
case Opts#opts.space of
|
|
|
|
0 -> []
|
|
|
|
; X when X > 0 -> [ ?space || _ <- lists:seq(1, X) ]
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2011-10-26 12:39:28 -07:00
|
|
|
indent(Opts) ->
|
|
|
|
case Opts#opts.indent of
|
|
|
|
0 -> []
|
|
|
|
; X when X > 0 ->
|
|
|
|
Indent = [ ?space || _ <- lists:seq(1, X) ],
|
|
|
|
indent(Indent, Opts#opts.depth, [?newline])
|
|
|
|
end.
|
|
|
|
|
|
|
|
indent(_Indent, 0, Acc) -> Acc;
|
|
|
|
indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]).
|
|
|
|
|
|
|
|
|
|
|
|
indent_or_space(Opts) ->
|
|
|
|
case Opts#opts.indent > 0 of
|
|
|
|
true -> indent(Opts)
|
|
|
|
; false -> space(Opts)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
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
|
|
|
basic_format_test_() ->
|
2011-10-24 22:51:39 -07:00
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"empty object", ?_assertEqual(format(<<"{}">>, []), <<"{}">>)},
|
|
|
|
{"empty array", ?_assertEqual(format(<<"[]">>, []), <<"[]">>)},
|
|
|
|
{"naked integer", ?_assertEqual(format(<<"123">>, []), <<"123">>)},
|
2012-03-31 12:33:19 -07:00
|
|
|
{"naked float", ?_assertEqual(format(<<"1.23">>, []), <<"1.23">>)},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked string", ?_assertEqual(format(<<"\"hi\"">>, []), <<"\"hi\"">>)},
|
2012-03-29 21:43:55 -07:00
|
|
|
{"naked string with control character", ?_assertEqual(
|
2012-03-29 22:13:01 -07:00
|
|
|
format(<<"\"hi\\n\"">>, []), <<"\"hi\\n\"">>
|
2012-03-29 21:43:55 -07:00
|
|
|
)},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked literal", ?_assertEqual(format(<<"true">>, []), <<"true">>)},
|
|
|
|
{"simple object", ?_assertEqual(
|
|
|
|
format(<<" { \"key\" :\n\t \"value\"\r\r\r\n } ">>, []),
|
|
|
|
<<"{\"key\":\"value\"}">>
|
|
|
|
)},
|
|
|
|
{"really simple object", ?_assertEqual(format(<<"{\"k\":\"v\"}">>, []) , <<"{\"k\":\"v\"}">>)},
|
|
|
|
{"nested object", ?_assertEqual(
|
|
|
|
format(<<"{\"k\":{\"k\":\"v\"}, \"j\":{}}">>, []),
|
|
|
|
<<"{\"k\":{\"k\":\"v\"},\"j\":{}}">>
|
|
|
|
)},
|
|
|
|
{"simple array", ?_assertEqual(
|
|
|
|
format(<<" [\n\ttrue,\n\tfalse , \n \tnull\n] ">>, []),
|
|
|
|
<<"[true,false,null]">>
|
|
|
|
)},
|
|
|
|
{"really simple array", ?_assertEqual(format(<<"[1]">>, []), <<"[1]">>)},
|
|
|
|
{"nested array", ?_assertEqual(format(<<"[[[]]]">>, []), <<"[[[]]]">>)},
|
|
|
|
{"nested structures", ?_assertEqual(
|
|
|
|
format(<<"[
|
|
|
|
{
|
|
|
|
\"key\":\"value\",
|
|
|
|
\"another key\": \"another value\",
|
|
|
|
\"a list\": [true, false]
|
|
|
|
},
|
|
|
|
[[{}]]
|
2012-11-21 23:04:14 -08:00
|
|
|
]">>, []),
|
2012-03-16 15:34:57 -07:00
|
|
|
<<"[{\"key\":\"value\",\"another key\":\"another value\",\"a list\":[true,false]},[[{}]]]">>
|
|
|
|
)},
|
2011-10-27 06:35:48 -07:00
|
|
|
{"simple nested structure",
|
2012-03-16 15:34:57 -07:00
|
|
|
?_assertEqual(
|
|
|
|
format(<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>, []),
|
|
|
|
<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>
|
2011-10-27 06:35:48 -07:00
|
|
|
)
|
2011-10-24 22:51:39 -07:00
|
|
|
}
|
|
|
|
].
|
|
|
|
|
2012-03-05 19:38:17 -08:00
|
|
|
basic_to_json_test_() ->
|
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"empty object", ?_assertEqual(to_json([{}], []), <<"{}">>)},
|
|
|
|
{"empty array", ?_assertEqual(to_json([], []), <<"[]">>)},
|
|
|
|
{"naked integer", ?_assertEqual(to_json(123, []), <<"123">>)},
|
2012-03-31 12:33:19 -07:00
|
|
|
{"naked float", ?_assertEqual(to_json(1.23, []) , <<"1.23">>)},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked string", ?_assertEqual(to_json(<<"hi">>, []), <<"\"hi\"">>)},
|
2012-03-29 21:43:55 -07:00
|
|
|
{"naked string with control character", ?_assertEqual(
|
2012-03-29 22:13:01 -07:00
|
|
|
to_json(<<"hi\n">>, []), <<"\"hi\\n\"">>
|
2012-03-29 21:43:55 -07:00
|
|
|
)},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked literal", ?_assertEqual(to_json(true, []), <<"true">>)},
|
|
|
|
{"simple object", ?_assertEqual(
|
|
|
|
to_json(
|
|
|
|
[{<<"key">>, <<"value">>}],
|
|
|
|
[]
|
2012-11-21 23:04:14 -08:00
|
|
|
),
|
2012-03-16 15:34:57 -07:00
|
|
|
<<"{\"key\":\"value\"}">>
|
|
|
|
)},
|
|
|
|
{"nested object", ?_assertEqual(
|
|
|
|
to_json(
|
|
|
|
[{<<"k">>,[{<<"k">>,<<"v">>}]},{<<"j">>,[{}]}],
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
<<"{\"k\":{\"k\":\"v\"},\"j\":{}}">>
|
|
|
|
)},
|
|
|
|
{"simple array", ?_assertEqual(to_json([true, false, null], []), <<"[true,false,null]">>)},
|
|
|
|
{"really simple array", ?_assertEqual(to_json([1], []), <<"[1]">>)},
|
|
|
|
{"nested array", ?_assertEqual(to_json([[[]]], []), <<"[[[]]]">>)},
|
|
|
|
{"nested structures", ?_assertEqual(
|
|
|
|
to_json(
|
|
|
|
[
|
2012-03-05 19:38:17 -08:00
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{<<"key">>, <<"value">>},
|
|
|
|
{<<"another key">>, <<"another value">>},
|
|
|
|
{<<"a list">>, [true, false]}
|
2012-03-05 19:38:17 -08:00
|
|
|
],
|
2012-03-16 15:34:57 -07:00
|
|
|
[[[{}]]]
|
|
|
|
],
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
<<"[{\"key\":\"value\",\"another key\":\"another value\",\"a list\":[true,false]},[[{}]]]">>
|
|
|
|
)},
|
|
|
|
{"simple nested structure", ?_assertEqual(
|
|
|
|
to_json(
|
|
|
|
[[], [{<<"k">>, [[], [{}]]}, {<<"j">>, [{}]}], []],
|
|
|
|
[]
|
|
|
|
),
|
|
|
|
<<"[[],{\"k\":[[],{}],\"j\":{}},[]]">>
|
|
|
|
)}
|
2012-03-05 19:38:17 -08:00
|
|
|
].
|
|
|
|
|
2011-10-26 06:33:53 -07:00
|
|
|
opts_test_() ->
|
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"unspecified indent/space", ?_assertEqual(
|
|
|
|
format(<<" [\n\ttrue,\n\tfalse,\n\tnull\n] ">>, [space, indent]),
|
|
|
|
<<"[\n true,\n false,\n null\n]">>
|
|
|
|
)},
|
|
|
|
{"specific indent/space", ?_assertEqual(
|
|
|
|
format(
|
2012-11-21 23:04:14 -08:00
|
|
|
<<"\n{\n\"key\" : [],\n\"another key\" : true\n}\n">>,
|
2012-03-16 15:34:57 -07:00
|
|
|
[{space, 2}, {indent, 3}]
|
|
|
|
),
|
|
|
|
<<"{\n \"key\": [],\n \"another key\": true\n}">>
|
|
|
|
)},
|
|
|
|
{"nested structures", ?_assertEqual(
|
|
|
|
format(
|
2012-11-21 23:04:14 -08:00
|
|
|
<<"[{\"key\":\"value\", \"another key\": \"another value\"}, [[true, false, null]]]">>,
|
2012-03-16 15:34:57 -07:00
|
|
|
[{space, 2}, {indent, 2}]
|
|
|
|
),
|
|
|
|
<<"[\n {\n \"key\": \"value\",\n \"another key\": \"another value\"\n },\n [\n [\n true,\n false,\n null\n ]\n ]\n]">>
|
|
|
|
)},
|
|
|
|
{"array spaces", ?_assertEqual(
|
|
|
|
format(<<"[1,2,3]">>, [{space, 2}]),
|
|
|
|
<<"[1, 2, 3]">>
|
|
|
|
)},
|
|
|
|
{"object spaces", ?_assertEqual(
|
|
|
|
format(<<"{\"a\":true,\"b\":true,\"c\":true}">>, [{space, 2}]),
|
|
|
|
<<"{\"a\": true, \"b\": true, \"c\": true}">>
|
|
|
|
)},
|
2012-03-31 12:33:19 -07:00
|
|
|
{"array indent", ?_assertEqual(
|
|
|
|
format(<<"[1.23, 1.23, 1.23]">>, [{indent, 2}]),
|
|
|
|
<<"[\n 1.23,\n 1.23,\n 1.23\n]">>
|
|
|
|
)},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"object indent", ?_assertEqual(
|
|
|
|
format(<<"{\"a\":true,\"b\":true,\"c\":true}">>, [{indent, 2}]),
|
|
|
|
<<"{\n \"a\":true,\n \"b\":true,\n \"c\":true\n}">>
|
|
|
|
)}
|
2011-10-26 06:33:53 -07:00
|
|
|
].
|
|
|
|
|
2012-11-21 23:04:14 -08:00
|
|
|
|
2011-10-24 22:51:39 -07:00
|
|
|
-endif.
|