diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-10 20:11:05 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-10 20:11:05 +0930 |
commit | 2123c622de3a527b6173f6826bf8ceb21ed840fd (patch) | |
tree | 579ec7e78d6519aa12e12104cbfc877d15f6476c | |
parent | 2323b5d0839412f276415f661e3b058b55f5bd4c (diff) | |
download | lua-cjson-2123c622de3a527b6173f6826bf8ceb21ed840fd.tar.gz lua-cjson-2123c622de3a527b6173f6826bf8ceb21ed840fd.tar.bz2 lua-cjson-2123c622de3a527b6173f6826bf8ceb21ed840fd.zip |
Remove whitespace from generated JSON output
Remove excess whitespace to reduce output size and increase encode
performance.
Suggested by: Zhang "agentzh" Yichun <agentzh@gmail.com>
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | lua_cjson.c | 16 | ||||
-rwxr-xr-x | tests/test.lua | 10 |
3 files changed, 14 insertions, 13 deletions
@@ -1,5 +1,6 @@ | |||
1 | Version 1.0.1 (May 10 2011) | 1 | Version 1.0.1 (May 10 2011) |
2 | * Added build support for OSX | 2 | * Added build support for OSX |
3 | * Removed unnecessary whitespace from JSON output | ||
3 | 4 | ||
4 | Version 1.0 (May 9 2011) | 5 | Version 1.0 (May 9 2011) |
5 | * Initial release. | 6 | * 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, | |||
500 | 500 | ||
501 | json_encode_descend(l, cfg); | 501 | json_encode_descend(l, cfg); |
502 | 502 | ||
503 | strbuf_append_mem(json, "[ ", 2); | 503 | strbuf_append_char(json, '['); |
504 | 504 | ||
505 | comma = 0; | 505 | comma = 0; |
506 | for (i = 1; i <= array_length; i++) { | 506 | for (i = 1; i <= array_length; i++) { |
507 | if (comma) | 507 | if (comma) |
508 | strbuf_append_mem(json, ", ", 2); | 508 | strbuf_append_char(json, ','); |
509 | else | 509 | else |
510 | comma = 1; | 510 | comma = 1; |
511 | 511 | ||
@@ -514,7 +514,7 @@ static void json_append_array(lua_State *l, json_config_t *cfg, strbuf_t *json, | |||
514 | lua_pop(l, 1); | 514 | lua_pop(l, 1); |
515 | } | 515 | } |
516 | 516 | ||
517 | strbuf_append_mem(json, " ]", 2); | 517 | strbuf_append_char(json, ']'); |
518 | 518 | ||
519 | cfg->current_depth--; | 519 | cfg->current_depth--; |
520 | } | 520 | } |
@@ -538,14 +538,14 @@ static void json_append_object(lua_State *l, json_config_t *cfg, | |||
538 | json_encode_descend(l, cfg); | 538 | json_encode_descend(l, cfg); |
539 | 539 | ||
540 | /* Object */ | 540 | /* Object */ |
541 | strbuf_append_mem(json, "{ ", 2); | 541 | strbuf_append_char(json, '{'); |
542 | 542 | ||
543 | lua_pushnil(l); | 543 | lua_pushnil(l); |
544 | /* table, startkey */ | 544 | /* table, startkey */ |
545 | comma = 0; | 545 | comma = 0; |
546 | while (lua_next(l, -2) != 0) { | 546 | while (lua_next(l, -2) != 0) { |
547 | if (comma) | 547 | if (comma) |
548 | strbuf_append_mem(json, ", ", 2); | 548 | strbuf_append_char(json, ','); |
549 | else | 549 | else |
550 | comma = 1; | 550 | comma = 1; |
551 | 551 | ||
@@ -554,10 +554,10 @@ static void json_append_object(lua_State *l, json_config_t *cfg, | |||
554 | if (keytype == LUA_TNUMBER) { | 554 | if (keytype == LUA_TNUMBER) { |
555 | strbuf_append_char(json, '"'); | 555 | strbuf_append_char(json, '"'); |
556 | json_append_number(l, json, -2, cfg->encode_refuse_badnum); | 556 | json_append_number(l, json, -2, cfg->encode_refuse_badnum); |
557 | strbuf_append_mem(json, "\": ", 3); | 557 | strbuf_append_mem(json, "\":", 2); |
558 | } else if (keytype == LUA_TSTRING) { | 558 | } else if (keytype == LUA_TSTRING) { |
559 | json_append_string(l, json, -2); | 559 | json_append_string(l, json, -2); |
560 | strbuf_append_mem(json, ": ", 2); | 560 | strbuf_append_char(json, ':'); |
561 | } else { | 561 | } else { |
562 | json_encode_exception(l, -2, | 562 | json_encode_exception(l, -2, |
563 | "table key must be a number or string"); | 563 | "table key must be a number or string"); |
@@ -570,7 +570,7 @@ static void json_append_object(lua_State *l, json_config_t *cfg, | |||
570 | /* table, key */ | 570 | /* table, key */ |
571 | } | 571 | } |
572 | 572 | ||
573 | strbuf_append_mem(json, " }", 2); | 573 | strbuf_append_char(json, '}'); |
574 | 574 | ||
575 | cfg->current_depth--; | 575 | cfg->current_depth--; |
576 | } | 576 | } |
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 = { | |||
83 | { json.encode, { json.null }, true, { 'null' } }, | 83 | { json.encode, { json.null }, true, { 'null' } }, |
84 | { json.encode, { true }, true, { 'true' } }, | 84 | { json.encode, { true }, true, { 'true' } }, |
85 | { json.encode, { false }, true, { 'false' } }, | 85 | { json.encode, { false }, true, { 'false' } }, |
86 | { json.encode, { { } }, true, { '{ }' } }, | 86 | { json.encode, { { } }, true, { '{}' } }, |
87 | { json.encode, { 10 }, true, { '10' } }, | 87 | { json.encode, { 10 }, true, { '10' } }, |
88 | { json.encode, { NaN }, | 88 | { json.encode, { NaN }, |
89 | false, { "Cannot serialise number: must not be NaN or Inf" } }, | 89 | false, { "Cannot serialise number: must not be NaN or Inf" } }, |
@@ -117,15 +117,15 @@ local encode_table_tests = { | |||
117 | return "Setting sparse array (true, 2, 3) / max depth (5)" | 117 | return "Setting sparse array (true, 2, 3) / max depth (5)" |
118 | end, | 118 | end, |
119 | { json.encode, { { [3] = "sparse test" } }, | 119 | { json.encode, { { [3] = "sparse test" } }, |
120 | true, { '[ null, null, "sparse test" ]' } }, | 120 | true, { '[null,null,"sparse test"]' } }, |
121 | 121 | ||
122 | { json.encode, { { [1] = "one", [4] = "sparse test" } }, | 122 | { json.encode, { { [1] = "one", [4] = "sparse test" } }, |
123 | true, { '[ "one", null, null, "sparse test" ]' } }, | 123 | true, { '["one",null,null,"sparse test"]' } }, |
124 | 124 | ||
125 | { json.encode, { { [1] = "one", [5] = "sparse test" } }, | 125 | { json.encode, { { [1] = "one", [5] = "sparse test" } }, |
126 | true, { '{ "1": "one", "5": "sparse test" }' } }, | 126 | true, { '{"1":"one","5":"sparse test"}' } }, |
127 | 127 | ||
128 | { json.encode, { nested5 }, true, { '[ [ [ [ [ "nested" ] ] ] ] ]' } }, | 128 | { json.encode, { nested5 }, true, { '[[[[["nested"]]]]]' } }, |
129 | { json.encode, { { nested5 } }, | 129 | { json.encode, { { nested5 } }, |
130 | false, { "Cannot serialise, excessive nesting (6)" } }, | 130 | false, { "Cannot serialise, excessive nesting (6)" } }, |
131 | { json.encode, { table_cycle }, | 131 | { json.encode, { table_cycle }, |