escape strings and keys in the encoder

This commit is contained in:
alisdair sullivan 2012-03-15 22:56:21 -07:00
parent 97a7d295f1
commit 8dafdb32b3
2 changed files with 25 additions and 10 deletions

View file

@ -97,7 +97,7 @@ json_escape(<<$\t, Rest/binary>>, Opts, Acc) ->
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) when C >= 0, C < $\s ->
json_escape(Rest,
Opts,
<<Acc/binary, (unicode:characters_to_binary(json_escape_sequence(C)))/binary>>
<<Acc/binary, (json_escape_sequence(C))/binary>>
);
%% escape forward slashes -- optionally -- to faciliate microsoft's retarded
%% date format
@ -108,7 +108,7 @@ json_escape(<<C/utf8, Rest/binary>>, Opts, Acc)
when C == 16#2028; C == 16#2029 ->
json_escape(Rest,
Opts,
<<Acc/binary, (unicode:characters_to_binary(json_escape_sequence(C)))/binary>>
<<Acc/binary, (json_escape_sequence(C))/binary>>
);
%% any other legal codepoint
json_escape(<<C/utf8, Rest/binary>>, Opts, Acc) ->
@ -122,7 +122,7 @@ json_escape(Rest, Opts, Acc) ->
%% convert a codepoint to it's \uXXXX equiv.
json_escape_sequence(X) ->
<<A:4, B:4, C:4, D:4>> = <<X:16>>,
[$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))].
unicode:characters_to_binary([$\\, $u, (to_hex(A)), (to_hex(B)), (to_hex(C)), (to_hex(D))]).
to_hex(10) -> $a;