diff options
| author | Thibault Charbonnier <thibaultcha@me.com> | 2016-02-28 20:03:12 -0800 |
|---|---|---|
| committer | Thibault Charbonnier <thibaultcha@me.com> | 2016-03-03 10:54:53 -0800 |
| commit | 7a9c25ee69f38974e99322971eace37ba1753074 (patch) | |
| tree | 8aded977eea3eca5f7c6ff2934615a602dacbc8f /tests | |
| parent | 77b8669c95500629dff6e34b4b6b0df5d0041ddf (diff) | |
| download | lua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.tar.gz lua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.tar.bz2 lua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.zip | |
feat: cjson.as_array metamethod to enforce empty array encoding
A proposed improved patch of openresty/lua-cjson#1 (a patch commonly
proposed to lua-cjson and its forks), taking into considerations
comments from the original PR.
- use a lightuserdata key to store the metatable in the Lua Registry
(more efficient and avoiding conflicts)
- provide a lightuserdata resulting in empty arrays as well
- tests cases moved to t/agentzh.t, where cases for
'encode_empty_table_as_object' are already written. It seems like a
better place for tests specific to the OpenResty fork's additions.
- a more complex test case
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/agentzh.t | 76 |
1 files changed, 74 insertions, 2 deletions
diff --git a/tests/agentzh.t b/tests/agentzh.t index 0b546ff..e76f910 100644 --- a/tests/agentzh.t +++ b/tests/agentzh.t | |||
| @@ -41,7 +41,79 @@ print(cjson.encode({dogs = {}})) | |||
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | === TEST 4: & in JSON | 44 | === TEST 4: empty_array userdata |
| 45 | --- lua | ||
| 46 | local cjson = require "cjson" | ||
| 47 | print(cjson.encode({arr = cjson.empty_array})) | ||
| 48 | --- out | ||
| 49 | {"arr":[]} | ||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | === TEST 5: empty_array_mt | ||
| 54 | --- lua | ||
| 55 | local cjson = require "cjson" | ||
| 56 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 57 | print(cjson.encode({arr = empty_arr})) | ||
| 58 | --- out | ||
| 59 | {"arr":[]} | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | === TEST 6: empty_array_mt and empty tables as objects (explicit) | ||
| 64 | --- lua | ||
| 65 | local cjson = require "cjson" | ||
| 66 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 67 | print(cjson.encode({obj = {}, arr = empty_arr})) | ||
| 68 | --- out | ||
| 69 | {"arr":[],"obj":{}} | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | === TEST 7: empty_array_mt and empty tables as objects (explicit) | ||
| 74 | --- lua | ||
| 75 | local cjson = require "cjson" | ||
| 76 | cjson.encode_empty_table_as_object(true) | ||
| 77 | local empty_arr = setmetatable({}, cjson.empty_array_mt) | ||
| 78 | local data = { | ||
| 79 | arr = empty_arr, | ||
| 80 | foo = { | ||
| 81 | obj = {}, | ||
| 82 | foobar = { | ||
| 83 | arr = cjson.empty_array, | ||
| 84 | obj = {} | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | print(cjson.encode(data)) | ||
| 89 | --- out | ||
| 90 | {"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":[]} | ||
| 91 | |||
| 92 | |||
| 93 | |||
| 94 | === TEST 8: empty_array_mt on non-empty tables | ||
| 95 | --- lua | ||
| 96 | local cjson = require "cjson" | ||
| 97 | cjson.encode_empty_table_as_object(true) | ||
| 98 | local array = {"hello", "world", "lua"} | ||
| 99 | setmetatable(array, cjson.empty_array_mt) | ||
| 100 | local data = { | ||
| 101 | arr = array, | ||
| 102 | foo = { | ||
| 103 | obj = {}, | ||
| 104 | foobar = { | ||
| 105 | arr = cjson.empty_array, | ||
| 106 | obj = {} | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | print(cjson.encode(data)) | ||
| 111 | --- out | ||
| 112 | {"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":["hello","world","lua"]} | ||
| 113 | |||
| 114 | |||
| 115 | |||
| 116 | === TEST 9: & in JSON | ||
| 45 | --- lua | 117 | --- lua |
| 46 | local cjson = require "cjson" | 118 | local cjson = require "cjson" |
| 47 | local a="[\"a=1&b=2\"]" | 119 | local a="[\"a=1&b=2\"]" |
| @@ -52,7 +124,7 @@ print(cjson.encode(b)) | |||
| 52 | 124 | ||
| 53 | 125 | ||
| 54 | 126 | ||
| 55 | === TEST 5: default and max precision | 127 | === TEST 10: default and max precision |
| 56 | --- lua | 128 | --- lua |
| 57 | local math = require "math" | 129 | local math = require "math" |
| 58 | local cjson = require "cjson" | 130 | local cjson = require "cjson" |
