aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThibault Charbonnier <thibaultcha@me.com>2017-08-05 15:19:43 -0700
committerYichun Zhang (agentzh) <agentzh@gmail.com>2017-11-17 11:46:46 -0800
commitb5e364c7c60167995944ed3a3b9c54d9a377fc1d (patch)
treeb2554317191a34b155aca68be0a54ef364296b58 /tests
parent5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (diff)
downloadlua-cjson-b5e364c7c60167995944ed3a3b9c54d9a377fc1d.tar.gz
lua-cjson-b5e364c7c60167995944ed3a3b9c54d9a377fc1d.tar.bz2
lua-cjson-b5e364c7c60167995944ed3a3b9c54d9a377fc1d.zip
feature: set cjson.array_mt on decoded JSON arrays.2.1.0.6rc1
this can be turned on via cjson.decode_array_with_array_mt(true). off by default. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/agentzh.t61
1 files changed, 55 insertions, 6 deletions
diff --git a/tests/agentzh.t b/tests/agentzh.t
index dd70fb8..7967337 100644
--- a/tests/agentzh.t
+++ b/tests/agentzh.t
@@ -153,7 +153,56 @@ print(cjson.encode(data))
153 153
154 154
155 155
156=== TEST 12: array_mt on tables with hash part 156=== TEST 12: decode() by default does not set array_mt on empty arrays
157--- lua
158local cjson = require "cjson"
159local json = [[{"my_array":[]}]]
160local t = cjson.decode(json)
161local has_metatable = getmetatable(t.my_array) == cjson.array_mt
162print("decoded JSON array has metatable: " .. tostring(has_metatable))
163print(cjson.encode(t))
164--- out
165decoded JSON array has metatable: false
166{"my_array":{}}
167
168
169
170=== TEST 13: decode() sets array_mt on non-empty arrays if enabled
171--- lua
172local cjson = require "cjson"
173cjson.decode_array_with_array_mt(true)
174local json = [[{"my_array":["hello","world"]}]]
175local t = cjson.decode(json)
176t.my_array.hash_value = "adding a hash value"
177-- emptying the array part
178t.my_array[1] = nil
179t.my_array[2] = nil
180local has_metatable = getmetatable(t.my_array) == cjson.array_mt
181print("decoded JSON array has metatable: " .. tostring(has_metatable))
182print(cjson.encode(t))
183--- out
184decoded JSON array has metatable: true
185{"my_array":[]}
186
187
188
189=== TEST 14: cfg can enable/disable setting array_mt
190--- lua
191local cjson = require "cjson"
192cjson.decode_array_with_array_mt(true)
193cjson.decode_array_with_array_mt(false)
194local json = [[{"my_array":[]}]]
195local t = cjson.decode(json)
196local has_metatable = getmetatable(t.my_array) == cjson.array_mt
197print("decoded JSON array has metatable: " .. tostring(has_metatable))
198print(cjson.encode(t))
199--- out
200decoded JSON array has metatable: false
201{"my_array":{}}
202
203
204
205=== TEST 15: array_mt on tables with hash part
157--- lua 206--- lua
158local cjson = require "cjson" 207local cjson = require "cjson"
159local data 208local data
@@ -175,7 +224,7 @@ print(cjson.encode(data))
175 224
176 225
177 226
178=== TEST 13: multiple calls to lua_cjson_new (1/3) 227=== TEST 16: multiple calls to lua_cjson_new (1/3)
179--- lua 228--- lua
180local cjson = require "cjson" 229local cjson = require "cjson"
181package.loaded["cjson"] = nil 230package.loaded["cjson"] = nil
@@ -187,7 +236,7 @@ print(cjson.encode(arr))
187 236
188 237
189 238
190=== TEST 14: multiple calls to lua_cjson_new (2/3) 239=== TEST 17: multiple calls to lua_cjson_new (2/3)
191--- lua 240--- lua
192local cjson = require "cjson" 241local cjson = require "cjson"
193package.loaded["cjson"] = nil 242package.loaded["cjson"] = nil
@@ -199,7 +248,7 @@ print(cjson.encode(arr))
199 248
200 249
201 250
202=== TEST 15: multiple calls to lua_cjson_new (3/3) 251=== TEST 18: multiple calls to lua_cjson_new (3/3)
203--- lua 252--- lua
204local cjson = require "cjson.safe" 253local cjson = require "cjson.safe"
205-- load another cjson instance (not in package.loaded) 254-- load another cjson instance (not in package.loaded)
@@ -211,7 +260,7 @@ print(cjson.encode(arr))
211 260
212 261
213 262
214=== TEST 16: & in JSON 263=== TEST 19: & in JSON
215--- lua 264--- lua
216local cjson = require "cjson" 265local cjson = require "cjson"
217local a="[\"a=1&b=2\"]" 266local a="[\"a=1&b=2\"]"
@@ -222,7 +271,7 @@ print(cjson.encode(b))
222 271
223 272
224 273
225=== TEST 17: default and max precision 274=== TEST 20: default and max precision
226--- lua 275--- lua
227local math = require "math" 276local math = require "math"
228local cjson = require "cjson" 277local cjson = require "cjson"