views:

22

answers:

2

Hi All,

I want to marshal null objects as null 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?

+1  A: 

Hi, 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.

Mrityunjay
No, JSON has explicit null value, so this is not due to JSON. Rather it is due to whatever processes JAXB annotations, and how it chooses to handle null values of Java objects.
StaxMan
+1  A: 

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.

StaxMan
Thanks Staxman, Yes, We are using Jackson JSON provider. I was thinking that there should be a way out with the JAXB annotations.I'll try this out.
Vanchinathan Chandrasekaran
Ah. Maybe Jersey just defaults to configuring ObjectMapper to suppress nulls? While default setting is not to do this, Jersey may choose to override this keep behavior similar to other serializers. Anyway, method to call is within SerializationgConfig (setSerializationInclusion), accessible using ObjectMapper.getSerializationConfig(). There is also annotation @JsonSerialize that can be used to define per-class inclusion (all, non-null, non-defaults).
StaxMan