2011-10-21 18:16:16 -07:00
|
|
|
%% The MIT License
|
|
|
|
|
2013-03-10 20:30:24 -07:00
|
|
|
%% Copyright (c) 2010-2013 Alisdair Sullivan <alisdairsullivan@yahoo.ca>
|
2011-10-21 18:16:16 -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_encoder).
|
|
|
|
|
2012-03-26 18:52:32 -07:00
|
|
|
-export([encoder/3]).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-spec encoder(Handler::module(), State::any(), Config::jsx:config()) -> jsx:encoder().
|
2011-11-29 19:39:14 -08:00
|
|
|
|
2013-02-12 11:54:42 -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.
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
-include("jsx_config.hrl").
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
-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
|
2011-10-21 18:16:16 -07:00
|
|
|
).
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
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.
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
value(String, {Handler, State}, Config) when is_binary(String) ->
|
2013-03-04 21:51:43 -08:00
|
|
|
Handler:handle_event({string, clean_string(String, {Handler, State}, Config)}, State);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(Float, {Handler, State}, _Config) when is_float(Float) ->
|
2011-11-29 19:39:14 -08:00
|
|
|
Handler:handle_event({float, Float}, State);
|
2013-02-12 11:54:42 -08:00
|
|
|
value(Int, {Handler, State}, _Config) when is_integer(Int) ->
|
2011-11-29 19:39:14 -08:00
|
|
|
Handler:handle_event({integer, Int}, State);
|
2013-02-12 11:54:42 -08:00
|
|
|
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);
|
2013-02-12 11:54:42 -08:00
|
|
|
value([{}], {Handler, State}, _Config) ->
|
2011-11-29 19:39:14 -08:00
|
|
|
Handler:handle_event(end_object, Handler:handle_event(start_object, State));
|
2013-02-12 11:54:42 -08:00
|
|
|
value([], {Handler, State}, _Config) ->
|
2011-11-29 19:39:14 -08:00
|
|
|
Handler:handle_event(end_array, Handler:handle_event(start_array, State));
|
2013-02-12 11:54:42 -08:00
|
|
|
value(List, Handler, Config) when is_list(List) ->
|
|
|
|
list_or_object(List, Handler, Config);
|
2013-03-04 21:51:43 -08:00
|
|
|
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).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
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,
|
2012-09-03 20:06:50 -07:00
|
|
|
value(
|
2013-03-11 01:13:01 -07:00
|
|
|
Value,
|
2013-03-04 21:51:43 -08:00
|
|
|
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
2013-02-12 11:54:42 -08:00
|
|
|
Config
|
2012-09-03 20:06:50 -07:00
|
|
|
)
|
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
Config
|
2012-09-03 20:06:50 -07:00
|
|
|
);
|
2013-02-12 11:54:42 -08:00
|
|
|
object([{Key, Value}], {Handler, State}, Config) when is_atom(Key); is_binary(Key) ->
|
2012-09-03 20:06:50 -07:00
|
|
|
object(
|
|
|
|
[],
|
2012-03-15 22:56:21 -07:00
|
|
|
{
|
|
|
|
Handler,
|
|
|
|
value(
|
2013-03-11 01:13:01 -07:00
|
|
|
Value,
|
2013-03-04 21:51:43 -08:00
|
|
|
{Handler, Handler:handle_event({key, clean_string(fix_key(Key), {Handler, State}, Config)}, State)},
|
2013-02-12 11:54:42 -08:00
|
|
|
Config
|
2012-03-15 22:56:21 -07:00
|
|
|
)
|
|
|
|
},
|
2013-02-12 11:54:42 -08:00
|
|
|
Config
|
2012-03-15 22:56:21 -07:00
|
|
|
);
|
2013-06-24 11:32:03 -06:00
|
|
|
object([], {Handler, State}, _Config) -> Handler:handle_event(end_object, State);
|
|
|
|
object(Term, Handler, Config) -> ?error(object, Term, Handler, Config).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2013-02-12 11:54:42 -08:00
|
|
|
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);
|
2013-02-12 11:54:42 -08:00
|
|
|
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).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
2012-04-04 20:04:17 -07:00
|
|
|
|
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-03-04 21:51:43 -08:00
|
|
|
clean_string(Bin, Handler, Config) ->
|
2013-03-06 01:28:39 -08:00
|
|
|
case clean_string(Bin, Config) of
|
|
|
|
{error, badarg} -> ?error(string, Bin, Handler, Config);
|
|
|
|
String -> String
|
2013-03-04 21:51:43 -08:00
|
|
|
end.
|
|
|
|
|
2012-03-28 21:51:21 -07:00
|
|
|
|
2012-03-24 19:42:00 -07:00
|
|
|
|
2013-03-06 01:28:39 -08:00
|
|
|
-include("jsx_strings.hrl").
|
|
|
|
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2012-03-28 23:31:07 -07:00
|
|
|
|
2012-06-08 21:44:39 -07:00
|
|
|
encode_test_() ->
|
2013-02-13 19:13:50 -08:00
|
|
|
Data = jsx:test_cases(),
|
2011-10-21 18:16:16 -07:00
|
|
|
[
|
2013-02-11 18:22:45 -08:00
|
|
|
{
|
|
|
|
Title, ?_assertEqual(
|
|
|
|
Events ++ [end_json],
|
2013-02-12 11:54:42 -08:00
|
|
|
start(Term, {jsx, []}, #config{})
|
2012-03-16 15:34:57 -07:00
|
|
|
)
|
2013-02-11 18:22:45 -08:00
|
|
|
} || {Title, _, Term, Events} <- Data
|
2012-03-20 22:42:58 -07:00
|
|
|
].
|
|
|
|
|
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
|
|
|
|
2012-04-04 20:04:17 -07:00
|
|
|
|
2013-03-04 21:21:45 -08:00
|
|
|
error_test_() ->
|
|
|
|
[
|
2013-03-04 21:51:43 -08:00
|
|
|
{"value error", ?_assertError(badarg, encode(self(), []))},
|
2013-03-05 16:36:49 -08:00
|
|
|
{"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}])
|
2013-03-04 21:51:43 -08:00
|
|
|
)},
|
|
|
|
{"string error", ?_assertEqual(
|
2013-03-05 16:36:49 -08:00
|
|
|
{string, <<239, 191, 191>>},
|
|
|
|
encode(<<239, 191, 191>>, [{error_handler, Error}])
|
2013-03-04 21:21:45 -08:00
|
|
|
)}
|
|
|
|
].
|
2012-04-04 20:04:17 -07:00
|
|
|
|
2013-06-24 11:32:03 -06:00
|
|
|
-endif.
|