Preserve original hash/object keys/fields order

73 views
Skip to first unread message

Adam Strzelecki

unread,
Jan 22, 2010, 8:03:20 AM1/22/10
to jansso...@googlegroups.com
Hello,

Would be cool if Jansson has preserved order of object keys as they were read from the file or created.
The problem is that I read and write JSON based configuration using Jansson, but anytime I write configuration the objects key order is scrambled, so even to content of the json structure does NOT change, file does change.

I know 1.2 has an option to JSON_SORT_KEYS that partially solves the problem (if you want to sort, but I don't).

I know too the keys are stored in hash table so it is NOT possible to reconstruct original order just from such hash table, but simple fix would be to keep simple global counter/serial and when creating new json_t increment it and store its value in json_t, then when storing/writing to file save keys sorting by this counter/serial. Kind of JSON_PRESERVE_KEYS_ORDER :)

Cheers,
--
Adam | nanoant.com

Petri Lehtinen

unread,
Jan 23, 2010, 2:03:31 PM1/23/10
to jansso...@googlegroups.com

Would be cool indeed. I'd like to take this more generic, i.e. to
remember the order in which keys are inserted into an object. The
global counter doesn't work like this, it only helps remember in which
order the values are created. Moreover, it doesn't work at all for
true, false and null, as they are singletons (only one value of each
type ever exists).

A better approach would be to keep a per-object counter and add the
serial number to each key/value pair that's added in the object. I'll
have a better look at this at some point.

Thanks for the suggestion!

Petri

Petri Lehtinen

unread,
Feb 11, 2010, 2:02:40 PM2/11/10
to jansso...@googlegroups.com

Implemented and pushed to the master branch. Thanks again for the
suggestion!

> commit f18ef5144a77ebdbe7285711884b217b86e2c3b6
> Author: Petri Lehtinen <pe...@digip.org>
> Date: Tue Feb 9 21:29:33 2010 +0200
>
> Implement JSON_PRESERVE_ORDER encoding flag
>
> With this encoding flag, the object key-value pairs in output are in
> the same order in which they were first inserted into the object.
>
> To make this possible, a key of an object is now a serial number plus
> a string. An object keeps an increasing counter which is used to
> assign serial number to the keys. Hashing, comparison and public API
> functions were changed to act only on the string part, i.e. the serial
> number is ignored everywhere else but in the encoder, where it's used
> to order object keys if JSON_PRESERVE_ORDER flag is used.
>
> doc/apiref.rst | 8 ++++
> src/dump.c | 31 ++++++++++-----
> src/jansson.h | 1 +
> src/jansson_private.h | 8 ++++
> src/value.c | 45 +++++++++++++++++-----
> test/bin/json_process.c | 3 +
> test/suites/api/test_object.c | 36 +++++++++++++++++
> test/suites/encoding-flags/preserve-order/env | 1 +
> test/suites/encoding-flags/preserve-order/input | 1 +
> test/suites/encoding-flags/preserve-order/output | 1 +
> 10 files changed, 115 insertions(+), 20 deletions(-)

Adam Strzelecki

unread,
Feb 12, 2010, 10:19:43 AM2/12/10
to jansso...@googlegroups.com
> Implemented and pushed to the master branch. Thanks again for the
> suggestion!

Awesome. I thank you for implementing that suggestion and for all your work.

Regards,
--
Adam

Reply all
Reply to author
Forward
0 new messages