jsx/src/jsx_format.erl

174 lines
6.2 KiB
Erlang
Raw Normal View History

%% 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.
-module(jsx_format).
-author("alisdairsullivan@yahoo.ca").
-export([format/2]).
2010-08-10 13:07:16 -07:00
-include("./include/jsx_format.hrl").
2010-08-10 13:07:16 -07:00
-ifdef(test).
-include_lib("eunit/include/eunit.hrl").
-endif.
format(JSON, Opts) when is_binary(JSON) ->
P = jsx:parser(extract_parser_opts(Opts)),
format(fun() -> P(JSON) end, Opts);
format(F, OptsList) when is_function(F) ->
Opts = parse_opts(OptsList, #opts{}),
{Continue, String} = format_something(F(), Opts, 0),
case Continue() of
{event, end_json, _} -> encode(String, Opts)
; _ -> {error, badarg}
end.
parse_opts([{indent, Val}|Rest], Opts) ->
parse_opts(Rest, Opts#opts{indent = Val});
parse_opts([indent|Rest], Opts) ->
parse_opts(Rest, Opts#opts{indent = 1});
parse_opts([{space, Val}|Rest], Opts) ->
parse_opts(Rest, Opts#opts{space = Val});
parse_opts([space|Rest], Opts) ->
parse_opts(Rest, Opts#opts{space = 1});
parse_opts([{output_encoding, Val}|Rest], Opts) ->
parse_opts(Rest, Opts#opts{output_encoding = Val});
parse_opts([_|Rest], Opts) ->
parse_opts(Rest, Opts);
parse_opts([], Opts) ->
Opts.
extract_parser_opts(Opts) ->
[ {K, V} || {K, V} <- Opts, lists:member(K, [comments, encoding]) ].
format_something({event, start_object, Next}, Opts, Level) ->
2010-08-10 13:07:16 -07:00
case Next() of
{event, end_object, Continue} ->
{Continue, [?start_object, ?end_object]}
; Event ->
{Continue, Object} = format_object(Event, [], Opts, Level + 1),
{Continue, [?start_object, Object, indent(Opts, Level), ?end_object]}
end;
format_something({event, start_array, Next}, Opts, Level) ->
2010-08-10 13:07:16 -07:00
case Next() of
{event, end_array, Continue} ->
{Continue, [?start_array, ?end_array]}
; Event ->
{Continue, Object} = format_array(Event, [], Opts, Level + 1),
{Continue, [?start_array, Object, indent(Opts, Level), ?end_array]}
end;
format_something({event, {Type, Value}, Next}, _Opts, _Level) ->
{Next, [encode(Type, Value)]}.
format_object({event, end_object, Next}, Acc, _Opts, _Level) ->
{Next, Acc};
format_object({event, {key, Key}, Next}, Acc, Opts, Level) ->
{Continue, Value} = format_something(Next(), Opts, Level),
case Continue() of
{event, end_object, NextNext} ->
{NextNext, [Acc, indent(Opts, Level), encode(string, Key), ?colon, space(Opts), Value]}
; Else ->
format_object(Else,
2010-08-10 13:07:16 -07:00
[Acc, indent(Opts, Level), encode(string, Key), ?colon, space(Opts), Value, ?comma, space(Opts)],
Opts,
Level
)
end.
format_array({event, end_array, Next}, Acc, _Opts, _Level) ->
{Next, Acc};
format_array(Event, Acc, Opts, Level) ->
{Continue, Value} = format_something(Event, Opts, Level),
case Continue() of
{event, end_array, NextNext} ->
{NextNext, [Acc, indent(Opts, Level), Value]}
; Else ->
2010-08-10 13:07:16 -07:00
format_array(Else, [Acc, indent(Opts, Level), Value, ?comma, space(Opts)], Opts, Level)
end.
encode(Acc, Opts) when is_list(Acc) ->
case Opts#opts.output_encoding of
iolist -> Acc
; UTF when ?is_utf_encoding(UTF) -> unicode:characters_to_binary(Acc, utf8, UTF)
; _ -> erlang:throw(badarg)
end;
encode(string, String) ->
[?quote, String, ?quote];
encode(literal, Literal) ->
erlang:atom_to_list(Literal);
encode(_, Number) ->
Number.
indent(Opts, Level) ->
case Opts#opts.indent of
0 -> []
; X when X > 0 ->
Indent = [ ?space || _ <- lists:seq(1, X) ],
indent(Indent, Level, [?newline])
end.
indent(_Indent, 0, Acc) ->
Acc;
indent(Indent, N, Acc) ->
indent(Indent, N - 1, [Acc, Indent]).
space(Opts) ->
case Opts#opts.space of
0 -> []
; X when X > 0 -> [ ?space || _ <- lists:seq(1, X) ]
2010-08-10 13:07:16 -07:00
end.
%% eunit tests
-ifdef(test).
minify_test_() ->
[
{"minify object", ?_assert(format(<<" { \"key\" :\n\t \"value\"\r\r\r\n } ">>, []) =:= <<"{\"key\":\"value\"}">>)},
{"minify array", ?_assert(format(<<" [\n\ttrue,\n\tfalse,\n\tnull\n] ">>, []) =:= <<"[true,false,null]">>)}
].
opts_test_() ->
[
{"unspecified indent/space", ?_assert(format(<<" [\n\ttrue,\n\tfalse,\n\tnull\n] ">>, [space, indent]) =:= <<"[\n true, \n false, \n null\n]">>)},
{"specific indent/space", ?_assert(format(<<"\n{\n\"key\" : [],\n\"another key\" : true\n}\n">>, [{space, 2}, {indent, 4}]) =:= <<"{\n \"key\": [], \n \"another key\": true\n}">>)},
{"nested structures", ?_assert(format(<<"[{\"key\":\"value\", \"another key\": \"another value\"}, [[true, false, null]]]">>, [{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]">>)},
{"just spaces", ?_assert(format(<<"[1,2,3]">>, [{space, 2}]) =:= <<"[1, 2, 3]">>)},
{"just indent", ?_assert(format(<<"[1.0, 2.0, 3.0]">>, [{indent, 2}]) =:= <<"[\n 1.0,\n 2.0,\n 3.0\n]">>)}
].
-endif.