Display null for objects -JSON- JAXB
I want to marshal null objects as n开发者_开发问答ull in the JSON representation. But, right now, Am not seeing the element in the JSON if the object is null.
Example:
@XmlAccessType(FIELD)
@XmlType(name="foo" propOrder={"foo"}
class foo{
@XmlElement
private Integer foo;
private Integer another_foo;
..
getter()
setter()
}
In my code am setting foo element to null.
But the JSON representation does not show the element in the response.
The response looks like this
"foo" :{
"another_foo":something
}
I tried setting xml element attribute nillable true. (@XmlElement(nillable=true)
Which makes the response look like,
"foo" :{
"another_foo" : something
"foo":{nil :true}
}
I want it to be like,
"foo" :{
"another_foo":something
"foo" : null
}
What am doing wrong here?
First things first: JAXB does NOT do JSON. It is an XML API. So you are probably using a framework (my guess: JAX-RS implementation like maybe Jersey)? It is necessary to know which one you are using to give more help.
Assuming this, question is, how is the package using JAXB annotations to guide JSON serialization. Some basically convert objects to XML (either logical structure, or full xml), and then convert to JSON using a convention. This may cause data loss because of differences in data model between XML and JSON.
Now: simple solution for most JAX-RS implementations is to not use JAXB annotation based approach at all, but a JSON-specific serializer, such as Jackson's JacksonJsonProvider (Jackson can actually use JAXB annotations, too). It will by default include null values for properties, although this is configurable in case you want to suppress nulls.
Here is JavaDoc that shows how to use Jackson provider (specify FEATURE_POJO_MAPPING for configuration) with Jersey, if that might help.
i think the functionality of json is like that only, it will not show you null values in the Json string. Suppose an example, you have an object with two attributes say age and name. Now if the age is 7 and name is null, then if according to you, name should also be included in the JSON object then, u must right the code to explicit handle the deserialization of the same code. then you will have to handle the null value thing as JSON will treat "null" as a string and will assign it to the object which will make your object with name="null", which is wrong.
If you want to achieve the same, then you must extend the class JSON object and write your own implementation.
Note: I'm the EclipseLink JAXB (MOXy) lead and member of the JAXB (JSR-222) expert group.
EclipseLink JAXB (MOXy) offers native support for JSON. Below is an example of how the @XmlElement
annotation can be leveraged to map your use case:
Foo
By default a JAXB implementation will not marshal a null property. You can change this behaviour by setting the nillable
property on @XmlElement
to true.
package forum3938279;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
class Foo {
@XmlElement(nillable=true)
private Integer foo;
private Integer another_foo;
}
jaxb.properties
To specify MOXy as your JAXB provider you must include a file called jaxb.properties
in the same package as your domain model with the following entry:
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum3938279;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Foo foo = new Foo();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.marshal(foo, System.out);
}
}
Output
Below is the output from running the demo code. Both fields had null values. The field without the @XmlElement
annotation was not marshalled, and the one with @XmlElement(nillable=true)
was marshalled as expected in JSON.
{
"foo" : null
}
For More Information
- http://blog.bdoughan.com/2011/08/json-binding-with-eclipselink-moxy.html
精彩评论