xml: Add preserverOrder option
Imagine that I have the following XML, and the order of elements is crucial. As far as I understand, a way to handle this case does not exist. ´´´
It would be convenient to have an option to handle this use case. The JSON result will be more complex, but I will handle this use case appropriately.
It is technically already (partially) possible, though there's an open issue for that:
- #57
In JS, dictionnaries are ordered so when the parser will build the document, it'll add them in the order it found them. For the same reason, when the stringifier iterates on the object properties, it'll add them in the order you registered
You can verify this behaviour in your browser:
Reordering objects is achievable but kind of tedious
const {foo, bar} = JSON.parse(`{"foo":1, "bar":2}`)
JSON.stringify({bar, foo})
However, the mentionned issue is because when using an array element, the stringifier will iterate over it at once, effectively loosing order for this particular datatype if your elements were spread across the same node, but for the documents without array you can exploit the behaviour described above