Which Java JSON libraries are good at repeating JAXB annotations?

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

Can someone recommend some Java JSON libraries that repeat JAXB annotations well? Lightweight libraries are preferred.

+6
source share
3 answers

Note. I am EclipseLink JAXB (MOXy) , and a member of the JAXB Expert Group ( JSR-222 ).

Check out the JSON binding added to the EclipseLink JAXB (MOXy). We not only use JAXB annotations, but also use 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); } } 

In addition to JAXB annotations, MOXy extensions (such as @XmlPath) are supported, which makes it easy to create one annotated model that can be used for both XML and JSON:

+4
source

I would use Jackson . It seems to have good JAXB support out of the box.

Reference:

+3
source

Apache Jersey has decent JAXB support. I would recommend you take a look at the following discussion regarding his internal related posts.

+2
source

Source: https://habr.com/ru/post/896706/


All Articles