Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Java JSON libraries make good reuse of JAXB annotations? [closed]

Tags:

java

json

jaxb

I am developing a project that already uses XML serialization, so I need an elegant solution to support JSON, by reusing the JAXB annotations.

Can anyone recommend some Java JSON libraries that makes a good reuse of JAXB annotations? Lightweight libraries are preferred.

like image 644
nikuco Avatar asked Sep 06 '11 11:09

nikuco


People also ask

Can Jackson use JAXB annotations?

With XML module Jackson provides support for JAXB (javax. xml. bind) annotations as an alternative to native Jackson annotations, so it is possible to reuse existing data beans that are created with JAXB to read and write XMLs.

What is the difference between Jackson and JAXB?

JAXB converts XML to java objects, and Jackson converts the same java objects to JSON.

Is JAXBContext thread safe?

JAXBContext is thread safe and should only be created once and reused to avoid the cost of initializing the metadata multiple times. Marshaller and Unmarshaller are not thread safe, but are lightweight to create and could be created per operation.

What is JAXB marshalling and Unmarshalling?

JAXB definitionsMarshalling is the process of transforming Java objects into XML documents. Unmarshalling is the process of reading XML documents into Java objects. The JAXBContext class provides the client's entry point to the JAXB API. It provides API for marshalling, unmarshalling and validating.


2 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB (JSR-222) expert group.

Check out the JSON binding being added to EclipseLink JAXB (MOXy). Not only do we leverage the JAXB annotations we also leverage the runtime APIs:

package blog.json.twitter;

import java.util.Date;    
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SearchResults.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media.type", "application/json");
        StreamSource source = new StreamSource("http://search.twitter.com/search.json?q=jaxb");
        JAXBElement<SearchResults> jaxbElement = unmarshaller.unmarshal(source, SearchResults.class);

        Result result = new Result();
        result.setCreatedAt(new Date());
        result.setFromUser("bdoughan");
        result.setText("You can now use EclipseLink JAXB (MOXy) with JSON :)");
        jaxbElement.getValue().getResults().add(result);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty("eclipselink.media.type", "application/json");
        marshaller.marshal(jaxbElement, System.out);
    }

}
  • http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html

In addition to the JAXB annotations MOXy extensions (such as @XmlPath) are supported making it even easier to have one annotated model that can be used for both XML and JSON:

  • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html?m=0
like image 140
bdoughan Avatar answered Sep 30 '22 08:09

bdoughan


I'd Use Jackson. It seems to have good JAXB support out of the box.

Reference:

  • Using JAXB annotations with Jackson
like image 24
Sean Patrick Floyd Avatar answered Sep 30 '22 08:09

Sean Patrick Floyd