Are there strict rules for converting JSON to XML and vice versa?

Are there any strict rules for converting between JSON and XML?

The program I'm working on should be able to output the results in both formats, but among all the possible conversion utilities, libraries, etc. I could not understand if there was any standard (possibly de facto) for this conversion.

Common problems, as I see it, are the following:

  • conversion from one format to another, and then back should give a result identical to the original;

  • JSON has arrays - a simple nesting of such child elements in XML will not be executed, since some XML processing tools did not preserve order;

  • XML has attributes - representing them as children will change the source XML when the transformation is inverse.

Is it possible to find any documentation on this issue - or can I use any suitable converter, because there is no such standard?

Thank you in advance for links, tips, recommendations.

+6
source share
3 answers

No ... There is currently no strict rule.

As you mean ... Although JSON can be converted to XML, the conversion cannot be reliable, because XML tag lists are by definition not associated with any particular data structure, where as JSON data structures (maps and lists). Thus ... JSON files, if they are converted to XML, cannot be losslessly converted back to JSON (unless, of course, you embed some non-standard meta-information in JSON objects that are used to decode XML).

+1
source

Personally, many of them will really depend on your specifics for implementation. But in the end, the real key must be in the real relationship that you have ...

  • Object -> JSON
  • JSON -> Object
  • Object -> XML
  • XML -> Object

How to really do everything that tried to perform an arbitrary conversion from XML β†’ JSON or vice versa, it would be very difficult to control / process how you draw. But if you have a common object model in the middle, you should be fine.

Therefore, while your serialization and deserialization methods work for the corresponding types of objects, there should be no problems in real processes.

0
source

Yes it is possible. I think that all your problems can be fixed if you determine how to handle them. I do not know a standard way to do this.

I think this is very important in how you will need to do this http://jsontoxml.utilities-online.info/

Basically, you β€œencode” attributes and text data to indicate which data, which attribute, etc. Pretty interesting, and I think playing with this tool will give you some ideas on creating a set of rules that will work for you.

The biggest thing, if you move forward, is to document how processing works and what is expected.

0
source

Source: https://habr.com/ru/post/903728/


All Articles