jsx/examples/jsx_prettify.erl

101 lines
3.6 KiB
Erlang
Raw Normal View History

2010-05-25 21:26:55 -07:00
%% The MIT License
2010-05-25 21:48:36 -07:00
%% Copyright (c) 2010 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
2010-05-25 21:26:55 -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.
-module(jsx_prettify).
-author("alisdairsullivan@yahoo.ca").
2010-05-24 23:35:26 -07:00
-export([pretty/2, jsx_event/2]).
2010-05-24 23:35:26 -07:00
-record(opts, {
indent = 4
}).
pretty(JSON, Opts) ->
2010-05-24 23:35:26 -07:00
Init = init(parse_opts(Opts, #opts{})),
P = jsx:decoder({{jsx_prettify, jsx_event}, Init}, []),
2010-05-30 16:39:58 -07:00
case P(JSON) of
{incomplete, _} -> {error, badjson}
; {error, badjson} -> {error, badjson}
; {Result, _} -> Result
end.
2010-05-24 23:35:26 -07:00
2010-05-24 23:35:26 -07:00
parse_opts([{indent, Val}|Rest], Opts) ->
parse_opts(Rest, Opts#opts{indent = Val});
parse_opts([], Opts) ->
Opts.
2010-05-24 23:35:26 -07:00
init(Opts) ->
{[], Opts#opts.indent, 0, new}.
2010-05-24 23:35:26 -07:00
jsx_event(start_object, {Acc, Indent, Level, value}) ->
{Acc ++ ",\n" ++ indent(Indent, Level) ++ "{", Indent, Level + 1, new};
jsx_event(start_object, {Acc, Indent, Level, _}) ->
{Acc ++ "{", Indent, Level + 1, new};
2010-05-24 23:35:26 -07:00
jsx_event(start_array, {Acc, Indent, Level, value}) ->
{Acc ++ ",\n" ++ indent(Indent, Level) ++ "[", Indent, Level + 1, new};
jsx_event(start_array, {Acc, Indent, Level, _}) ->
{Acc ++ "[", Indent, Level + 1, new};
jsx_event(end_object, {Acc, Indent, Level, value}) ->
{Acc ++ "\n" ++ indent(Indent, Level - 1) ++ "}", Indent, Level - 1, value};
jsx_event(end_object, {Acc, Indent, Level, new}) ->
{Acc ++ "}", Indent, Level - 1, value};
jsx_event(end_array, {Acc, Indent, Level, value}) ->
{Acc ++ "\n" ++ indent(Indent, Level - 1) ++ "]", Indent, Level - 1, value};
jsx_event(end_array, {Acc, Indent, Level, new}) ->
{Acc ++ "]", Indent, Level - 1, value};
jsx_event({key, Key}, {Acc, Indent, Level, value}) ->
{Acc ++ ",\n" ++ indent(Indent, Level) ++ "\"" ++ Key ++ "\": ", Indent, Level, key};
jsx_event({key, Key}, {Acc, Indent, Level, _}) ->
{Acc ++ "\n" ++ indent(Indent, Level) ++ "\"" ++ Key ++ "\": ", Indent, Level, key};
jsx_event({Type, Value}, {Acc, Indent, Level, value}) ->
{Acc ++ ",\n" ++ indent(Indent, Level) ++ format(Type, Value), Indent, Level, value};
jsx_event({Type, Value}, {Acc, Indent, Level, new}) ->
{Acc ++ "\n" ++ indent(Indent, Level) ++ format(Type, Value), Indent, Level, value};
jsx_event({Type, Value}, {Acc, Indent, Level, key}) ->
{Acc ++ format(Type, Value), Indent, Level, value};
2010-05-30 15:58:28 -07:00
jsx_event(end_of_stream, {Acc, _, _, _}) ->
2010-05-24 23:35:26 -07:00
Acc.
format(string, String) ->
"\"" ++ String ++ "\"";
format(literal, Literal) ->
2010-06-01 02:36:02 -07:00
erlang:atom_to_list(Literal);
format(_, Number) ->
Number;
2010-05-24 23:35:26 -07:00
indent(Indent, Level) ->
[ 16#20 || _ <- lists:seq(1, Indent * Level) ].