From b5e364c7c60167995944ed3a3b9c54d9a377fc1d Mon Sep 17 00:00:00 2001 From: Thibault Charbonnier Date: Sat, 5 Aug 2017 15:19:43 -0700 Subject: feature: set cjson.array_mt on decoded JSON arrays. this can be turned on via cjson.decode_array_with_array_mt(true). off by default. Signed-off-by: Yichun Zhang (agentzh) --- tests/agentzh.t | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 6 deletions(-) (limited to 'tests') 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)) -=== TEST 12: array_mt on tables with hash part +=== TEST 12: decode() by default does not set array_mt on empty arrays +--- lua +local cjson = require "cjson" +local json = [[{"my_array":[]}]] +local t = cjson.decode(json) +local has_metatable = getmetatable(t.my_array) == cjson.array_mt +print("decoded JSON array has metatable: " .. tostring(has_metatable)) +print(cjson.encode(t)) +--- out +decoded JSON array has metatable: false +{"my_array":{}} + + + +=== TEST 13: decode() sets array_mt on non-empty arrays if enabled +--- lua +local cjson = require "cjson" +cjson.decode_array_with_array_mt(true) +local json = [[{"my_array":["hello","world"]}]] +local t = cjson.decode(json) +t.my_array.hash_value = "adding a hash value" +-- emptying the array part +t.my_array[1] = nil +t.my_array[2] = nil +local has_metatable = getmetatable(t.my_array) == cjson.array_mt +print("decoded JSON array has metatable: " .. tostring(has_metatable)) +print(cjson.encode(t)) +--- out +decoded JSON array has metatable: true +{"my_array":[]} + + + +=== TEST 14: cfg can enable/disable setting array_mt +--- lua +local cjson = require "cjson" +cjson.decode_array_with_array_mt(true) +cjson.decode_array_with_array_mt(false) +local json = [[{"my_array":[]}]] +local t = cjson.decode(json) +local has_metatable = getmetatable(t.my_array) == cjson.array_mt +print("decoded JSON array has metatable: " .. tostring(has_metatable)) +print(cjson.encode(t)) +--- out +decoded JSON array has metatable: false +{"my_array":{}} + + + +=== TEST 15: array_mt on tables with hash part --- lua local cjson = require "cjson" local data @@ -175,7 +224,7 @@ print(cjson.encode(data)) -=== TEST 13: multiple calls to lua_cjson_new (1/3) +=== TEST 16: multiple calls to lua_cjson_new (1/3) --- lua local cjson = require "cjson" package.loaded["cjson"] = nil @@ -187,7 +236,7 @@ print(cjson.encode(arr)) -=== TEST 14: multiple calls to lua_cjson_new (2/3) +=== TEST 17: multiple calls to lua_cjson_new (2/3) --- lua local cjson = require "cjson" package.loaded["cjson"] = nil @@ -199,7 +248,7 @@ print(cjson.encode(arr)) -=== TEST 15: multiple calls to lua_cjson_new (3/3) +=== TEST 18: multiple calls to lua_cjson_new (3/3) --- lua local cjson = require "cjson.safe" -- load another cjson instance (not in package.loaded) @@ -211,7 +260,7 @@ print(cjson.encode(arr)) -=== TEST 16: & in JSON +=== TEST 19: & in JSON --- lua local cjson = require "cjson" local a="[\"a=1&b=2\"]" @@ -222,7 +271,7 @@ print(cjson.encode(b)) -=== TEST 17: default and max precision +=== TEST 20: default and max precision --- lua local math = require "math" local cjson = require "cjson" -- cgit v1.2.3-55-g6feb