diff options
author | Thibault Charbonnier <thibaultcha@me.com> | 2017-08-05 15:19:43 -0700 |
---|---|---|
committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2017-11-17 11:46:46 -0800 |
commit | b5e364c7c60167995944ed3a3b9c54d9a377fc1d (patch) | |
tree | b2554317191a34b155aca68be0a54ef364296b58 /tests | |
parent | 5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (diff) | |
download | lua-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.t | 61 |
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 | ||
158 | local cjson = require "cjson" | ||
159 | local json = [[{"my_array":[]}]] | ||
160 | local t = cjson.decode(json) | ||
161 | local has_metatable = getmetatable(t.my_array) == cjson.array_mt | ||
162 | print("decoded JSON array has metatable: " .. tostring(has_metatable)) | ||
163 | print(cjson.encode(t)) | ||
164 | --- out | ||
165 | decoded 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 | ||
172 | local cjson = require "cjson" | ||
173 | cjson.decode_array_with_array_mt(true) | ||
174 | local json = [[{"my_array":["hello","world"]}]] | ||
175 | local t = cjson.decode(json) | ||
176 | t.my_array.hash_value = "adding a hash value" | ||
177 | -- emptying the array part | ||
178 | t.my_array[1] = nil | ||
179 | t.my_array[2] = nil | ||
180 | local has_metatable = getmetatable(t.my_array) == cjson.array_mt | ||
181 | print("decoded JSON array has metatable: " .. tostring(has_metatable)) | ||
182 | print(cjson.encode(t)) | ||
183 | --- out | ||
184 | decoded JSON array has metatable: true | ||
185 | {"my_array":[]} | ||
186 | |||
187 | |||
188 | |||
189 | === TEST 14: cfg can enable/disable setting array_mt | ||
190 | --- lua | ||
191 | local cjson = require "cjson" | ||
192 | cjson.decode_array_with_array_mt(true) | ||
193 | cjson.decode_array_with_array_mt(false) | ||
194 | local json = [[{"my_array":[]}]] | ||
195 | local t = cjson.decode(json) | ||
196 | local has_metatable = getmetatable(t.my_array) == cjson.array_mt | ||
197 | print("decoded JSON array has metatable: " .. tostring(has_metatable)) | ||
198 | print(cjson.encode(t)) | ||
199 | --- out | ||
200 | decoded 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 |
158 | local cjson = require "cjson" | 207 | local cjson = require "cjson" |
159 | local data | 208 | local 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 |
180 | local cjson = require "cjson" | 229 | local cjson = require "cjson" |
181 | package.loaded["cjson"] = nil | 230 | package.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 |
192 | local cjson = require "cjson" | 241 | local cjson = require "cjson" |
193 | package.loaded["cjson"] = nil | 242 | package.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 |
204 | local cjson = require "cjson.safe" | 253 | local 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 |
216 | local cjson = require "cjson" | 265 | local cjson = require "cjson" |
217 | local a="[\"a=1&b=2\"]" | 266 | local 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 |
227 | local math = require "math" | 276 | local math = require "math" |
228 | local cjson = require "cjson" | 277 | local cjson = require "cjson" |