jsx/src/jsx_encoder.erl

160 lines
5.1 KiB
Erlang
Raw Normal View History

%% The MIT License
%% Copyright (c) 2011 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_encoder).
-export([encoder/3, pre_encode/2]).
2012-01-31 20:56:08 -08:00
-spec encoder(Handler::module(), State::any(), Opts::jsx:opts()) -> jsx:encoder().
2011-11-29 19:39:14 -08:00
2012-01-31 20:56:08 -08:00
encoder(Handler, State, Opts) ->
2012-03-05 19:53:55 -08:00
fun(JSON) ->
start(
JSON,
{Handler, Handler:init(State)},
jsx_utils:parse_opts(Opts)
)
end.
-include("jsx_opts.hrl").
-ifndef(error).
-define(error(Args),
erlang:error(badarg, Args)
).
-endif.
2011-11-29 19:39:14 -08:00
start(Term, {Handler, State}, Opts) ->
Handler:handle_event(end_json, value(pre_encode(Term, Opts), {Handler, State}, Opts)).
2012-03-15 22:56:21 -07:00
value(String, {Handler, State}, Opts) when is_binary(String) ->
Handler:handle_event({string, clean_string(String, Opts)}, State);
2011-11-29 19:39:14 -08:00
value(Float, {Handler, State}, _Opts) when is_float(Float) ->
Handler:handle_event({float, Float}, State);
value(Int, {Handler, State}, _Opts) when is_integer(Int) ->
Handler:handle_event({integer, Int}, State);
value(Literal, {Handler, State}, _Opts)
when Literal == true; Literal == false; Literal == null ->
Handler:handle_event({literal, Literal}, State);
value([{}], {Handler, State}, _Opts) ->
Handler:handle_event(end_object, Handler:handle_event(start_object, State));
value([], {Handler, State}, _Opts) ->
Handler:handle_event(end_array, Handler:handle_event(start_array, State));
value([Tuple|_] = List, Handler, Opts) when is_tuple(Tuple) ->
list_or_object(List, Handler, Opts);
2012-06-08 21:40:19 -07:00
value(List, Handler, Opts) when is_list(List) ->
list_or_object(List, Handler, Opts);
2011-11-29 19:39:14 -08:00
value(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
list_or_object([Term|Rest], {Handler, State}, Opts) ->
2013-02-05 16:54:59 -08:00
case pre_encode(Term, Opts) of
{K, V} ->
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Opts)
; T ->
list([T|Rest], {Handler, Handler:handle_event(start_array, State)}, Opts)
end.
object([{Key, Value}, Next|Rest], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) ->
2012-09-03 21:27:00 -07:00
V = pre_encode(Value, Opts),
2013-02-05 16:54:59 -08:00
object(
[pre_encode(Next, Opts)|Rest],
{
Handler,
value(
2013-02-05 16:54:59 -08:00
V,
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)},
Opts
)
},
Opts
);
object([{Key, Value}], {Handler, State}, Opts) when is_atom(Key); is_binary(Key) ->
object(
[],
2012-03-15 22:56:21 -07:00
{
Handler,
value(
pre_encode(Value, Opts),
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), Opts)}, State)},
2012-03-15 22:56:21 -07:00
Opts
)
},
Opts
);
2011-11-29 19:39:14 -08:00
object([], {Handler, State}, _Opts) -> Handler:handle_event(end_object, State);
object(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
list([Value, Next|Rest], {Handler, State}, Opts) ->
list([pre_encode(Next, Opts)|Rest], {Handler, value(Value, {Handler, State}, Opts)}, Opts);
list([Value], {Handler, State}, Opts) ->
list([], {Handler, value(Value, {Handler, State}, Opts)}, Opts);
2011-11-29 19:39:14 -08:00
list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State);
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
pre_encode(Value, #opts{pre_encode=false}) -> io:format("~p~n", [Value]), Value;
pre_encode(Value, Opts) -> (Opts#opts.pre_encode)(Value).
2012-03-15 22:56:21 -07:00
fix_key(Key) when is_atom(Key) -> fix_key(atom_to_binary(Key, utf8));
fix_key(Key) when is_binary(Key) -> Key.
2013-02-05 00:03:58 -08:00
clean_string(Bin, Opts) -> jsx_utils:clean_string(Bin, Opts).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
encode_test_() ->
Data = jsx:empty_array()
++ jsx:deep_array()
++ jsx:really_deep_array()
++ jsx:empty_object()
++ jsx:literals()
++ jsx:naked_literals()
++ jsx:integers()
++ jsx:naked_integers()
++ jsx:floats()
++ jsx:naked_floats(),
[
{
Title, ?_assertEqual(
Events ++ [end_json],
start(Term, {jsx, []}, #opts{})
)
} || {Title, _, Term, Events} <- Data
].
-endif.