-2

I am using gson.fromJson(json,class) to convert json into a Java Object. If I do not pass a parameter (other than an int) in the json, it is deserialized as null, but the int variable in the object is deserialized as 0.

So my question is, is there any annotation or any way I can use, so that if I do not pass the int parameter in the json, it is not deserialized as 0. Ideally, it would not show up at all in the object, but at the very least it would be null.

For example, the following json: {"name":"asas"}

If I have the above json, then using gson.fromJson(), will automatically deserialized id as 0.

Is there a way to eliminate that?

public class Student{

    String name;
    int id;

    //getters and setters
}
MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
Yash Bansal
  • 402
  • 5
  • 10
  • 3
    So what value would you prefer the `id` to be set to, if it's not in the JSON? – Dawood ibn Kareem Aug 21 '19 at 23:26
  • it will be great if the parameter is ignored all together, but if it is not possible then null, but I think null will also not be possible. Adding any other value breaks the code – Yash Bansal Aug 21 '19 at 23:28
  • 2
    OK, let me ask that a different way. Which of the `2^32` possible `int` values would you like to have assigned to that `int` field? `Null` is not an `int` value. `Ignored` is not an `int` value. Perhaps `int` is not the data type you're looking for. – Dawood ibn Kareem Aug 21 '19 at 23:30
  • Null would be possible if you use the `Integer` type instead of `int` on your id field, otherwise as @DawoodibnKareem says the id field has to have some value – Jose Nuno Aug 21 '19 at 23:31
  • yeah, now I realized int should not be used, but its been used and I cannot change that now. So, there is no way, we can just ignore to set the value, if it is not present in the json? – Yash Bansal Aug 21 '19 at 23:32
  • No, if it has to be `int`, you'll need to set it to _something_. The word `int` means "set aside four bytes of memory, and interpret those bytes as a number, according to certain well-understood rules". So as soon as you say `int`, there'll always be a number in there. – Dawood ibn Kareem Aug 22 '19 at 00:51

1 Answers1

1

It will be great if the parameter is ignored all together, but if it is not possible then null, but I think null will also not be possible

It's not possible to ignore the parameter because the object is defined as having the field - unlike when serializing, when you can simply not write null fields into the output JSON, deserializing requires that you create the object, and the object will have its fields, because they're what define that object. I.e., in order to ignore id if it's not present, you'd have to deserialize to a different object, without an id.

To solve your problem:

int cannot be null. It's a primitive. Primitives cannot be null.

Integer can be null. Because Integer is an Object. Change int id; to Integer id;, and if necessary edit your GSON config to get the behavior you want.

MyStackRunnethOver
  • 4,872
  • 2
  • 28
  • 42
  • Thanks a lot, so can we do it in other way. After getting the Java object, I have to convert it into xml. Is it possible to have some annotations on the field which ignores the parameter to be included in the xml if a certain value is assigned to the field. – Yash Bansal Aug 21 '19 at 23:35
  • It depends. Basically any framework will allow you to ignore `null` values when serializing. If you want to ignore *other* values then you will probably need to use a `getter` method to serialize (look up how to do this), and make the getter return null when you want the actual value to be ignored. – MyStackRunnethOver Aug 21 '19 at 23:40
  • And also, watch out: if you want to keep your `int` but not deserialize it to XML when it was null in the input json, **checking if it is 0** could introduce a bug: you won't know whether the input json had a null int, or it had an int that was **actually 0** – MyStackRunnethOver Aug 21 '19 at 23:42
  • yeah, in that case I will probably go for a negative value. Thanks for the info, so what should i look/read for to return null, I am not much familiar with Gson library, can you please suggest some good link – Yash Bansal Aug 21 '19 at 23:46