kazuho / picojson

a header-file-only, JSON parser serializer in C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maintaining order of values when serializing.

Ender1618 opened this issue · comments

Would it be possible to add support to maintain the original order of json object value entries, as they were specified int he original json that was parsed, when serializing a json DOM?

I have json object values ordered to be more comprehensible when input into the parser, but when I serialize the same DOM they come out in lexicographic order. I know this has to do with using std maps as json objects, since traversing those with the map iterator will yield a sorting by key, but it may be possible.

👍 to this feature request ... for my application I'd also much prefer to get a JSON object entries in the order in which I put them in.
Not sure how to implement this though ... ?

Sounds like a reasonable request to me.

What I would like to ask in turn is:

  • do you know an associative container that fits your need (that remembers the order in which the key-value pairs were added)?
  • would you mind checking if the container works fine with picojson, by changing the type of picojson::value::object (defined in line 112) to the container?

Once the two questions are met and if you do not want to maintain the altered version of picojson (as described in the second point), I will extend the type-traits mechanism (proposed in #52) to cover the object type as well and merge it to master.

I don't know which container to use.

This stackoverflow post suggests using boost.multiindex. I don't have time to look at how this works and if something small can be bundled with picojson.

This stackoverflow post suggests using boost.multiindex. I don't have time to look at how this works and if something small can be bundled with picojson.

Thank you for the info.

In this case, You do not need to worry about the size of the container implementation, since there is no need to bundle it with picojson. #52 is a proposal of providing a interface for the users to specify the value types at compile time.