From 2123c622de3a527b6173f6826bf8ceb21ed840fd Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Tue, 10 May 2011 20:11:05 +0930 Subject: Remove whitespace from generated JSON output Remove excess whitespace to reduce output size and increase encode performance. Suggested by: Zhang "agentzh" Yichun --- NEWS | 1 + lua_cjson.c | 16 ++++++++-------- tests/test.lua | 10 +++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/NEWS b/NEWS index a016d7b..fb71cec 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,6 @@ Version 1.0.1 (May 10 2011) * Added build support for OSX +* Removed unnecessary whitespace from JSON output Version 1.0 (May 9 2011) * Initial release. diff --git a/lua_cjson.c b/lua_cjson.c index fa2d6a7..63b3d06 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -500,12 +500,12 @@ static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json, json_encode_descend(l, cfg); - strbuf_append_mem(json, "[ ", 2); + strbuf_append_char(json, '['); comma = 0; for (i = 1; i <= array_length; i++) { if (comma) - strbuf_append_mem(json, ", ", 2); + strbuf_append_char(json, ','); else comma = 1; @@ -514,7 +514,7 @@ static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json, lua_pop(l, 1); } - strbuf_append_mem(json, " ]", 2); + strbuf_append_char(json, ']'); cfg->current_depth--; } @@ -538,14 +538,14 @@ static void json_append_object(lua_State *l, json_config_t *cfg, json_encode_descend(l, cfg); /* Object */ - strbuf_append_mem(json, "{ ", 2); + strbuf_append_char(json, '{'); lua_pushnil(l); /* table, startkey */ comma = 0; while (lua_next(l, -2) != 0) { if (comma) - strbuf_append_mem(json, ", ", 2); + strbuf_append_char(json, ','); else comma = 1; @@ -554,10 +554,10 @@ static void json_append_object(lua_State *l, json_config_t *cfg, if (keytype == LUA_TNUMBER) { strbuf_append_char(json, '"'); json_append_number(l, json, -2, cfg->encode_refuse_badnum); - strbuf_append_mem(json, "\": ", 3); + strbuf_append_mem(json, "\":", 2); } else if (keytype == LUA_TSTRING) { json_append_string(l, json, -2); - strbuf_append_mem(json, ": ", 2); + strbuf_append_char(json, ':'); } else { json_encode_exception(l, -2, "table key must be a number or string"); @@ -570,7 +570,7 @@ static void json_append_object(lua_State *l, json_config_t *cfg, /* table, key */ } - strbuf_append_mem(json, " }", 2); + strbuf_append_char(json, '}'); cfg->current_depth--; } diff --git a/tests/test.lua b/tests/test.lua index b1395b0..1408bb9 100755 --- a/tests/test.lua +++ b/tests/test.lua @@ -83,7 +83,7 @@ local encode_simple_tests = { { json.encode, { json.null }, true, { 'null' } }, { json.encode, { true }, true, { 'true' } }, { json.encode, { false }, true, { 'false' } }, - { json.encode, { { } }, true, { '{ }' } }, + { json.encode, { { } }, true, { '{}' } }, { json.encode, { 10 }, true, { '10' } }, { json.encode, { NaN }, false, { "Cannot serialise number: must not be NaN or Inf" } }, @@ -117,15 +117,15 @@ local encode_table_tests = { return "Setting sparse array (true, 2, 3) / max depth (5)" end, { json.encode, { { [3] = "sparse test" } }, - true, { '[ null, null, "sparse test" ]' } }, + true, { '[null,null,"sparse test"]' } }, { json.encode, { { [1] = "one", [4] = "sparse test" } }, - true, { '[ "one", null, null, "sparse test" ]' } }, + true, { '["one",null,null,"sparse test"]' } }, { json.encode, { { [1] = "one", [5] = "sparse test" } }, - true, { '{ "1": "one", "5": "sparse test" }' } }, + true, { '{"1":"one","5":"sparse test"}' } }, - { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, + { json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } }, { json.encode, { { nested5 } }, false, { "Cannot serialise, excessive nesting (6)" } }, { json.encode, { table_cycle }, -- cgit v1.2.3-55-g6feb