3

I've been searching to no avail for a set of routines to do conversion between JSON and XML. I have found such routines in Javascript, Java, PHP, and Python, but not in C or C++.

FWIW, my json library is json-spirit. I am currently processing JSON, and would like to add XML support via a conversion layer (convert incoming messages from XML to JSON, process them, convert results back to XML, and them out).

Does anyone have any pointers?

I've also seen a number of references to badgerfish, rayfish, rabbitfish... encoding conventions, but they seem to point to dead URLs. Is there a reference somewhere which describes each convention?

And yes, I've checked on json.org.

1
  • To add part answer to my question, rayfish, badgerfish, et. Al are described here: bramstein.com/projects/xsltjson Commented Sep 13, 2011 at 2:47

3 Answers 3

3

By far, the only specifically designed C++ library that directly converts XML to JSON I found on the Internet is xml2json: https://github.com/Cheedoong/xml2json

You can also convert JSON to XML if following the same rules.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you have example of json to xml in your library ?
How can you convert JSON to XML using that library?
No, you cannot really simply convert the other way around, library does not support it out-of-box. Sure, you can write it yourself applying the same code as the lib, but that's not the question
3

Boost.PropertyTree handles both JSON and XML. There are some quirks in their implementations, so it wouldn't be a direct transformation, but it shouldn't need much work to adapt a property_tree between JSON and XML.

3 Comments

Something to keep in mind -- Boost.PropertyTree does not have a proper XML parser; it only supports the subset of XML that it's capable of generating, which is very, very little.
Something else to keep in mind. Boost.PropertyTree does not have a decent JSON writer (or at least the version I'm using doesn't). Property tree doesn't track types so everything comes out as strings, and not all property trees can be output as json, which means you may have XML you can read, but cant then save as JSON.
So what is a more reliable alternative?
1

You could write a xslt for your xml document to convert to json. But I see no standard jslt for converting json.

2 Comments

Yes, I've seen that solution, but apart from having to bring in an XML library now (not a big deal, i am prepared to do that), it also means that I need to come up with something for json->xml. I can do the xml->json just fine, but json->xml is a bit challenging for me.
XSLT 3.0 also offers JSON to XML conversion: w3.org/TR/xslt-30/#json-to-xml-mapping

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.