Showing posts with label Unmarshalling. Show all posts
Showing posts with label Unmarshalling. Show all posts

Sunday, 5 August 2012

JAXB JSON Marshalling / Unmarshalling

The Jackson library offers a library to process objects with JAXB annotations, but it Jackson does not support all JAXB annotations (see the bottom of this page for more information).

The code example uses the following Java class:
@XmlRootElement()
public class Recipe {

    private String title;
    private int duration;

    public String getTitle() {
        return title;
    }

    @XmlTransient
    public void setTitle(String title) {
        this.title = title;
    }

    public int getDuration() {
        return duration;
    }

    @XmlElement
    public void setDuration(int duration) {
        this.duration = duration;
    }

}
We deliberately annotate the title as transient to prove that Jackson processes JAXB annotations and does not rely on the standard Pojo to JSON processus.

The following code performs the marshalling / unmarshalling:
Recipe recipe = new Recipe();
recipe.setDuration(30);
recipe.setTitle("Chicken recipe");
        
ObjectMapper mapper = new ObjectMapper();
        
AnnotationIntrospector introspector
    = new JaxbAnnotationIntrospector();
mapper.setAnnotationIntrospector(introspector);
        
// Printing JSON
String result = mapper.writeValueAsString(recipe);
System.out.println(result);
        
// Parsing JSON
Recipe retr = mapper.readValue(result, Recipe.class);
        
System.out.println("Title   : " + retr.getTitle());
System.out.println("Duration: " + retr.getDuration());
It relies on the following dependency:
<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-jaxb-annotations</artifactId>
  <version>2.0.5</version>
</dependency>
And produces the following:
{"duration":30}
Title   : null
Duration: 30

Pojo to XML • Pojo to JSON • JAXB to XML • JAXB to JSON • JAXB Annotations Tutorial Table Of Content

JAXB XML Marshalling / Unmarshalling

We are going to perform a simple round trip to convert an JAXB annotated Java object into an XML (marshalling) and back into an XML object (unmarshalling). The code example is available on GitHub in the JAXB-JSON-XML Marshalling directory.

The following is a simple Java object with JAXB annotation:
@XmlRootElement
public class Book {

    private String title;
    private int year;

    public String getTitle() {
        return title;
    }

    @XmlElement
    public void setTitle(String title) {
        this.title = title;
    }

    public int getYear() {
        return year;
    }

    @XmlElement
    public void setYear(int year) {
        this.year = year;
    }

}
The following code performs the round trip:
    public static void main(String[] args) throws JAXBException {

        Book book = new Book();
        book.setTitle("Book title");
        book.setYear(2010);

        // Creating a Marshaller
        JAXBContext jaxbContext = JAXBContext.newInstance(Book.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter result = new StringWriter();
        jaxbMarshaller.marshal(book, result);

        // Printing XML
        String xml = result.toString();
        System.out.println(xml);

        // Creating an Unmarshaller
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        StringReader sr = new StringReader(xml);
        
        Book retr = (Book) jaxbUnmarshaller.unmarshal(sr);
        
        System.out.println("Title: " + retr.getTitle());
        System.out.println("Year : " + retr.getYear());

    }
The output is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
    <title>Book title</title>
    <year>2010</year>
</book>

Title: Book title
Year : 2010

Pojo to XML • Pojo to JSON • JAXB to XML • JAXB to JSON • JAXB Annotations Tutorial Table Of Content