jsx/src/jsx_encoder.erl

174 lines
5.7 KiB
Erlang
Raw Normal View History

%% The MIT License
2013-03-10 20:30:24 -07:00
%% Copyright (c) 2010-2013 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]).
-spec encoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:encoder().
2011-11-29 19:39:14 -08:00
encoder(Handler, State, Config) ->
2012-03-05 19:53:55 -08:00
fun(JSON) ->
start(
JSON,
{Handler, Handler:init(State)},
2013-03-06 01:37:08 -08:00
jsx_config:parse_config(Config)
2012-03-05 19:53:55 -08:00
)
end.
-include("jsx_config.hrl").
-ifndef(error).
2013-03-04 21:21:45 -08:00
-define(error(State, Term, Handler, Config),
case Config#config.error_handler of
false -> erlang:error(badarg);
2013-03-06 01:37:08 -08:00
F -> erlang:throw(F(Term, {encoder, State, Handler}, jsx_config:config_to_list(Config)))
2013-03-04 21:21:45 -08:00
end
).
-endif.
start(Term, {Handler, State}, Config) ->
2013-03-11 01:13:01 -07:00
try Handler:handle_event(end_json, value(Term, {Handler, State}, Config))
2013-03-04 21:21:45 -08:00
catch
throw:Error -> Error;
Type:Value -> erlang:Type(Value)
end.
value(String, {Handler, State}, Config) when is_binary(String) ->
Handler:handle_event({string, clean_string(String, {Handler, State}, Config)}, State);
value(Float, {Handler, State}, _Config) when is_float(Float) ->
2011-11-29 19:39:14 -08:00
Handler:handle_event({float, Float}, State);
value(Int, {Handler, State}, _Config) when is_integer(Int) ->
2011-11-29 19:39:14 -08:00
Handler:handle_event({integer, Int}, State);
value(Literal, {Handler, State}, _Config)
2011-11-29 19:39:14 -08:00
when Literal == true; Literal == false; Literal == null ->
Handler:handle_event({literal, Literal}, State);
value([{}], {Handler, State}, _Config) ->
2011-11-29 19:39:14 -08:00
Handler:handle_event(end_object, Handler:handle_event(start_object, State));
value([], {Handler, State}, _Config) ->
2011-11-29 19:39:14 -08:00
Handler:handle_event(end_array, Handler:handle_event(start_array, State));
value(List, Handler, Config) when is_list(List) ->
list_or_object(List, Handler, Config);
value(Term, Handler, Config) -> ?error(value, Term, Handler, Config).
2011-11-29 19:39:14 -08:00
2013-03-11 01:13:01 -07:00
list_or_object([{K, V}|Rest], {Handler, State}, Config) when is_atom(K); is_binary(K) ->
object([{K, V}|Rest], {Handler, Handler:handle_event(start_object, State)}, Config);
list_or_object(Terms, {Handler, State}, Config) when is_list(Terms) ->
list(Terms, {Handler, Handler:handle_event(start_array, State)}, Config).
object([{Key, Value}, Next|Rest], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
2013-03-11 01:13:01 -07:00
object([Next|Rest], {Handler,
value(
2013-03-11 01:13:01 -07:00
Value,
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
Config
)
},
Config
);
object([{Key, Value}], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
object(
[],
2012-03-15 22:56:21 -07:00
{
Handler,
value(
2013-03-11 01:13:01 -07:00
Value,
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
Config
2012-03-15 22:56:21 -07:00
)
},
Config
2012-03-15 22:56:21 -07:00
);
object([], {Handler, State}, _Config) -> Handler:handle_event(end_object, State);
object(Term, Handler, Config) -> ?error(object, Term, Handler, Config).
list([Value, Next|Rest], {Handler, State}, Config) ->
2013-03-11 01:13:01 -07:00
list([Next|Rest], {Handler, value(Value, {Handler, State}, Config)}, Config);
list([Value], {Handler, State}, Config) ->
2013-02-24 02:57:35 -08:00
list([], {Handler, value(Value, {Handler, State}, Config)}, Config);
2013-03-04 21:21:45 -08:00
list([], {Handler, State}, _Config) -> Handler:handle_event(end_array, State).
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.
clean_string(Bin, Handler, Config) ->
case clean_string(Bin, Config) of
{error, badarg} -> ?error(string, Bin, Handler, Config);
String -> String
end.
-include("jsx_strings.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
2012-06-08 21:44:39 -07:00
encode_test_() ->
2013-02-13 19:13:50 -08:00
Data = jsx:test_cases(),
[
{
Title, ?_assertEqual(
Events ++ [end_json],
start(Term, {jsx, []}, #config{})
)
} || {Title, _, Term, Events} <- Data
].
2012-03-28 23:46:18 -07:00
2013-03-06 01:37:08 -08:00
encode(Term, Config) -> start(Term, {jsx, []}, jsx_config:parse_config(Config)).
2013-02-24 03:02:16 -08:00
2013-03-04 21:21:45 -08:00
error_test_() ->
[
{"value error", ?_assertError(badarg, encode(self(), []))},
{"string error", ?_assertError(badarg, encode(<<239, 191, 191>>, []))}
2013-03-04 21:21:45 -08:00
].
custom_error_handler_test_() ->
Error = fun(Term, {_, State, _}, _) -> {State, Term} end,
[
{"value error", ?_assertEqual(
{value, self()},
encode(self(), [{error_handler, Error}])
)},
{"string error", ?_assertEqual(
{string, <<239, 191, 191>>},
encode(<<239, 191, 191>>, [{error_handler, Error}])
2013-03-04 21:21:45 -08:00
)}
].
-endif.