2011-10-21 18:16:16 -07:00
|
|
|
%% 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).
|
|
|
|
|
2011-11-29 19:39:14 -08:00
|
|
|
-export([encoder/3]).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
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.
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-06 19:59:49 -08:00
|
|
|
-include("jsx_opts.hrl").
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
|
|
|
-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(Term, {Handler, State}, Opts)).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2012-03-15 22:56:21 -07:00
|
|
|
value(String, {Handler, State}, Opts) when is_binary(String) ->
|
2012-03-20 19:36:27 -07:00
|
|
|
Handler:handle_event({string, check_string(String, {Handler, State}, 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(List, {Handler, State}, Opts) when is_list(List) ->
|
|
|
|
list_or_object(List, {Handler, State}, Opts);
|
|
|
|
value(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
|
|
|
|
|
|
|
|
|
|
|
list_or_object([Tuple|_] = List, {Handler, State}, Opts) when is_tuple(Tuple) ->
|
|
|
|
object(List, {Handler, Handler:handle_event(start_object, State)}, Opts);
|
|
|
|
list_or_object(List, {Handler, State}, Opts) ->
|
|
|
|
list(List, {Handler, Handler:handle_event(start_array, State)}, Opts).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2011-11-29 19:39:14 -08:00
|
|
|
object([{Key, Value}|Rest], {Handler, State}, Opts) ->
|
2012-03-15 22:56:21 -07:00
|
|
|
object(
|
|
|
|
Rest,
|
|
|
|
{
|
|
|
|
Handler,
|
|
|
|
value(
|
|
|
|
Value,
|
2012-03-20 19:36:27 -07:00
|
|
|
{Handler, Handler:handle_event({key, check_string(fix_key(Key), {Handler, State}, 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]).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
|
2011-11-29 19:39:14 -08:00
|
|
|
list([Value|Rest], {Handler, State}, Opts) ->
|
|
|
|
list(Rest, {Handler, value(Value, {Handler, State}, Opts)}, Opts);
|
|
|
|
list([], {Handler, State}, _Opts) -> Handler:handle_event(end_array, State);
|
|
|
|
list(Term, Handler, Opts) -> ?error([Term, Handler, Opts]).
|
2011-10-21 18:16:16 -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.
|
|
|
|
|
|
|
|
|
2012-03-20 19:36:27 -07:00
|
|
|
check_string(String, Handler, Opts) ->
|
|
|
|
case check_string(String) of
|
|
|
|
true -> String;
|
|
|
|
false ->
|
|
|
|
case Opts#opts.loose_unicode of
|
|
|
|
true -> clean_string(String, <<>>);
|
|
|
|
false -> erlang:error(badarg, [String, Handler, Opts])
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
check_string(<<C/utf8, Rest/binary>>) when C < 16#fdd0 ->
|
|
|
|
check_string(Rest);
|
|
|
|
check_string(<<C/utf8, Rest/binary>>) when C > 16#fdef, C < 16#fffe ->
|
|
|
|
check_string(Rest);
|
|
|
|
check_string(<<C/utf8, Rest/binary>>)
|
|
|
|
when C =/= 16#fffe andalso C =/= 16#ffff andalso
|
|
|
|
C =/= 16#1fffe andalso C =/= 16#1ffff andalso
|
|
|
|
C =/= 16#2fffe andalso C =/= 16#2ffff andalso
|
|
|
|
C =/= 16#3fffe andalso C =/= 16#3ffff andalso
|
|
|
|
C =/= 16#4fffe andalso C =/= 16#4ffff andalso
|
|
|
|
C =/= 16#5fffe andalso C =/= 16#5ffff andalso
|
|
|
|
C =/= 16#6fffe andalso C =/= 16#6ffff andalso
|
|
|
|
C =/= 16#7fffe andalso C =/= 16#7ffff andalso
|
|
|
|
C =/= 16#8fffe andalso C =/= 16#8ffff andalso
|
|
|
|
C =/= 16#9fffe andalso C =/= 16#9ffff andalso
|
|
|
|
C =/= 16#afffe andalso C =/= 16#affff andalso
|
|
|
|
C =/= 16#bfffe andalso C =/= 16#bffff andalso
|
|
|
|
C =/= 16#cfffe andalso C =/= 16#cffff andalso
|
|
|
|
C =/= 16#dfffe andalso C =/= 16#dffff andalso
|
|
|
|
C =/= 16#efffe andalso C =/= 16#effff andalso
|
|
|
|
C =/= 16#ffffe andalso C =/= 16#fffff andalso
|
|
|
|
C =/= 16#10fffe andalso C =/= 16#10ffff ->
|
|
|
|
check_string(Rest);
|
|
|
|
check_string(<<>>) -> true;
|
|
|
|
check_string(<<_, _/binary>>) -> false.
|
|
|
|
|
|
|
|
clean_string(<<C/utf8, Rest/binary>>, Acc) when C >= 16#fdd0, C =< 16#fdef ->
|
|
|
|
io:format("1: ~p~n", [C]),
|
|
|
|
clean_string(Rest, <<Acc/binary, 16#fffd/utf8>>);
|
|
|
|
clean_string(<<C/utf8, Rest/binary>>, Acc)
|
|
|
|
when C == 16#fffe orelse C == 16#ffff orelse
|
|
|
|
C == 16#1fffe orelse C == 16#1ffff orelse
|
|
|
|
C == 16#2fffe orelse C == 16#2ffff orelse
|
|
|
|
C == 16#3fffe orelse C == 16#3ffff orelse
|
|
|
|
C == 16#4fffe orelse C == 16#4ffff orelse
|
|
|
|
C == 16#5fffe orelse C == 16#5ffff orelse
|
|
|
|
C == 16#6fffe orelse C == 16#6ffff orelse
|
|
|
|
C == 16#7fffe orelse C == 16#7ffff orelse
|
|
|
|
C == 16#8fffe orelse C == 16#8ffff orelse
|
|
|
|
C == 16#9fffe orelse C == 16#9ffff orelse
|
|
|
|
C == 16#afffe orelse C == 16#affff orelse
|
|
|
|
C == 16#bfffe orelse C == 16#bffff orelse
|
|
|
|
C == 16#cfffe orelse C == 16#cffff orelse
|
|
|
|
C == 16#dfffe orelse C == 16#dffff orelse
|
|
|
|
C == 16#efffe orelse C == 16#effff orelse
|
|
|
|
C == 16#ffffe orelse C == 16#fffff orelse
|
|
|
|
C == 16#10fffe orelse C == 16#10ffff ->
|
|
|
|
io:format("2: ~p~n", [C]),
|
|
|
|
clean_string(Rest, <<Acc/binary, 16#fffd/utf8>>);
|
|
|
|
clean_string(<<C/utf8, Rest/binary>>, Acc) ->
|
|
|
|
io:format("3: ~p~n", [C]),
|
|
|
|
clean_string(Rest, <<Acc/binary, C/utf8>>);
|
|
|
|
clean_string(<<>>, Acc) -> Acc.
|
|
|
|
|
2011-12-01 22:12:50 -08:00
|
|
|
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
-ifdef(TEST).
|
|
|
|
-include_lib("eunit/include/eunit.hrl").
|
|
|
|
|
2011-11-29 19:39:14 -08:00
|
|
|
encode(Term) -> (encoder(jsx, [], []))(Term).
|
2011-10-21 18:16:16 -07:00
|
|
|
|
2012-03-20 19:36:27 -07:00
|
|
|
encode(Term, Opts) -> (encoder(jsx, [], Opts))(Term).
|
|
|
|
|
2011-10-21 18:16:16 -07:00
|
|
|
|
|
|
|
encode_test_() ->
|
|
|
|
[
|
2012-03-16 15:34:57 -07:00
|
|
|
{"naked string", ?_assertEqual(encode(<<"a string">>), [{string, <<"a string">>}, end_json])},
|
|
|
|
{"naked integer", ?_assertEqual(encode(123), [{integer, 123}, end_json])},
|
|
|
|
{"naked float", ?_assertEqual(encode(1.23), [{float, 1.23}, end_json])},
|
|
|
|
{"naked literal", ?_assertEqual(encode(null), [{literal, null}, end_json])},
|
|
|
|
{"empty object", ?_assertEqual(encode([{}]), [start_object, end_object, end_json])},
|
|
|
|
{"empty list", ?_assertEqual(encode([]), [start_array, end_array, end_json])},
|
|
|
|
{"simple list", ?_assertEqual(
|
|
|
|
encode([1,2,3,true,false]),
|
|
|
|
[
|
|
|
|
start_array,
|
2011-11-29 19:39:14 -08:00
|
|
|
{integer, 1},
|
|
|
|
{integer, 2},
|
|
|
|
{integer, 3},
|
|
|
|
{literal, true},
|
|
|
|
{literal, false},
|
|
|
|
end_array,
|
2012-03-16 15:34:57 -07:00
|
|
|
end_json
|
|
|
|
]
|
|
|
|
)
|
2011-11-29 19:39:14 -08:00
|
|
|
},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"simple object", ?_assertEqual(
|
|
|
|
encode([{<<"a">>, true}, {<<"b">>, false}]),
|
|
|
|
[
|
|
|
|
start_object,
|
2011-11-29 19:39:14 -08:00
|
|
|
{key, <<"a">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"b">>},
|
|
|
|
{literal, false},
|
|
|
|
end_object,
|
2012-03-16 15:34:57 -07:00
|
|
|
end_json
|
|
|
|
]
|
|
|
|
)
|
2011-11-29 19:39:14 -08:00
|
|
|
},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"complex term", ?_assertEqual(
|
|
|
|
encode([
|
|
|
|
{<<"a">>, true},
|
|
|
|
{<<"b">>, false},
|
|
|
|
{<<"c">>, [1,2,3]},
|
|
|
|
{<<"d">>, [{<<"key">>, <<"value">>}]}
|
|
|
|
]),
|
|
|
|
[
|
|
|
|
start_object,
|
2011-11-29 19:39:14 -08:00
|
|
|
{key, <<"a">>},
|
|
|
|
{literal, true},
|
|
|
|
{key, <<"b">>},
|
|
|
|
{literal, false},
|
|
|
|
{key, <<"c">>},
|
|
|
|
start_array,
|
|
|
|
{integer, 1},
|
|
|
|
{integer, 2},
|
|
|
|
{integer, 3},
|
|
|
|
end_array,
|
|
|
|
{key, <<"d">>},
|
|
|
|
start_object,
|
|
|
|
{key, <<"key">>},
|
|
|
|
{string, <<"value">>},
|
|
|
|
end_object,
|
|
|
|
end_object,
|
2012-03-16 15:34:57 -07:00
|
|
|
end_json
|
|
|
|
]
|
|
|
|
)
|
2011-12-01 22:12:50 -08:00
|
|
|
},
|
2012-03-16 15:34:57 -07:00
|
|
|
{"atom keys", ?_assertEqual(
|
|
|
|
encode([{key, <<"value">>}]),
|
|
|
|
[start_object, {key, <<"key">>}, {string, <<"value">>}, end_object, end_json]
|
|
|
|
)
|
2012-03-20 19:36:27 -07:00
|
|
|
},
|
|
|
|
{"bad string", ?_assertError(
|
|
|
|
badarg,
|
|
|
|
encode([<<"a bad string: ", 16#ffff/utf8>>])
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{"allow bad string", ?_assertEqual(
|
|
|
|
encode([<<"a bad string: ", 16#1ffff/utf8>>], [loose_unicode]),
|
|
|
|
[start_array, {string, <<"a bad string: ", 16#fffd/utf8>>}, end_array, end_json]
|
|
|
|
)
|
2011-11-29 19:39:14 -08:00
|
|
|
}
|
2011-10-21 18:16:16 -07:00
|
|
|
].
|
|
|
|
|
|
|
|
-endif.
|