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

Sorted XML elements after conversion #508

Closed
ssy-lehmann opened this issue Jan 22, 2020 · 5 comments
Closed

Sorted XML elements after conversion #508

ssy-lehmann opened this issue Jan 22, 2020 · 5 comments

Comments

@ssy-lehmann
Copy link

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

@johnjaylward
Copy link
Contributor

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 JSONObject to an ordered collection of any kind will not be accepted.

Please check up on the FAQ as well under the wiki pages: https://github.com/stleary/JSON-java/wiki

@ssy-lehmann
Copy link
Author

Mhhhh ... looking deeper it seems to me that order also might be a topic for the JSON side.
There should not be a hard coded way for the unordered nor the ordered side.
Considering (roughly) a JSONPolicies class:

  • JSONConfig.createMap() - returns by default new HashMap<String, Object>();
  • JSONConfig.registerMapCreator(...) - could be also a lambda

Is that something you could agree from its idea?

@johnjaylward
Copy link
Contributor

johnjaylward commented Jan 23, 2020

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 enum with values like StandardsCompliant, InsertionOrder.

/**
 * 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.

@johnjaylward
Copy link
Contributor

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);

@stleary
Copy link
Owner

stleary commented May 12, 2020

Closed for lack of activity.

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

3 participants