From 3ecf8e9b0b97c0a55279fcd0a41e3771861363ee Mon Sep 17 00:00:00 2001 From: Jonas Tarnstrom Date: Wed, 14 Dec 2011 16:02:49 +0100 Subject: [PATCH] Added support for encoding and escaping NULL (U+0000) as \\u0000 --- python/tests.py | 15 +++++++++++++++ python/version.h | 2 +- ultrajsonenc.c | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/python/tests.py b/python/tests.py index c0b5f5b..639d34d 100644 --- a/python/tests.py +++ b/python/tests.py @@ -456,6 +456,21 @@ class UltraJSONTests(TestCase): except OverflowError: pass + def test_encodeNullCharacter(self): + input = "31337 \x00 1337" + output = ujson.encode(input) + + self.assertEquals(input, json.loads(output)) + self.assertEquals(output, json.dumps(input)) + self.assertEquals(input, ujson.decode(output)) + self.assertEquals('" \\u0000\\r\\n "', ujson.dumps(u" \u0000\r\n ")) + pass + + def test_decodeNullCharacter(self): + input = "\"31337 \\u0000 31337\"" + self.assertEquals(ujson.decode(input), json.loads(input)) + + def test_encodeListLongConversion(self): input = [9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807, 9223372036854775807 ] output = ujson.encode(input) diff --git a/python/version.h b/python/version.h index 3ec0105..f35ee71 100644 --- a/python/version.h +++ b/python/version.h @@ -1 +1 @@ -#define UJSON_VERSION "1.12" +#define UJSON_VERSION "1.14" diff --git a/ultrajsonenc.c b/ultrajsonenc.c index f051fee..8e03631 100644 --- a/ultrajsonenc.c +++ b/ultrajsonenc.c @@ -134,8 +134,21 @@ int Buffer_EscapeStringUnvalidated (JSOBJ obj, JSONObjectEncoder *enc, const cha switch (*io) { case 0x00: - enc->offset += (of - enc->offset); - return TRUE; + if (io < end) + { + *(of++) = '\\'; + *(of++) = 'u'; + *(of++) = '0'; + *(of++) = '0'; + *(of++) = '0'; + *(of++) = '0'; + break; + } + else + { + enc->offset += (of - enc->offset); + return TRUE; + } case '\"': (*of++) = '\\'; (*of++) = '\"'; break; case '\\': (*of++) = '\\'; (*of++) = '\\'; break; @@ -213,8 +226,22 @@ int Buffer_EscapeStringValidated (JSOBJ obj, JSONObjectEncoder *enc, const char { case 0: { - enc->offset += (of - enc->offset); - return TRUE; + if (io < end) + { + *(of++) = '\\'; + *(of++) = 'u'; + *(of++) = '0'; + *(of++) = '0'; + *(of++) = '0'; + *(of++) = '0'; + io ++; + continue; + } + else + { + enc->offset += (of - enc->offset); + return TRUE; + } } case 1: