Skip to content

Commit

Permalink
Improve escape-characters performance.
Browse files Browse the repository at this point in the history
This no longer performs a large number of generic dispatches.

This also only now supports writing <byte-string> as <unicode-string>
isn't yet supported correctly.
  • Loading branch information
waywardmonkeys committed May 8, 2014
1 parent 2cdb9fc commit f34325b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions json-serializer.dylan
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ define method write-object (serializer :: <json-serializer>, object :: <symbol>)
write-object(serializer, as(<string>, object));
end;

define function escape-characters (string :: <string>) => (string :: <string>)
let output = "";
for (char :: <character> in string)
define function escape-characters (string :: <byte-string>) => (string :: <byte-string>)
let output = make(limited(<stretchy-vector>, of: <byte-character>, size: string.size * 2));
for (char :: <byte-character> in string)
select (char)
'\\', '"', '\b', '\f', '\n', '\r', '\t' =>
output := add!(output, '\\');
output := add!(output, char);
add!(output, '\\');
add!(output, char);
otherwise =>
output := add!(output, char);
add!(output, char);
end;
end;
output
as(<byte-string>, output)
end;

define method write-object (serializer :: <json-serializer>, object :: <string>)
define method write-object (serializer :: <json-serializer>, object :: <byte-string>)
let stream = serializer.stream;
write(stream, "\"");
write(stream, escape-characters(object));
Expand Down

0 comments on commit f34325b

Please sign in to comment.