|
@@ -35,6 +35,8 @@
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Macros & Types
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
+-define(ERROR(Function, Args), {error, {badarg, [{?MODULE, Function, Args, [{line, ?LINE}]}]}}).
|
|
|
+
|
|
|
-type next() :: {array_next, [jsone:json_value()]}
|
|
|
| {object_value, jsone:json_object_members()}
|
|
|
| {object_next, jsone:json_string(), jsone:json_object_members()}.
|
|
@@ -47,22 +49,22 @@
|
|
|
| {object_value, jsone:json_string(), jsone:json_object_members()}
|
|
|
| {object_next, jsone:json_string(), jsone:json_value(), jsone:json_object_members()}.
|
|
|
|
|
|
+-type decode_result() :: {ok, jsone:json_value(), Rest::binary()} | {error, {Reason::term(), [erlang:stack_item()]}}.
|
|
|
+
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Exported Functions
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% @doc JSONバイナリをデコードする.
|
|
|
-%%
|
|
|
-%% デコードに失敗した場合は`{invalid_json, 失敗位置より後のJSON::binary()}'形式のエラーが送出される.
|
|
|
--spec decode(binary()) -> {jsone:json_value(), RestJson::binary()}.
|
|
|
+-spec decode(binary()) -> decode_result().
|
|
|
decode(<<Json/binary>>) ->
|
|
|
whitespace(Json, value, [], <<"">>).
|
|
|
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
%% Internal Functions
|
|
|
%%--------------------------------------------------------------------------------
|
|
|
--spec next(binary(), jsone:json_value(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec next(binary(), jsone:json_value(), [next()], binary()) -> decode_result().
|
|
|
next(<<Bin/binary>>, Value, [], _Buf) ->
|
|
|
- {Value, Bin};
|
|
|
+ {ok, Value, Bin};
|
|
|
next(<<Bin/binary>>, Value, [Next | Nexts], Buf) ->
|
|
|
case Next of
|
|
|
{array_next, Values} -> whitespace(Bin, {array_next, [Value | Values]}, Nexts, Buf);
|
|
@@ -70,7 +72,7 @@ next(<<Bin/binary>>, Value, [Next | Nexts], Buf) ->
|
|
|
{object_next, Key, Members} -> whitespace(Bin, {object_next, Key, Value, Members}, Nexts, Buf)
|
|
|
end.
|
|
|
|
|
|
--spec whitespace(binary(), whitespace_next(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec whitespace(binary(), whitespace_next(), [next()], binary()) -> decode_result().
|
|
|
whitespace(<<$ , Bin/binary>>, Next, Nexts, Buf) -> whitespace(Bin, Next, Nexts, Buf);
|
|
|
whitespace(<<$\t, Bin/binary>>, Next, Nexts, Buf) -> whitespace(Bin, Next, Nexts, Buf);
|
|
|
whitespace(<<$\r, Bin/binary>>, Next, Nexts, Buf) -> whitespace(Bin, Next, Nexts, Buf);
|
|
@@ -82,14 +84,14 @@ whitespace(<<Bin/binary>>, Next, Nexts, Buf) ->
|
|
|
object -> object(Bin, Nexts, Buf);
|
|
|
string -> case Bin of
|
|
|
<<$", Bin2/binary>> -> string(Bin2, byte_size(Buf), Nexts, Buf);
|
|
|
- _ -> error(badarg, [Bin, Next, Nexts, Buf])
|
|
|
+ _ -> ?ERROR(whitespace, [Bin, Next, Nexts, Buf])
|
|
|
end;
|
|
|
{array_next, Values} -> array_next(Bin, Values, Nexts, Buf);
|
|
|
{object_value, Key, Members} -> object_value(Bin, Key, Members, Nexts, Buf);
|
|
|
{object_next, Key, Value, Members} -> object_next(Bin, [{Key, Value} | Members], Nexts, Buf)
|
|
|
end.
|
|
|
|
|
|
--spec value(binary(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec value(binary(), [next()], binary()) -> decode_result().
|
|
|
value(<<"false", Bin/binary>>, Nexts, Buf) -> next(Bin, false, Nexts, Buf);
|
|
|
value(<<"true", Bin/binary>>, Nexts, Buf) -> next(Bin, true, Nexts, Buf);
|
|
|
value(<<"null", Bin/binary>>, Nexts, Buf) -> next(Bin, null, Nexts, Buf);
|
|
@@ -98,29 +100,29 @@ value(<<${, Bin/binary>>, Nexts, Buf) -> whitespace(Bin, object, Nexts, Buf
|
|
|
value(<<$", Bin/binary>>, Nexts, Buf) -> string(Bin, byte_size(Buf), Nexts, Buf);
|
|
|
value(<<Bin/binary>>, Nexts, Buf) -> number(Bin, Nexts, Buf).
|
|
|
|
|
|
--spec array(binary(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec array(binary(), [next()], binary()) -> decode_result().
|
|
|
array(<<$], Bin/binary>>, Nexts, Buf) -> next(Bin, [], Nexts, Buf);
|
|
|
array(<<Bin/binary>>, Nexts, Buf) -> whitespace(Bin, value, [{array_next, []} | Nexts], Buf).
|
|
|
|
|
|
--spec array_next(binary(), [jsone:json_value()], [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec array_next(binary(), [jsone:json_value()], [next()], binary()) -> decode_result().
|
|
|
array_next(<<$], Bin/binary>>, Values, Nexts, Buf) -> next(Bin, lists:reverse(Values), Nexts, Buf);
|
|
|
array_next(<<$,, Bin/binary>>, Values, Nexts, Buf) -> whitespace(Bin, value, [{array_next, Values} | Nexts], Buf);
|
|
|
-array_next(Bin, Values, Nexts, Buf) -> error(badarg, [Bin, Values, Nexts, Buf]).
|
|
|
+array_next(Bin, Values, Nexts, Buf) -> ?ERROR(array_next, [Bin, Values, Nexts, Buf]).
|
|
|
|
|
|
--spec object(binary(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec object(binary(), [next()], binary()) -> decode_result().
|
|
|
object(<<$}, Bin/binary>>, Nexts, Buf) -> next(Bin, {[]}, Nexts, Buf);
|
|
|
object(<<Bin/binary>>, Nexts, Buf) -> whitespace(Bin, string, [{object_value, []} | Nexts], Buf).
|
|
|
|
|
|
--spec object_value(binary(), jsone:json_string(), jsone:json_object_members(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec object_value(binary(), jsone:json_string(), jsone:json_object_members(), [next()], binary()) -> decode_result().
|
|
|
object_value(<<$:, Bin/binary>>, Key, Members, Nexts, Buf) -> whitespace(Bin, value, [{object_next, Key, Members} | Nexts], Buf);
|
|
|
-object_value(Bin, Key, Members, Nexts, Buf) -> error(badarg, [Bin, Key, Members, Nexts, Buf]).
|
|
|
+object_value(Bin, Key, Members, Nexts, Buf) -> ?ERROR(object_value, [Bin, Key, Members, Nexts, Buf]).
|
|
|
|
|
|
--spec object_next(binary(), jsone:json_object_members(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec object_next(binary(), jsone:json_object_members(), [next()], binary()) -> decode_result().
|
|
|
object_next(<<$}, Bin/binary>>, Members, Nexts, Buf) -> next(Bin, {Members}, Nexts, Buf);
|
|
|
object_next(<<$,, Bin/binary>>, Members, Nexts, Buf) -> whitespace(Bin, string, [{object_value, Members} | Nexts], Buf);
|
|
|
-object_next(Bin, Members, Nexts, Buf) -> error(badarg, [Bin, Members, Nexts, Buf]).
|
|
|
+object_next(Bin, Members, Nexts, Buf) -> ?ERROR(object_next, [Bin, Members, Nexts, Buf]).
|
|
|
|
|
|
--spec string(binary(), non_neg_integer(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec string(binary(), non_neg_integer(), [next()], binary()) -> decode_result().
|
|
|
string(<<$", Bin/binary>>, Start, Nexts, Buf) -> next(Bin, binary:part(Buf, Start, byte_size(Buf) - Start), Nexts, Buf);
|
|
|
string(<<$\\, B/binary>>, Start, Nexts, Buf) ->
|
|
|
case B of
|
|
@@ -133,12 +135,12 @@ string(<<$\\, B/binary>>, Start, Nexts, Buf) ->
|
|
|
<<$r, Bin/binary>> -> string(Bin, Start, Nexts, <<Buf/binary, $\r>>);
|
|
|
<<$t, Bin/binary>> -> string(Bin, Start, Nexts, <<Buf/binary, $\t>>);
|
|
|
<<$u, Bin/binary>> -> unicode_string(Bin, Start, Nexts, Buf);
|
|
|
- _ -> error(badarg, [<<$\\, B/binary>>, Start, Nexts, Buf])
|
|
|
+ _ -> ?ERROR(string, [<<$\\, B/binary>>, Start, Nexts, Buf])
|
|
|
end;
|
|
|
string(<<C, Bin/binary>>, Start, Nexts, Buf) when 16#20 =< C ->
|
|
|
string(Bin, Start, Nexts, <<Buf/binary, C>>).
|
|
|
|
|
|
--spec unicode_string(binary(), non_neg_integer(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec unicode_string(binary(), non_neg_integer(), [next()], binary()) -> decode_result().
|
|
|
unicode_string(<<N:4/binary, Bin/binary>>, Start, Nexts, Buf) ->
|
|
|
case binary_to_integer(N, 16) of
|
|
|
High when 16#D800 =< High, High =< 16#DBFF ->
|
|
@@ -149,17 +151,17 @@ unicode_string(<<N:4/binary, Bin/binary>>, Start, Nexts, Buf) ->
|
|
|
Low when 16#DC00 =< Low, Low =< 16#DFFF ->
|
|
|
Unicode = 16#10000 + (High - 16#D800) * 16#400 + (Low - 16#DC00),
|
|
|
string(Bin2, Start, Nexts, unicode_to_utf8(Unicode, Buf));
|
|
|
- _ -> error(badarg, [<<N/binary, Bin/binary>>, Start, Nexts, Buf])
|
|
|
+ _ -> ?ERROR(unicode_string, [<<N/binary, Bin/binary>>, Start, Nexts, Buf])
|
|
|
end;
|
|
|
- _ -> error(badarg, [<<N/binary, Bin/binary>>, Start, Nexts, Buf])
|
|
|
+ _ -> ?ERROR(unicode_string, [<<N/binary, Bin/binary>>, Start, Nexts, Buf])
|
|
|
end;
|
|
|
Unicode when 16#DC00 =< Unicode, Unicode =< 16#DFFF -> % サロゲートペアの後半部分
|
|
|
- error(badarg, [<<N/binary, Bin/binary>>, Start, Nexts, Buf]);
|
|
|
+ ?ERROR(unicode_string, [<<N/binary, Bin/binary>>, Start, Nexts, Buf]);
|
|
|
Unicode ->
|
|
|
string(Bin, Start, Nexts, unicode_to_utf8(Unicode, Buf))
|
|
|
end;
|
|
|
unicode_string(Bin, Acc, Nexts, Buf) ->
|
|
|
- error(badarg, [Bin, Acc, Nexts, Buf]).
|
|
|
+ ?ERROR(unicode_string, [Bin, Acc, Nexts, Buf]).
|
|
|
|
|
|
-spec unicode_to_utf8(0..1114111, binary()) -> binary().
|
|
|
unicode_to_utf8(Code, Buf) when Code < 16#80 ->
|
|
@@ -180,39 +182,39 @@ unicode_to_utf8(Code, Buf) -> % NOTE: サロゲートペアの仕組み上、コ
|
|
|
D = 2#10000000 bor (Code band 2#111111),
|
|
|
<<Buf/binary, A, B, C, D>>.
|
|
|
|
|
|
--spec number(binary(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number(binary(), [next()], binary()) -> decode_result().
|
|
|
number(<<$-, Bin/binary>>, Nexts, Buf) -> number_integer_part(Bin, -1, Nexts, Buf);
|
|
|
number(<<Bin/binary>>, Nexts, Buf) -> number_integer_part(Bin, 1, Nexts, Buf).
|
|
|
|
|
|
--spec number_integer_part(binary(), 1|-1, [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_integer_part(binary(), 1|-1, [next()], binary()) -> decode_result().
|
|
|
number_integer_part(<<$0, Bin/binary>>, Sign, Nexts, Buf) ->
|
|
|
number_fraction_part(Bin, Sign, 0, Nexts, Buf);
|
|
|
number_integer_part(<<C, Bin/binary>>, Sign, Nexts, Buf) when $1 =< C, C =< $9 ->
|
|
|
number_integer_part_rest(Bin, C - $0, Sign, Nexts, Buf);
|
|
|
number_integer_part(Bin, Sign, Nexts, Buf) ->
|
|
|
- error(badarg, [Bin, Sign, Nexts, Buf]).
|
|
|
+ ?ERROR(number_integer_part, [Bin, Sign, Nexts, Buf]).
|
|
|
|
|
|
--spec number_integer_part_rest(binary(), non_neg_integer(), 1|-1, [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_integer_part_rest(binary(), non_neg_integer(), 1|-1, [next()], binary()) -> decode_result().
|
|
|
number_integer_part_rest(<<C, Bin/binary>>, N, Sign, Nexts, Buf) when $0 =< C, C =< $9 ->
|
|
|
number_integer_part_rest(Bin, N * 10 + C - $0, Sign, Nexts, Buf);
|
|
|
number_integer_part_rest(<<Bin/binary>>, N, Sign, Nexts, Buf) ->
|
|
|
number_fraction_part(Bin, Sign, N, Nexts, Buf).
|
|
|
|
|
|
--spec number_fraction_part(binary(), 1|-1, non_neg_integer(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_fraction_part(binary(), 1|-1, non_neg_integer(), [next()], binary()) -> decode_result().
|
|
|
number_fraction_part(<<$., Bin/binary>>, Sign, Int, Nexts, Buf) ->
|
|
|
number_fraction_part_rest(Bin, Sign, Int, 0, Nexts, Buf);
|
|
|
number_fraction_part(<<Bin/binary>>, Sign, Int, Nexts, Buf) ->
|
|
|
number_exponation_part(Bin, Sign * Int, 0, Nexts, Buf).
|
|
|
|
|
|
--spec number_fraction_part_rest(binary(), 1|-1, non_neg_integer(), non_neg_integer(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_fraction_part_rest(binary(), 1|-1, non_neg_integer(), non_neg_integer(), [next()], binary()) -> decode_result().
|
|
|
number_fraction_part_rest(<<C, Bin/binary>>, Sign, N, DecimalOffset, Nexts, Buf) when $0 =< C, C =< $9 ->
|
|
|
number_fraction_part_rest(Bin, Sign, N * 10 + C - $0, DecimalOffset + 1, Nexts, Buf);
|
|
|
number_fraction_part_rest(<<Bin/binary>>, Sign, N, DecimalOffset, Nexts, Buf) when DecimalOffset > 0 ->
|
|
|
number_exponation_part(Bin, Sign * N, DecimalOffset, Nexts, Buf);
|
|
|
number_fraction_part_rest(Bin, Sign, N, DecimalOffset, Nexts, Buf) ->
|
|
|
- error(badarg, [Bin, Sign, N, DecimalOffset, Nexts, Buf]).
|
|
|
+ ?ERROR(number_fraction_part_rest, [Bin, Sign, N, DecimalOffset, Nexts, Buf]).
|
|
|
|
|
|
--spec number_exponation_part(binary(), integer(), non_neg_integer(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_exponation_part(binary(), integer(), non_neg_integer(), [next()], binary()) -> decode_result().
|
|
|
number_exponation_part(<<$e, $+, Bin/binary>>, N, DecimalOffset, Nexts, Buf) ->
|
|
|
number_exponation_part(Bin, N, DecimalOffset, 1, 0, true, Nexts, Buf);
|
|
|
number_exponation_part(<<$E, $+, Bin/binary>>, N, DecimalOffset, Nexts, Buf) ->
|
|
@@ -231,11 +233,11 @@ number_exponation_part(<<Bin/binary>>, N, DecimalOffset, Nexts, Buf) ->
|
|
|
_ -> next(Bin, N / math:pow(10, DecimalOffset), Nexts, Buf)
|
|
|
end.
|
|
|
|
|
|
--spec number_exponation_part(binary(), integer(), non_neg_integer(), 1|-1, non_neg_integer(), boolean(), [next()], binary()) -> {jsone:json_value(), Rest::binary()}.
|
|
|
+-spec number_exponation_part(binary(), integer(), non_neg_integer(), 1|-1, non_neg_integer(), boolean(), [next()], binary()) -> decode_result().
|
|
|
number_exponation_part(<<C, Bin/binary>>, N, DecimalOffset, ExpSign, Exp, _, Nexts, Buf) when $0 =< C, C =< $9 ->
|
|
|
number_exponation_part(Bin, N, DecimalOffset, ExpSign, Exp * 10 + C - $0, false, Nexts, Buf);
|
|
|
number_exponation_part(<<Bin/binary>>, N, DecimalOffset, ExpSign, Exp, false, Nexts, Buf) ->
|
|
|
Pos = ExpSign * Exp - DecimalOffset,
|
|
|
next(Bin, N * math:pow(10, Pos), Nexts, Buf);
|
|
|
number_exponation_part(Bin, N, DecimalOffset, ExpSign, Exp, IsFirst, Nexts, Buf) ->
|
|
|
- error(badarg, [Bin, N, DecimalOffset, ExpSign, Exp, IsFirst, Nexts, Buf]).
|
|
|
+ ?ERROR(number_exponation_part, [Bin, N, DecimalOffset, ExpSign, Exp, IsFirst, Nexts, Buf]).
|