fnogatz / xsd2json

Translate XML Schema into equivalent JSON Schema
MIT License
150 stars 28 forks source link

Order gets changed while transforming #20

Closed PiQ99 closed 9 years ago

PiQ99 commented 9 years ago

Hey guy,

is there any hint, why the order gets changed for xsd-elements, that are getting transformed into a json-schema file?

I came across this problem, when the order was checked of the elements that are included in a sequence-tag. There, the order is not allowed to be changed related to the xsd-schema. In contrast to this, I know, json is not able / or dont has to maintain the same order.

Here is an example, which was converted with version 1.0.1 of xsd2json: http://jsfiddle.net/PiQ99/vz3mcm7g/

Is there anything that can be changed to have the same order? :-)

Thanks for your answer.

fnogatz commented 9 years ago

There is no standard in JSON Schema to enforce the order of properties of an object as stated in this JSON Schema mailing list discussion.

An alternative would be to translate a given xs:sequence by xsd2json not as object but ordered array. Currently an xs:sequence with the elements El1 and El2 is translated as the following (note that the order of El1 and El2 within the sub-schema is non-deterministic as it does not matter):

{
  "type": "object",
  "properties": {
    "El2": { ... },
    "El1": { ... }
  }
}

The other way to translate it into an ordered array could result in the following sub-schema:

{
  "type": "array",
  "items": [
    ... list of sub-schemas ...
  ],
  "additionalItems"
}

I've forked your example on jsfiddle with this version: https://jsfiddle.net/5p6do12r/1/

I don't like this approach as the aim of xsd2json is to translate an existing XML Schema into a JSON Schema a human would create. And I don't think one would prefer this array representation in favor to a simple object. JSON Schema simply provides no mechanism to order the properties.

PiQ99 commented 9 years ago

Thanks for your answer. Well, its the answer I, more or less, expected. Thats why I also mentioned the thing, that there is no requirement to have the same order in the json-schema like the xsd had previously.

So in the end, I have to cope with this "problem" myself.

Thank you.