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

A new encode_key_order option for json encoder introduced #4620

Open
TarantoolBot opened this issue Nov 4, 2024 · 0 comments
Open

A new encode_key_order option for json encoder introduced #4620

TarantoolBot opened this issue Nov 4, 2024 · 0 comments

Comments

@TarantoolBot
Copy link
Collaborator

Product: Tarantool
Since: 3.3.0

This option allows to specify the order in which the keys of object will be
serialized. To specify a particular order, it is possible to pass an
array of the keys, and resulting JSON string will contain the provided
keys in the specified order, while other present keys of an object will
be present after in some arbitrary order.

map = {a = 1, c = 100, b = 2}

require('json').encode(map)
-- returns '{"b":2,"a":1,"c":100}'

require('json').encode(map, {encode_key_order = {'a', 'b'}})
-- returns '{"a":1,"b":2,"c":100}'

require('json').(map, {encode_key_order = {'a', 'b', 'd'}})
-- returns '{"a":1,"b":2,"c":100}'

For a persistent serializer object a persistent ordering can be defined:

map_1 = {a = 1, c = 100, b = 2}

j = require('json').new()
j.cfg({encode_key_order = {'a', 'b'}})

j.encode(map_1)
-- returns '{"a":1,"b":2,"c":100}'

j.encode(map_1, {encode_key_order = {'c', 'b'}})
-- returns '{"c":100,"b":2,"a":1}'

j.encode(map_1)
-- returns '{"a":1,"b":2,"c":100}'

Requested by @grafin in tarantool/tarantool@8806b7b.

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

1 participant