Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Gson produce XML? [closed]

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.

like image 921
paj28 Avatar asked Feb 17 '14 09:02

paj28


People also ask

How do I convert JSON to XML using GSON?

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.

Is GSON better than Jackson?

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.

What does GSON toJson do?

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 do you keep spaces in the tags when converting XML to JSON in Java?

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 <!


1 Answers

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.

like image 101
yanana Avatar answered Sep 29 '22 08:09

yanana