Note. I am an EclipseLink JAXB (MOXy) and a member of the JAXB Group (JSR-222) .
The following is an example of how this can be done using MOXy.
Person
MOXy will disable the Person object as you want, without specifying any metadata. To make the output JSON contain null values, you can use the annotation @XmlElement(nillable=true) (see binding to JSON and XML - processing a null value ).
package forum8748537; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) public class Person { @XmlElement(nillable=true) Integer id; @XmlElement(nillable=true) String fname; @XmlElement(nillable=true) Region regions; }
jaxb.properties
To specify MOXy as your JAXB provider (JSR-222), you need to add a file named jaxb.properties in the same package as the domain classes, with the following entry (see Specifying EclipseLink MOXy as a JAXB Provider ).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum8748537; import java.io.StringReader; import java.util.*; import javax.xml.bind.*; import javax.xml.transform.stream.StreamSource; public class Demo { public static void main(String[] args) throws Exception { Map<String, Object> properties = new HashMap<String, Object>(2); properties.put("eclipselink.media-type", "application/json"); properties.put("eclipselink.json.include-root", false); JAXBContext jc = JAXBContext.newInstance(new Class[] {Person.class}, properties); StringReader json = new StringReader("{\"id\":null,\"fname\":\"John\",\"region\":\"\"}"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Person person = unmarshaller.unmarshal(new StreamSource(json), Person.class).getValue(); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(person, System.out); } }
Exit
{ "id" : null, "fname" : "John", "regions" : null }
Using MOXy in a JAX-RS Application
An example of using MOXy in a JAX-RS application: