jsx/src/jsx_format.erl

203 lines
7 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).
-export([format/2]).
-record(opts, {
output_encoding = utf8
}).
-type opts() :: [].
-spec format(Source::(binary() | list()), Opts::opts()) -> binary().
format(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)
).
parse_opts(Opts) -> parse_opts(Opts, #opts{}).
parse_opts([{output_encoding, Val}|Rest], Opts) when Val == utf8 ->
parse_opts(Rest, Opts#opts{output_encoding = Val});
parse_opts([_|Rest], Opts) ->
parse_opts(Rest, Opts);
parse_opts([], Opts) ->
Opts.
extract_opts(Opts) ->
extract_parser_opts(Opts, []).
extract_parser_opts([], Acc) -> Acc;
extract_parser_opts([{K,V}|Rest], Acc) ->
case lists:member(K, [loose_unicode, escape_forward_slash, explicit_end]) of
true -> [{K,V}] ++ Acc
; false -> extract_parser_opts(Rest, Acc)
end;
extract_parser_opts([K|Rest], Acc) ->
case lists:member(K, [loose_unicode, escape_forward_slash, explicit_end]) of
true -> [K] ++ Acc
; false -> extract_parser_opts(Rest, Acc)
end.
-define(start_object, <<"{">>).
-define(start_array, <<"[">>).
-define(end_object, <<"}">>).
-define(end_array, <<"]">>).
-define(colon, <<":">>).
-define(comma, <<",">>).
-define(quote, <<"\"">>).
-define(space, <<" ">>).
-define(newline, <<"\n">>).
fold(Event, {start, Acc, Opts}) ->
case Event of
2011-10-25 20:08:46 -07:00
{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, Opts}) ->
case Event of
{key, Key} ->
{[object_value|Stack], [Acc, encode(string, Key), ?colon], Opts}
; end_object ->
{Stack, [Acc, ?end_object], Opts}
end;
fold(Event, {[object_value|Stack], Acc, Opts}) ->
case Event of
{Type, Value} when Type == string; Type == literal;
Type == integer; Type == float ->
{[key|Stack], [Acc, encode(Type, Value)], 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}) ->
case Event of
{key, Key} ->
{[object_value|Stack], [Acc, ?comma, encode(string, Key), ?colon], Opts}
; end_object ->
{Stack, [Acc, ?end_object], Opts}
end;
fold(Event, {[array_start|Stack], Acc, Opts}) ->
case Event of
{Type, Value} when Type == string; Type == literal;
Type == integer; Type == float ->
{[array|Stack], [Acc, encode(Type, Value)], Opts}
; start_object -> {[object_start, array|Stack], [Acc, ?start_object], Opts}
; start_array -> {[array_start, array|Stack], [Acc, ?start_array], Opts}
; end_array -> {Stack, [Acc, ?end_array], Opts}
end;
fold(Event, {[array|Stack], Acc, Opts}) ->
case Event of
{Type, Value} when Type == string; Type == literal;
Type == integer; Type == float ->
{[array|Stack], [Acc, ?comma, encode(Type, Value)], Opts}
; end_array -> {Stack, [Acc, ?end_array], Opts}
; start_object -> {[object_start, array|Stack], [Acc, ?comma, ?start_object], Opts}
; start_array -> {[array_start, array|Stack], [Acc, ?comma, ?start_array], Opts}
end;
fold(end_json, {[], Acc, Opts}) -> encode(Acc, Opts).
encode(Acc, Opts) when is_list(Acc) ->
case Opts#opts.output_encoding of
iolist -> Acc
; utf8 -> unicode:characters_to_binary(Acc, utf8)
; _ -> erlang:error(badarg)
end;
encode(string, String) ->
[?quote, String, ?quote];
encode(literal, Literal) ->
erlang:atom_to_list(Literal);
encode(integer, Integer) ->
erlang:integer_to_list(Integer);
encode(float, Float) ->
jsx_utils:nice_decimal(Float).
%% eunit tests
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
basic_test_() ->
[
{"empty object", ?_assert(format(<<"{}">>, []) =:= <<"{}">>)},
{"empty array", ?_assert(format(<<"[]">>, []) =:= <<"[]">>)},
{"naked integer", ?_assert(format(<<"123">>, []) =:= <<"123">>)},
{"naked float", ?_assert(format(<<"1.23">>, []) =:= <<"1.23">>)},
2011-10-25 20:08:46 -07:00
{"naked string", ?_assert(format(<<"\"hi\"">>, []) =:= <<"\"hi\"">>)},
{"naked literal", ?_assert(format(<<"true">>, []) =:= <<"true">>)},
{"simple object",
?_assert(format(<<" { \"key\" :\n\t \"value\"\r\r\r\n } ">>,
[]
) =:= <<"{\"key\":\"value\"}">>
)
},
2011-10-25 20:08:46 -07:00
{"really simple object",
?_assert(format(<<"{\"k\":\"v\"}">>, []) =:= <<"{\"k\":\"v\"}">>)
},
{"simple array",
?_assert(format(<<" [\n\ttrue,\n\tfalse , \n \tnull\n] ">>,
[]
) =:= <<"[true,false,null]">>
)
},
2011-10-25 20:08:46 -07:00
{"really simple array", ?_assert(format(<<"[1]">>, []) =:= <<"[1]">>)},
{"nested structures",
?_assert(format(
<<"[{\"key\":\"value\",
\"another key\": \"another value\",
\"a list\": [true, false]
},
[[{}]]
]">>, []
) =:= <<"[{\"key\":\"value\",\"another key\":\"another value\",\"a list\":[true,false]},[[{}]]]">>
)
}
].
terms_test_() ->
[
{"terms",
?_assert(format([start_object,
{key, <<"key">>},
{string, <<"value">>},
end_object,
end_json
], []) =:= <<"{\"key\":\"value\"}">>
)}
].
-endif.