Ondra.Zizka.cz

Viewed 4684 times

JSON with JAXB - Jackson

I wondered how various RESTful implementations (like RESTeasy) use JAXB annotations to create JSON output. The Answer is Jackson - see Using JAXB annotations with Jackson.

In a short:

Maven deps:

<!-- Jackson -->
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-lgpl</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-lgpl</artifactId>
  <version>1.3.0</version>
</dependency>
<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-xc</artifactId>
  <version>1.3.0</version>
</dependency>

And a simple test:

package cz.zizka.ondra.jacksontest;

import java.io.IOException;
import javax.xml.bind.annotation.XmlRootElement;
import org.codehaus.jackson.map.AnnotationIntrospector;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;



/**
 *
 * @author Ondrej Zizka
 */
public class JacksonTest extends TtdTestBase {

  public void testJackson() throws IOException
  {
    ObjectMapper mapper = new ObjectMapper();
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
    // make deserializer use JAXB annotations (only)
    mapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
    // make serializer use JAXB annotations (only)
    mapper.getSerializationConfig().setAnnotationIntrospector(introspector);


    mapper.writeValue( System.out, new Car("Red Devil") );
  }

}// class JacksonTest



@XmlRootElement
class Car {
  public String name;

  public Car( String name ) {
    this.name = name;
  }
}

Nice, simple, easy. Enjoy!

0
Wicket 6.9.0 Inspector Session: 2.5K Page: 12.2K Remove