|
@@ -2,7 +2,7 @@
|
|
|
%%% @private
|
|
|
%%% @end
|
|
|
%%%
|
|
|
-%%% Copyright (c) 2013-2014, Takeru Ohta <phjgt308@gmail.com>
|
|
|
+%%% Copyright (c) 2013-2015, Takeru Ohta <phjgt308@gmail.com>
|
|
|
%%%
|
|
|
%%% The MIT License
|
|
|
%%%
|
|
@@ -39,6 +39,7 @@
|
|
|
-define(IS_REDUNDANT_UTF8(B1, B2, FirstBitN), (B1 =:= 0 andalso B2 < (1 bsl (FirstBitN + 1)))).
|
|
|
-define(HEX(N, I), (binary:at(<<"0123456789abcdef">>, (N bsr (I * 4)) band 2#1111))).
|
|
|
-define(UNICODE_TO_HEX(Code), ?HEX(Code, 3), ?HEX(Code, 2), ?HEX(Code, 1), ?HEX(Code, 0)).
|
|
|
+-define(IS_STR(X), is_binary(X); is_atom(X)).
|
|
|
|
|
|
-type encode_result() :: {ok, binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
-type next() :: {array_values, [jsone:json_value()]}
|
|
@@ -46,8 +47,8 @@
|
|
|
| {object_members, jsone:json_object_members()}.
|
|
|
|
|
|
-record(encode_opt_v1, { native_utf8 = false :: boolean() }).
|
|
|
--define(ENCODE_OPT, #encode_opt_v1).
|
|
|
--type encode_opt() :: #encode_opt_v1{}.
|
|
|
+-define(OPT, #encode_opt_v1).
|
|
|
+-type opt() :: #encode_opt_v1{}.
|
|
|
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Exported Functions
|
|
@@ -64,7 +65,7 @@ encode(Value, Options) ->
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Internal Functions
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
--spec next([next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec next([next()], binary(), opt()) -> encode_result().
|
|
|
next([], Buf, _) -> {ok, Buf};
|
|
|
next([Next | Nexts], Buf, Opt) ->
|
|
|
case Next of
|
|
@@ -82,25 +83,26 @@ next([Next | Nexts], Buf, Opt) ->
|
|
|
end
|
|
|
end.
|
|
|
|
|
|
--spec value(jsone:json_value(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec value(jsone:json_value(), [next()], binary(), opt()) -> encode_result().
|
|
|
value(null, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "null">>, Opt);
|
|
|
value(false, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "false">>, Opt);
|
|
|
value(true, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, "true">>, Opt);
|
|
|
-value(Value, Nexts, Buf, Opt) when is_atom(Value) -> string(atom_to_binary(Value, utf8), Nexts, Buf, Opt);
|
|
|
value(Value, Nexts, Buf, Opt) when is_integer(Value) -> next(Nexts, <<Buf/binary, (integer_to_binary(Value))/binary>>, Opt);
|
|
|
value(Value, Nexts, Buf, Opt) when is_float(Value) -> next(Nexts, <<Buf/binary, (float_to_binary(Value))/binary>>, Opt);
|
|
|
-value(Value, Nexts, Buf, Opt) when is_binary(Value) -> string(Value, Nexts, Buf, Opt);
|
|
|
-value({_} = Value, Nexts, Buf, Opt) -> object(Value, Nexts, Buf, Opt);
|
|
|
-value([{}], Nexts, Buf, Opt) -> object({[]}, Nexts, Buf, Opt);
|
|
|
-value([{_, _}|_] = Value, Nexts, Buf, Opt) -> object({Value}, Nexts, Buf, Opt);
|
|
|
+value(Value, Nexts, Buf, Opt) when ?IS_STR(Value) -> string(Value, Nexts, Buf, Opt);
|
|
|
+value({Value}, Nexts, Buf, Opt) -> object(Value, Nexts, Buf, Opt);
|
|
|
+value([{}], Nexts, Buf, Opt) -> object([], Nexts, Buf, Opt);
|
|
|
+value([{_, _}|_] = Value, Nexts, Buf, Opt) -> object(Value, Nexts, Buf, Opt);
|
|
|
value(Value, Nexts, Buf, Opt) when is_list(Value) -> array(Value, Nexts, Buf, Opt);
|
|
|
-value(Value, Nexts, Buf, _) -> ?ERROR(value, [Value, Nexts, Buf]).
|
|
|
+value(Value, Nexts, Buf, _) -> ?ERROR(value, [Value, Nexts, Buf]).
|
|
|
|
|
|
--spec string(jsone:json_string(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec string(jsone:json_string(), [next()], binary(), opt()) -> encode_result().
|
|
|
string(<<Str/binary>>, Nexts, Buf, Opt) ->
|
|
|
- escape_string(Str, Nexts, <<Buf/binary, $">>, Opt).
|
|
|
+ escape_string(Str, Nexts, <<Buf/binary, $">>, Opt);
|
|
|
+string(Str, Nexts, Buf, Opt) ->
|
|
|
+ string(atom_to_binary(Str, utf8), Nexts, Buf, Opt).
|
|
|
|
|
|
--spec escape_string(binary(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec escape_string(binary(), [next()], binary(), opt()) -> encode_result().
|
|
|
escape_string(<<"">>, Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, $">>, Opt);
|
|
|
escape_string(<<$", Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $">>, Opt);
|
|
|
escape_string(<<$\/, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $\/>>, Opt);
|
|
@@ -112,7 +114,7 @@ escape_string(<<$\r, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, N
|
|
|
escape_string(<<$\t, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, $\\, $t>>, Opt);
|
|
|
escape_string(<<0:1, C:7, Str/binary>>, Nexts, Buf, Opt) -> escape_string(Str, Nexts, <<Buf/binary, C>>, Opt);
|
|
|
escape_string(<<2#110:3, B1:5, 2#10:2, B2:6, Str/binary>>, Nexts, Buf, Opt) when not ?IS_REDUNDANT_UTF8(B1, B2, 5) ->
|
|
|
- case Opt?ENCODE_OPT.native_utf8 of
|
|
|
+ case Opt?OPT.native_utf8 of
|
|
|
false ->
|
|
|
Unicode = (B1 bsl 6) + B2,
|
|
|
escape_unicode_char(Str, Unicode, Nexts, Buf, Opt);
|
|
@@ -120,7 +122,7 @@ escape_string(<<2#110:3, B1:5, 2#10:2, B2:6, Str/binary>>, Nexts, Buf, Opt) when
|
|
|
unicode_char(Str, <<2#110:3, B1:5, 2#10:2, B2:6>>, Nexts, Buf, Opt)
|
|
|
end;
|
|
|
escape_string(<<2#1110:4, B1:4, 2#10:2, B2:6, 2#10:2, B3:6, Str/binary>>, Nexts, Buf, Opt) when not ?IS_REDUNDANT_UTF8(B1, B2, 4) ->
|
|
|
- case Opt?ENCODE_OPT.native_utf8 of
|
|
|
+ case Opt?OPT.native_utf8 of
|
|
|
false ->
|
|
|
Unicode = (B1 bsl 12) + (B2 bsl 6) + B3,
|
|
|
escape_unicode_char(Str, Unicode, Nexts, Buf, Opt);
|
|
@@ -128,7 +130,7 @@ escape_string(<<2#1110:4, B1:4, 2#10:2, B2:6, 2#10:2, B3:6, Str/binary>>, Nexts,
|
|
|
unicode_char(Str, <<2#1110:4, B1:4, 2#10:2, B2:6, 2#10:2, B3:6>>, Nexts, Buf, Opt)
|
|
|
end;
|
|
|
escape_string(<<2#11110:5, B1:3, 2#10:2, B2:6, 2#10:2, B3:6, 2#10:2, B4:6, Str/binary>>, Nexts, Buf, Opt) when not ?IS_REDUNDANT_UTF8(B1, B2, 3) ->
|
|
|
- case Opt?ENCODE_OPT.native_utf8 of
|
|
|
+ case Opt?OPT.native_utf8 of
|
|
|
false ->
|
|
|
Unicode = (B1 bsl 18) + (B2 bsl 12) + (B3 bsl 6) + B4,
|
|
|
escape_unicode_char(Str, Unicode, Nexts, Buf, Opt);
|
|
@@ -141,7 +143,7 @@ escape_string(Str, Nexts, Buf, _) ->
|
|
|
unicode_char(Str, Char, Nexts, Buf, Opt) ->
|
|
|
escape_string(Str, Nexts, <<Buf/binary, Char/binary>>, Opt).
|
|
|
|
|
|
--spec escape_unicode_char(binary(), char(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec escape_unicode_char(binary(), char(), [next()], binary(), opt()) -> encode_result().
|
|
|
escape_unicode_char(<<Str/binary>>, Unicode, Nexts, Buf, Opt) when Unicode =< 16#FFFF ->
|
|
|
escape_string(Str, Nexts, <<Buf/binary, $\\, $u, ?UNICODE_TO_HEX(Unicode)>>, Opt);
|
|
|
escape_unicode_char(<<Str/binary>>, Unicode, Nexts, Buf, Opt) ->
|
|
@@ -149,33 +151,31 @@ escape_unicode_char(<<Str/binary>>, Unicode, Nexts, Buf, Opt) ->
|
|
|
<<High:10, Low:10>> = <<(Unicode - 16#10000):20>>, % XXX: inefficient
|
|
|
escape_string(Str, Nexts, <<Buf/binary, $\\, $u, ?UNICODE_TO_HEX(High + 16#D800), $\\, $u, ?UNICODE_TO_HEX(Low + 16#DC00)>>, Opt).
|
|
|
|
|
|
--spec array(jsone:json_array(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec array(jsone:json_array(), [next()], binary(), opt()) -> encode_result().
|
|
|
array(List, Nexts, Buf, Opt) ->
|
|
|
array_values(List, Nexts, <<Buf/binary, $[>>, Opt).
|
|
|
|
|
|
--spec array_values(jsone:json_array(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec array_values(jsone:json_array(), [next()], binary(), opt()) -> encode_result().
|
|
|
array_values([], Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, $]>>, Opt);
|
|
|
array_values([X | Xs], Nexts, Buf, Opt) -> value(X, [{array_values, Xs} | Nexts], Buf, Opt).
|
|
|
|
|
|
--spec object(jsone:json_object(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
-object({Members}, Nexts, Buf, Opt) ->
|
|
|
+-spec object(jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
|
|
|
+object(Members, Nexts, Buf, Opt) ->
|
|
|
object_members(Members, Nexts, <<Buf/binary, ${>>, Opt).
|
|
|
|
|
|
--spec object_members(jsone:json_object_members(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec object_members(jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
|
|
|
object_members([], Nexts, Buf, Opt) -> next(Nexts, <<Buf/binary, $}>>, Opt);
|
|
|
-object_members([{<<Key/binary>>, Value} | Xs], Nexts, Buf, Opt) -> string(Key, [{object_value, Value, Xs} | Nexts], Buf, Opt);
|
|
|
-object_members([{Key, Value} | Xs], Nexts, Buf, Opt) when is_atom(Key) -> string(atom_to_binary(Key, utf8), [{object_value, Value, Xs} | Nexts], Buf, Opt);
|
|
|
+object_members([{Key, Value} | Xs], Nexts, Buf, Opt) when ?IS_STR(Key) -> string(Key, [{object_value, Value, Xs} | Nexts], Buf, Opt);
|
|
|
object_members(Arg, Nexts, Buf, _) -> ?ERROR(object_members, [Arg, Nexts, Buf]).
|
|
|
|
|
|
--spec object_value(jsone:json_value(), jsone:json_object_members(), [next()], binary(), encode_opt()) -> encode_result().
|
|
|
+-spec object_value(jsone:json_value(), jsone:json_object_members(), [next()], binary(), opt()) -> encode_result().
|
|
|
object_value(Value, Members, Nexts, Buf, Opt) ->
|
|
|
value(Value, [{object_members, Members} | Nexts], <<Buf/binary, $:>>, Opt).
|
|
|
|
|
|
-
|
|
|
--spec parse_options([jsone:encode_option()]) -> encode_opt().
|
|
|
+-spec parse_options([jsone:encode_option()]) -> opt().
|
|
|
parse_options(Options) ->
|
|
|
- parse_option(Options, ?ENCODE_OPT{}).
|
|
|
+ parse_option(Options, ?OPT{}).
|
|
|
|
|
|
parse_option([], Opt) -> Opt;
|
|
|
parse_option([native_utf8|T], Opt) ->
|
|
|
- parse_option(T, Opt?ENCODE_OPT{native_utf8=true}).
|
|
|
+ parse_option(T, Opt?OPT{native_utf8=true}).
|