views:

47

answers:

1

We are building a webservice which consumes and produces JSON.

Problem: We are kind of confused about how to represent some specific details of a object.

Example: we specify any empty element or null element as null in JSON

The question we ask our self is how do we represent a object when its properties are all null.

example: car is a object and color and make are properties, which are null.

We represent it as

 car :
                             {
                              "color": null,
                              "make" : null
                             }

Some people argue that it should be represented as

               car : null

So lets say, car : null is the right way to do it. car is not an exact representation of the domain object. its a piece of a domain object, lets call it big car. So we make the car view object in some kind of translator. So we find a painful way of checking each field in the car object and if they are null, then set the car as null, so jackson jaxb provider can render it as null. I know that's not the right way to do it.

Can anyone suggest an alternative??

is there one place where we can check, is there any view object with all children null?? and then set it to null.

which way is more consumable??

A: 

If I understand correctly there are three different situations that you are trying to represent. One is that there IS NO car, the other is that there IS a car but we don't know anything about it, a final one is that there IS a car and we know something about it (perhaps everything about it). So there are three possible situations:

(1) Ricardo does not have a car

'ricardo': {
  'car': null
}

(2) Ricardo has a car but we don't know anything about it

'ricardo' : {
  'car' : {
    'color' : null,
    'make' : null
   }
 }

(3) Ricardo has a car and we know something about it (or everything)

'ricardo' : {
  'car' : {
    'color' : 'red',
    'make' : 'ferrari'
   }
}

You need to be sure of what you need to model and do the representation accordingly. Check here for the definitive reference.

rmarimon
Hi marimon,lets say car is a sub-object of big car. I know something about the big car, but what i want for a car is not there in big car. can this be represented as the second approach?? that's what we are doing.. but, is that right?And also how do we check that we dont know anything about the car?it cannot be like a sequence of not null checks... is there any other efficient way?
Vanchinathan Chandrasekaran