whitespace

This commit is contained in:
alisdair sullivan 2011-07-31 00:05:34 -07:00
parent ecdd34fa37
commit 03381567d2
4 changed files with 31 additions and 13 deletions

View file

@ -357,7 +357,9 @@ key(Bin, Stack, Opts) ->
string(Bin, Stack, Opts) -> string(Bin, Stack, Opts, []). string(Bin, Stack, Opts) -> string(Bin, Stack, Opts, []).
string(<<?quote/?utfx, Rest/binary>>, [key|_] = Stack, Opts, Acc) -> string(<<?quote/?utfx, Rest/binary>>, [key|_] = Stack, Opts, Acc) ->
{jsx, {key, unicode:characters_to_binary(lists:reverse(Acc))}, fun() -> colon(Rest, Stack, Opts) end}; {jsx, {key, unicode:characters_to_binary(lists:reverse(Acc))}, fun() ->
colon(Rest, Stack, Opts)
end};
string(<<?quote/?utfx, Rest/binary>>, Stack, Opts, Acc) -> string(<<?quote/?utfx, Rest/binary>>, Stack, Opts, Acc) ->
{jsx, {string, unicode:characters_to_binary(lists:reverse(Acc))}, fun() -> {jsx, {string, unicode:characters_to_binary(lists:reverse(Acc))}, fun() ->
maybe_done(Rest, Stack, Opts) maybe_done(Rest, Stack, Opts)
@ -1228,7 +1230,11 @@ check_bad(List) ->
). ).
check_replaced(List) -> check_replaced(List) ->
lists:dropwhile(fun({_, [{string, <<16#fffd/utf8>>}|_]}) -> true ; (_) -> false end, lists:dropwhile(fun({_, [{string, <<16#fffd/utf8>>}|_]}) ->
true
; (_) ->
false
end,
check(List, [loose_unicode], []) check(List, [loose_unicode], [])
). ).

View file

@ -76,7 +76,9 @@ list_or_object(Forms, _, _) -> {error, {badjson, Forms}}.
key([{key, Key}|Forms], Stack, Opts) when is_binary(Key) -> key([{key, Key}|Forms], Stack, Opts) when is_binary(Key) ->
{jsx, {key, json_escape(Key, Opts)}, fun() -> value(Forms, Stack, Opts) end}; {jsx, {key, json_escape(Key, Opts)}, fun() ->
value(Forms, Stack, Opts)
end};
key([end_object|Forms], [object|Stack], Opts) -> key([end_object|Forms], [object|Stack], Opts) ->
{jsx, end_object, fun() -> maybe_done(Forms, Stack, Opts) end}; {jsx, end_object, fun() -> maybe_done(Forms, Stack, Opts) end};
key([], Stack, Opts) -> key([], Stack, Opts) ->
@ -89,7 +91,9 @@ key(Forms, _, _) -> {error, {badjson, Forms}}.
value([{string, S}|Forms], Stack, Opts) when is_binary(S) -> value([{string, S}|Forms], Stack, Opts) when is_binary(S) ->
{jsx, {string, json_escape(S, Opts)}, fun() -> maybe_done(Forms, Stack, Opts) end}; {jsx, {string, json_escape(S, Opts)}, fun() ->
maybe_done(Forms, Stack, Opts)
end};
value([{float, F}|Forms], Stack, Opts) when is_float(F) -> value([{float, F}|Forms], Stack, Opts) when is_float(F) ->
{jsx, {float, F}, fun() -> maybe_done(Forms, Stack, Opts) end}; {jsx, {float, F}, fun() -> maybe_done(Forms, Stack, Opts) end};
value([{integer, I}|Forms], Stack, Opts) when is_integer(I) -> value([{integer, I}|Forms], Stack, Opts) when is_integer(I) ->
@ -164,10 +168,11 @@ json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C >= 0, C < $\s ->
json_escape(Rest, Opts, <<Acc/binary, (json_escape_sequence(C))/binary>>); json_escape(Rest, Opts, <<Acc/binary, (json_escape_sequence(C))/binary>>);
%% escape forward slashes -- optionally -- to faciliate microsoft's retarded %% escape forward slashes -- optionally -- to faciliate microsoft's retarded
%% date format %% date format
json_escape(<<$/, Rest/binary>>, Opts = #opts{escape_forward_slash = true}, Acc) -> json_escape(<<$/, Rest/binary>>, Opts=#opts{escape_forward_slash=true}, Acc) ->
json_escape(Rest, Opts, <<Acc/binary, $\\, $/>>); json_escape(Rest, Opts, <<Acc/binary, $\\, $/>>);
%% escape u+2028 and u+2029 to avoid problems with jsonp %% escape u+2028 and u+2029 to avoid problems with jsonp
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C == 16#2028; C == 16#2029 -> json_escape(<<C/utf8, Rest/binary>>, Opts, Acc)
when C == 16#2028; C == 16#2029 ->
json_escape(Rest, Opts, <<Acc/binary, (json_escape_sequence(C))/binary>>); json_escape(Rest, Opts, <<Acc/binary, (json_escape_sequence(C))/binary>>);
%% any other legal codepoint %% any other legal codepoint
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) -> json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) ->
@ -233,7 +238,8 @@ encode_incremental([Term|Terms], F, Expected, Acc) ->
loop({error, _}, _) -> error; loop({error, _}, _) -> error;
loop({jsx, incomplete, Next}, Acc) -> {jsx, incomplete, Next, lists:reverse(Acc)}; loop({jsx, incomplete, Next}, Acc) ->
{jsx, incomplete, Next, lists:reverse(Acc)};
loop({jsx, end_json, Next}, Acc) -> loop({jsx, end_json, Next}, Acc) ->
{jsx, incomplete, F} = Next(), {jsx, incomplete, F} = Next(),
{error, {badjson, []}} = F([]), {error, {badjson, []}} = F([]),
@ -311,8 +317,8 @@ escape_test_() ->
) )
}, },
{"microsoft i hate your date format", {"microsoft i hate your date format",
?_assert(json_escape( ?_assert(json_escape(<<"/Date(1303502009425)/">>,
<<"/Date(1303502009425)/">>, #opts{escape_forward_slash=true} #opts{escape_forward_slash=true}
) =:= <<"\\/Date(1303502009425)\\/">> ) =:= <<"\\/Date(1303502009425)\\/">>
) )
} }

