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

Keys in JSONObject are not maintaining their insertion order #920

Closed
coding-er opened this issue Dec 11, 2024 · 1 comment
Closed

Keys in JSONObject are not maintaining their insertion order #920

coding-er opened this issue Dec 11, 2024 · 1 comment

Comments

@coding-er
Copy link

Hi,

I've encountered an issue with org.json.JSONObject where the order of key-value pairs is not preserved when constructing JSON objects. This is problematic for my use case, as the frontend relies on the specific order of the data.

After examining the source code, I noticed that org.json.JSONObject uses HashMap internally, which does not maintain the insertion order of keys. I have two main questions regarding this issue:

Preservation of Insertion Order: Are there any strategies or plans to ensure the insertion order of key-value pairs in org.json.JSONObject?

Design Philosophy: What was the rationale behind using HashMap for org.json.JSONObject, given that it doesn't preserve order? Is there an intention to support ordered JSON in future versions of the library?

Thank you for your attention to this matter.

@stleary
Copy link
Owner

stleary commented Dec 11, 2024

@coding-er

Are there any strategies or plans to ensure the insertion order of key-value pairs in org.json.JSONObject?
No

What was the rationale behind using HashMap for org.json.JSONObject, given that it doesn't preserve order?
This project follows the JSON spec, which states that,
"An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array."

Is there an intention to support ordered JSON in future versions of the library?
No

This is a frequent question, so I am keeping issue #822 open for discussion. You can continue making your case there, if you want. Otherwise, your best bet is to fork the repository and implement support for ordering yourself, or use a more permissive library.

@stleary stleary closed this as completed Dec 11, 2024
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

2 participants