Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
As answered by esseplymale JAXB (JSR-222) covers XML not JSON binding. Howvever JAXB is frequently used directly (a JAXB impl used with something like Jettison to convert JSON to/from StAX events, see: http://blog.bdoughan.com/2011/04/jaxb-and-json-via-jettison.html) and indirectly (a JSON-binding implementation interpreting a subset of JAXB annotations) in this space.
Below is how you could accomplish your use case by leveraging EclipseLink MOXy as your JAXB provider.
Java Model
Person
Below is the Person
class from your question. I have added the @XmlAccessorType(XmlAccessType.FIELD)
annotation so that I could avoid adding the accessor methods (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
class Person {
private String desc;
}
jaxb.properties
You use MOXy as your JAXB provider you need to include a file called jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo Code
Demo
There are a few things to note in the demo code:
- The
JAXBContextProperties.MEDIA_TYPE
property is used to put MOXy in JSON-binding mode.
- The
JAXBContextProperties.JSON_INCLUDE_ROOT
property is used to tell MOXy to omit the root element.
- When the root element is omitted you need to tell MOXy what type you are unmarshalling by using one of the
unmarshal
methods that take a Class
parameter.
import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties);
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource json = new StreamSource("src/forum18417466/input.json");
Person person = unmarshaller.unmarshal(json, Person.class).getValue();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(person, System.out);
}
}
input.json/Output
{
"desc" : "abc"
}