Skip to content

cjson.encode() modified order #66

Closed

Description

@yhc19850706

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?

Activity

  1. dannygueta commented on Mar 1, 2021

    @dannygueta

    Hi,

    same problem here, any lead ?

    Thanks.

  2. libing0526 commented on Sep 10, 2021

    @libing0526

    I hava the same problem,anybody knows why?

  3. doujiang24 commented on Sep 10, 2021

    @doujiang24
    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.

  4. travisbell commented on Sep 10, 2021

    @travisbell

    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.

  5. libing0526 commented on Sep 13, 2021

    @libing0526

    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

  6. libing0526 commented on Sep 24, 2021

    @libing0526
  7. edo888 commented on Apr 16, 2022

    @edo888

    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"}

  8. added a commit that references this issue on Sep 20, 2025
    7819ac4
  9. added a commit that references this issue on Feb 9, 2026
    f945fde
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions