-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Sorted XML elements after conversion #508
Comments
This is not likely to be fixed unless someone else is willing to provide a pull request that let's the XML builder know what order to spit out the JSON object keys. Changing the Please check up on the FAQ as well under the wiki pages: https://github.com/stleary/JSON-java/wiki |
Mhhhh ... looking deeper it seems to me that order also might be a topic for the JSON side.
Is that something you could agree from its idea? |
That exposes implementation details, so off the top of my head it doesn't seem like a valid solution. We also still have the "compiles to java6" limitation. A similar solution would be to have a JSONConfig object like you suggest, but instead have an /**
* Configuration for how JSON data is parsed and maintained.
*/
public class JSONConfig {
public static enum KeyOrder {
/** Key Order is standards compliant (not ordered). This is the default.*/
StandardsCompliant, // internally would use HashMap
/**
* Key Order will be maintained by insertion order. This is not standards
* compliant; meaning that code that uses this option may be incompatible
* with other Standards Conforming systems.
*/
InsertionOrder, // internally would use LinkedHashMap
/**
* Key order will be maintained in alphabetical order as defined by
* {@link String#Compare(String, String)}. This is not standards compliant;
* meaning that code that uses this option may be incompatible with other
* Standards Conforming systems.
*/
AlphabeticalOrder, // internally would use TreeMap
}
/**
* Configures how JSON objects keep track of keys. Defaults to
* {@link KeyOrder#StandardsCompliant}.
*/
public static KeyOrder JSONObjectKeyOrder;
private JSONConfig(){}
static {
// the default should always be standards compliant.
JSONObjectKeyOrder = KeyOrder.StandardsCompliant;
}
}
// somewhere in your own code....
JSONConfig.JSONObjectKeyOrder = JSONConfig.KeyOrder.InsertionOrder; This also opens up possibilities for finishing the changes in #453 to maintain backwards compatibility. |
Another more aggressive but configurable option would be to use an instance class for the configuration but have a static GLOBAL option. Then you could pass individual configurations to parsers as needed. If you only needed to change the settings once for the whole application you could, but if you wanted to you could just change it for a single object. Maybe something like: // change the global default
JSONConfig.GLOBAL.JSONObjectKeyOrder = JSONConfig.KeyOrder.InsertionOrder;
// custom configuration that can be passed into certain functions.
JSONConfig myJsonConfig = new JSONConfig();
myJsonConfig.JSONObjectKeyOrder = JSONConfig.KeyOrder.AlphabeticalOrder;
// parse the JSON using a given configuration.
JSONObject jo = new JSONObject(myJsonString, myJsonConfig); |
Closed for lack of activity. |
Hi,
When converting JSON to XML with your library we have the problem that elements in XML do not appear in same order as in the JSON. Reason for it is following code:
https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L184
At this location as well as the other locations where you also create an instance of HashMap means that there is NO order.
Could you please provide a policy which control the order of the elements?
PS: Also on stackoverflow the problem has been detected: https://stackoverflow.com/questions/26034370/inverted-order-of-json-elements-in-java-after-xml-conversion but the solution to change the code is not an acceptable option for us.
Kind Regards,
Thomas
The text was updated successfully, but these errors were encountered: