Is it possible to use the Gson library to produce XML?
I am aware the gson-xml can read XML into Gson. Is there a library that lets you go the other way?
My motivation is that I have a complex application that needs to serialise to both JSON and XML and I'd ideally like to use a single serialisation library.
JSON to XML in JAVA is converted by using JSONObject json = new JSONObject(str); String xml = XML. If you have a valid dtd file or the xml file then it is very easy to transform json to xml and also xml to json.
ConclusionBoth Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.
Introduction. Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa. Gson is first constructed using GsonBuilder and then toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.
How can I keep spaces in strings when converting? Those spaces are unsignificant and would be removed by many tools. You can try adding the xml:space="preserve" attribute to the element, or enclosing the text inside a <!
I don't know if the library can serialize to both JSON and XML.
But from the same model-object, you can do it, e.g. Jackson-dataformat-xml as XML-Serializer and Jackson as JSON-serializer. And, if your application is complex enough to modularise, you should encapsulate serialization layer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With