removes loose_unicode opt from erlang -> json conversion

This commit is contained in:
alisdair sullivan 2011-11-30 19:08:38 -08:00
parent 3857980f6c
commit 03e50ac0cc

View file

@ -225,9 +225,7 @@ json_escape(<<$\t, Rest/binary>>, Opts, Acc) ->
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C >= 0, C < $\s -> json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C >= 0, C < $\s ->
json_escape(Rest, json_escape(Rest,
Opts, Opts,
<<Acc/binary, <<Acc/binary, (unicode:characters_to_binary(json_escape_sequence(C)))/binary>>
(unicode:characters_to_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
@ -238,24 +236,11 @@ json_escape(<<C/utf8, Rest/binary>>, Opts, Acc)
when C == 16#2028; C == 16#2029 -> when C == 16#2028; C == 16#2029 ->
json_escape(Rest, json_escape(Rest,
Opts, Opts,
<<Acc/binary, <<Acc/binary, (unicode:characters_to_binary(json_escape_sequence(C)))/binary>>
(unicode:characters_to_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) ->
json_escape(Rest, Opts, <<Acc/binary, C/utf8>>); json_escape(Rest, Opts, <<Acc/binary, C/utf8>>);
%% if loose_unicode is true, replace illegal sequences with u+fffd
%% u+fffe and u+ffff
json_escape(<<239, 191, X, Rest/binary>>, Opts=#opts{loose_unicode=true}, Acc)
when X == 190; X == 191 ->
json_escape(Rest, Opts, <<Acc/binary, 16#fffd/utf8>>);
%% surrogates
json_escape(<<237, X, _, Rest/binary>>, Opts=#opts{loose_unicode=true}, Acc)
when X >= 160 ->
json_escape(Rest, Opts, <<Acc/binary, 16#fffd/utf8>>);
json_escape(<<_, Rest/binary>>, Opts=#opts{loose_unicode=true}, Acc) ->
json_escape(Rest, Opts, <<Acc/binary, 16#fffd/utf8>>);
json_escape(<<>>, _Opts, Acc) -> json_escape(<<>>, _Opts, Acc) ->
Acc; Acc;
json_escape(Rest, Opts, Acc) -> json_escape(Rest, Opts, Acc) ->
@ -268,7 +253,6 @@ json_escape_sequence(X) ->
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))]. [$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
to_hex(10) -> $a; to_hex(10) -> $a;
to_hex(11) -> $b; to_hex(11) -> $b;
to_hex(12) -> $c; to_hex(12) -> $c;
@ -345,13 +329,6 @@ binary_escape_test_() ->
#opts{escape_forward_slash=true} #opts{escape_forward_slash=true}
) =:= <<"\\/Date(1303502009425)\\/">> ) =:= <<"\\/Date(1303502009425)\\/">>
) )
},
%% <<239, 191, 191>> is u+ffff
{"loose unicode",
?_assert(json_escape(<<"hi there "/utf8, 239, 191, 191, "!"/utf8>>,
#opts{loose_unicode=true}
) =:= <<"hi there "/utf8, 16#fffd/utf8, "!"/utf8>>
)
} }
]. ].