Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a library to convert Java POJOs to and from JSON and XML? [closed]

Tags:

java

json

xml

pojo

I have an object graph that I would like to convert to and from JSON and XML, for the purposes of creating a REST-style API. It strikes me that someone must have done this already, but a quick search using Google and Stack Overflow reveals nothing.

Does anyone know of a suitable (Apache or equivalent license preferred) library to do this?

like image 492
Darren Hague Avatar asked Mar 18 '09 16:03

Darren Hague


People also ask

How do you convert POJO to JSON?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.


2 Answers

GSON from google : http://code.google.com/p/google-gson/,

or

Jackson the library used in spring :https://github.com/FasterXML/jackson

and I would concur with others suggesting jaxb for XML to pojo, well supported lots of tools : its the standard.

like image 196
NimChimpsky Avatar answered Sep 22 '22 08:09

NimChimpsky


For POJO to XML I suggest using JAXB (there are other libraries as well, such as XStream for example, but JAXB is standardized).

For JSON I don't know anything, but if you want to implement a RESTful API, you might be interested in JSR-311 which defines a server-side API for RESTful APIs and Jersey, which is its reference implementation.

like image 21
Joachim Sauer Avatar answered Sep 19 '22 08:09

Joachim Sauer