From f34325bc42e1d98bf3580442de0348cd5ed91de7 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Thu, 8 May 2014 16:50:13 +0700 Subject: [PATCH] Improve escape-characters performance. This no longer performs a large number of generic dispatches. This also only now supports writing as isn't yet supported correctly. --- json-serializer.dylan | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/json-serializer.dylan b/json-serializer.dylan index 581ae00..00071b4 100644 --- a/json-serializer.dylan +++ b/json-serializer.dylan @@ -50,21 +50,21 @@ define method write-object (serializer :: , object :: ) write-object(serializer, as(, object)); end; -define function escape-characters (string :: ) => (string :: ) - let output = ""; - for (char :: in string) +define function escape-characters (string :: ) => (string :: ) + let output = make(limited(, of: , size: string.size * 2)); + for (char :: 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(, output) end; -define method write-object (serializer :: , object :: ) +define method write-object (serializer :: , object :: ) let stream = serializer.stream; write(stream, "\""); write(stream, escape-characters(object));