View file

@ -411,7 +411,9 @@ repeated_keys_test_() ->
) )
}, },
{"nested repeated key", {"nested repeated key",
?_assert(json_to_term(<<"[{\"a\":false,\"a\":true},{\"a\":false,\"a\":true}]">>, []) ?_assert(json_to_term(
<<"[{\"a\":false,\"a\":true},{\"a\":false,\"a\":true}]">>,
[])
=:= [[{<<"a">>, true}], [{<<"a">>, true}]] =:= [[{<<"a">>, true}], [{<<"a">>, true}]]
) )
}, },
@ -448,7 +450,9 @@ stream_test_() ->
[ [
{"streaming mode", {"streaming mode",
?_assert(begin ?_assert(begin
{jsx, incomplete, F} = json_to_term(<<"{">>, [{stream, true}]), {jsx, incomplete, F} = json_to_term(<<"{">>,
[{stream, true}]
),
F(<<"}">>) F(<<"}">>)
end =:= [{}]) end =:= [{}])
} }

View file

@ -233,7 +233,8 @@ detect_encoding(<<0, X>>, Opts) when X =/= 0 ->
{jsx, incomplete, {jsx, incomplete,
fun(end_stream) -> fun(end_stream) ->
try try
{jsx, incomplete, Next} = (jsx_utf16:decoder(Opts))(<<0, X>>), {jsx, incomplete, Next}
= (jsx_utf16:decoder(Opts))(<<0, X>>),
Next(end_stream) Next(end_stream)
catch catch
error:function_clause -> {error, {badjson, <<0, X>>}} error:function_clause -> {error, {badjson, <<0, X>>}}
@ -246,7 +247,8 @@ detect_encoding(<<X, 0>>, Opts) when X =/= 0 ->
{jsx, incomplete, {jsx, incomplete,
fun(end_stream) -> fun(end_stream) ->
try try
{jsx, incomplete, Next} = (jsx_utf16le:decoder(Opts))(<<X, 0>>), {jsx, incomplete, Next}
= (jsx_utf16le:decoder(Opts))(<<X, 0>>),
Next(end_stream) Next(end_stream)
catch catch
error:function_clause -> {error, {badjson, <<X, 0>>}} error:function_clause -> {error, {badjson, <<X, 0>>}}