Skip to content
New issue

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

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

Already on GitHub? # to your account

cjson.encode() modified order #66

Open
yhc19850706 opened this issue Nov 9, 2020 · 7 comments
Open

cjson.encode() modified order #66

yhc19850706 opened this issue Nov 9, 2020 · 7 comments

Comments

@yhc19850706
Copy link

when i use cjson.encode(table), the table is modified order ,why?
e.g:
local json = cjson.encode({name="yhc",age=40,birthday="1985"})

print(json) -- {"birthday":"1985","name":"yhc","age":40}

i don't want to change,how to do?

@dannygueta
Copy link

Hi,

same problem here, any lead ?

Thanks.

@libing0526
Copy link

I hava the same problem,anybody knows why?

@doujiang24
Copy link
Member

cjson.encode can not have the fixed order since Lua table doesn't have a fixed order for keys.
otherwise, cjson.encode have to sort for the keys, it will hit bad performance then.

@travisbell
Copy link

travisbell commented Sep 10, 2021

There is a PR on the original mpx/lua-cjson repo here, that adds support for this. It only took a few small tweaks to get working with the latest openresty/lua-cjson. I have a working branch of this here.

Just configure OpenResty with the --without-lua_cjson flag, and then you can just build and require your own custom cjson (I'm using LuaRocks for this) in your project. I've been using this in production for almost a year with no problems. Performance is still great.

@libing0526
Copy link

There is a PR on the original mpx/lua-cjson repo here, that adds support for this. It only took a few small tweaks to get working with the latest openresty/lua-cjson. I have a working branch of this here.

Just configure OpenResty with the --without-lua_cjson flag, and then you can just build and require your own custom cjson (I'm using LuaRocks for this) in your project. I've been using this in production for almost a year with no problems. Performance is still great.

This is a good method, but it seems that it can only be applied to a small amount of data. The number of citations Lua stack is limited to 20

@libing0526
Copy link

https://plain.blog.csdn.net/article/details/120364853
it seems like a good method

@edo888
Copy link

edo888 commented Apr 16, 2022

You can possibly define the order you want in __order metatable and use the forked version: https://github.com/edo888/lua-cjson

local cjson = require "cjson.safe"
cjson.decode_save_key_order(true)

local json = {name="yhc",age=40,birthday="1985"}
setmetatable(json, {
    __order = {"name", "age", "birthday"}
})
print(cjson.encode(json))

Output:

{"name":"yhc","age":40,"birthday":"1985"}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants