Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decode 会给空的json对象添加元表 ‘json.object’, 导致table.insert(t), encode 产生错误的结果 #46

Closed
sniper00 opened this issue Feb 19, 2020 · 1 comment

Comments

@sniper00
Copy link
Contributor

sniper00 commented Feb 19, 2020

local json = require("json")
local t = {a={},b={}}
local jsonstr = json.encode(t)
local tb = json.decode(jsonstr)
table.insert(tb.a,"hello world")
print(getmetatable(tb.a))
print(json.encode(tb))--{"b":{},"a":{}}
print(json.encode(tb,{empty_table_as_array = true}))--{"b":{},"a":{}}
@xpol
Copy link
Owner

xpol commented Feb 29, 2020

This should work:

local json = require("json")
local t = {a={},b={}}
local jsonstr = json.encode(t, {empty_table_as_array = true})
local tb = json.decode(jsonstr)
table.insert(tb.a,"hello world")

@xpol xpol closed this as completed Feb 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants