I'm working in scala but a java solution is acceptable.
I've been using org.json to convert my json api into xml so a odd-ball client (VB4 based and unwilling to change) can consume my api in xml.
Simple json to xml conversion works fine. Problem is I likely need to be able to roundtrip it and org.json doesn't seem to convert back and forth properly.
eg
{
"a" : ""
}
converts to:
<a></a>
when I convert this back I get:
{
"a" : { }
}
So org.json converts an empty string to an empty tag, and an empty tag into an empty object. My code is below:
object XmlSerializer {
def toXml(json:String) = {
val jsonObj = new JSONObject(json)
val xmlString = XML.toString(jsonObj)
xmlString
}
def fromXml(xml:String) = {
val jsonObj = XML.toJSONObject(xml)
val jsonString = jsonObj.toString
jsonString
}
}
Am I missing something or is the org.json.XML conversion just not smart enough? Seems a type attribute could have been used to guarantee conversion back to the correct type.