Reputation: 2852
Are there any strict rules for conversion between JSON and XML?
Program I am working on should be able to output results in both formats, but among all the possible conversion utilities, libraries etc. I could not understand if there are any standard (possibly, "de-facto") for this conversion.
Common problems, as I see, are:
conversion from one format to other and then back should give result identical to original;
JSON have arrays - simple nesting of similar child items in the XML would not do, since some XML-processing tools would not preserve the order;
XML have attributes - their representation as a child items would change original XML when converting back.
Can I found any documentation on this question - or I may use any suitable converter because the lack of such standard?
Thank you in advance for links, advices, guides.
Upvotes: 6
Views: 1206
Reputation: 3601
Yes it possible. I think all your concerns could be handled cleaning if you defined how they are to be processed. I don't know of a standard way of doing it.
I think this is very telling in how you would need to go about it http://jsontoxml.utilities-online.info/
Basically You'd "encode" attributes and text data with a way to signify what's data, what's an attribute, etc. Pretty intresting and I think playing with this tool will give you some ideas of out to create a ruleset that will work for you specifications.
Biggest thing if you go forward with this is to document how the processing works and what is expected.
Upvotes: 0
Reputation: 17648
No... There is no strict rule as of yet.
As you imply... Although JSON can be converted to XML, the conversion cannot be robust , because XML tag lists are not, by definition, coupled to any particular data structure, where as JSON data structures are (maps and lists). Thus... JSON files , if converted to XML, cannot be losslessly converted back to JSON (unless of course you embed some nonstandard meta information in the JSON objects which are used for the XML decoding).
Upvotes: 1
Reputation: 63126
Personally a lot of this is going to really depend on your specifics for implementation. But in the end, the real key should be in the actual relationships that you have...
As really doing anything that tried to do an arbitrary conversion from XML -> JSON or vice-versa would be really hard to manage/process as you outline. But if you have a common object model in the middle, you should be fine.
Therefore, as long as your Serialization and Deserialization methods work for the respective object types, the actual processes should not have any issues.
Upvotes: 0