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

Add option to return objects as Maps to support ordered properties #207

Open
contact-ouyang opened this issue Aug 28, 2019 · 5 comments
Open
Assignees

Comments

@contact-ouyang
Copy link

I understood that the json object is designed to be unordered, and json arrays will preserve order. So if I would like to preserve the order , then I should use list instead of object, but it is not convenient if I have an OrderedDictionary in the backend.

Now I am using c# for backend, in c#, it has OrderedDictionary which will preserve the order. After I marshal the OrderedDictionary to json, the order will be lost. When I debug the code, I found the OrderedDictionary was totally fine , the only problem was JSON changed the order of the key.

After I google about how to solve the problem, I realized that I am not the only one who asked about how to keep the order of keys in json object. Most backend languages have something similar to OrderedList, when they marshal to json , they have the same problems, and all the solutions say that we should use array instead of object.

Can we have a new type of object to keep the order of the keys so that the OrderedDictionary can marshal to json with preserved order? Thanks.

@jordanbtucker
Copy link
Member

You bring up some very interesting points, but are you sure you're posting in the right place. This is the repository for JSON5, a version of JSON that's easier for humans to read and write, but you don't mention JSON5 in your post anywhere.

That being said, it might be interesting to include an option in this library that returns a Map in place of each Object when parsing, and accepts Maps in place of Objects when stringifying. A Map is JavaScript's version of an ordered dictionary, however the keys of a Map can be non-string values. JSON.stringify treats Maps as empty objects.

@contact-ouyang
Copy link
Author

Thank you for your reply. Sorry that I might post the question in the wrong place. I thought JSON5 is improved JSON, I might have misunderstood the purpose of JSON5 library, maybe JSON5 is more about easier configuration purpose, I guess.

It is great If I have an additional type Map in JSON/JSON5 to exchange data, then I don't need to think about transform OrderedList to an Array (to remember the order) + an Object (to remember the key value).

@h-h-h-h
Copy link

h-h-h-h commented Jun 3, 2022

@jordanbtucker:

I'd also like to request the option to preserve the order with parse() and stringify() by using Maps. The npm package json-in-order also does this.

This is useful when deriving locale files with string IDs from a main locale file and you want changes like reordering strings or inserting a new string in a specific context to be transferred to the derived files.

If this feature becomes available, editing software like translation webapps could also start to uphold the order.

@jordanbtucker jordanbtucker changed the title Can we have a new type of object to support ordered keys? Return objects as Maps to support ordered properties Jun 3, 2022
@jordanbtucker jordanbtucker changed the title Return objects as Maps to support ordered properties Add option to return objects as Maps to support ordered properties Jun 3, 2022
@jordanbtucker jordanbtucker reopened this Jun 3, 2022
@jordanbtucker
Copy link
Member

Reopening this issue to accommodate more discussion.

@h-h-h-h
Copy link

h-h-h-h commented Jun 3, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants