Deserialize null JsonString

3,843 views
Skip to first unread message

Moritz Post

unread,
Mar 1, 2012, 8:31:23 AM3/1/12
to googl...@googlegroups.com
Hi gson folks

I am having a problem to deserialize a json field that is null. Imagine the following json object:

{ "key" : null }

When deserializing into 

public class MyClass {
  public AnotherClass key;
}

the field "key" stays null on MyClass. What i would like to do though is to set key to a default value that denotes that the key as been nulled explicitly. I tried to use a custom Deserializer but when the json field is set to null my custom Deserializer does not get called at all so i can not manually deserialize

How should i go about to let my classes know that a json property has been nulled explicitly?

Thanks in advance
Moritz Post

Moritz Post

unread,
Mar 1, 2012, 8:34:33 AM3/1/12
to googl...@googlegroups.com
The topic of the post is a bit missleading. It should read: Deserialize null value

Brandon Mintern

unread,
Mar 1, 2012, 1:41:23 PM3/1/12
to googl...@googlegroups.com
You have several options here.

1. If you want to go the custom deserializer route, you can create a
deserializer for MyClass (as opposed to AnotherClass). Now you can
deserialize however you want, including determining whether "key" is
mapped to null in the JsonObject or not set at all.

2. You can do the parsing manually, without creating a custom
deserializer. This would be something like:

JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject();
MyClass myClass = new MyClass();
if (json.has("key")) {
// MyClass.setKey can set the key and also set a boolean to
indicate that "key"
// has been set (to differentiate null from unset)
myClass.setKey(new Gson().fromJson(json.get("key"), AnotherClass.class));
}

3. You can have a sentinel value for AnotherClass, something like:

public class AnotherClass {
public static final AnotherClass UNSET = new AnotherClass();
// ....
}

public class MyClass {
public AnotherClass key = AnotherClass.UNSET;
}

Now you can use normal gson to parse MyClass, and if "key" is set to
null, you know that it has been parsed from Json and set to null. If
it is still UNSET, "key" was not in the Json at all.

> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/sy0b5FppdBwJ.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-gson...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

Moritz Post

unread,
Mar 1, 2012, 2:37:08 PM3/1/12
to googl...@googlegroups.com
Hi Brandon

Thank you for listing all the available options. Very comprehensive. 



1. If you want to go the custom deserializer route, you can create a
deserializer for MyClass (as opposed to AnotherClass). Now you can
deserialize however you want, including determining whether "key" is
mapped to null in the JsonObject or not set at all.

That is an option but MyClass has a lot of fields so mapping them all manually would not be very nice.
 

2. You can do the parsing manually, without creating a custom
deserializer. This would be something like:

JsonObject json = new JsonParser().parse(jsonString).getAsJsonObject();
MyClass myClass = new MyClass();
if (json.has("key")) {
    // MyClass.setKey can set the key and also set a boolean to
indicate that "key"
    // has been set (to differentiate null from unset)
    myClass.setKey(new Gson().fromJson(json.get("key"), AnotherClass.class));
}

Parsing by hand has the same caveats as the above and my json structue is much more complex.
 

3. You can have a sentinel value for AnotherClass, something like:

public class AnotherClass {
    public static final AnotherClass UNSET = new AnotherClass();
    // ....
}

public class MyClass {
    public AnotherClass key = AnotherClass.UNSET;
}

Now you can use normal gson to parse MyClass, and if "key" is set to
null, you know that it has been parsed from Json and set to null. If
it is still UNSET, "key" was not in the Json at all.

I think that is the most valid solution. I was not aware that the null value is also explicitly set when the null value is defined in json.

Thanks.

Greets
Moritz Post
 

On Thu, Mar 1, 2012 at 5:31 AM, Moritz Post <morit...@gmail.com> wrote:
> Hi gson folks
>
> I am having a problem to deserialize a json field that is null. Imagine the
> following json object:
>
> { "key" : null }
>
> When deserializing into
>
> public class MyClass {
>   public AnotherClass key;
> }
>
> the field "key" stays null on MyClass. What i would like to do though is to
> set key to a default value that denotes that the key as been nulled
> explicitly. I tried to use a custom Deserializer but when the json field is
> set to null my custom Deserializer does not get called at all so i can not
> manually deserialize
>
> How should i go about to let my classes know that a json property has been
> nulled explicitly?
>
> Thanks in advance
> Moritz Post
>
> --
> You received this message because you are subscribed to the Google Groups
> "google-gson" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-gson/-/sy0b5FppdBwJ.
> To post to this group, send email to googl...@googlegroups.com.
> To unsubscribe from this group, send email to


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.


> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.

> google-gson+unsubscribe@googlegroups.com.

Moritz Post

unread,
Mar 1, 2012, 2:40:41 PM3/1/12
to googl...@googlegroups.com
Sorry for overquoting. The new webinterface jumps all over the place for me.

Oscar Sanhueza Riveros

unread,
Nov 28, 2012, 5:40:19 PM11/28/12
to googl...@googlegroups.com
Hi, 

I was having the same problems with the nulls

just use 

Gson gson = new GsonBuilder().serializeNulls().create();

So the deserialization will handle the nulls and give null value to the strings then.

It is actually mentioned in the manual, but just explained when serializing.

Дмитрий Попов

unread,
Jul 17, 2015, 8:36:20 AM7/17/15
to googl...@googlegroups.com
Please don't confuse serialization and deserialization. Gson ignore null values during serialization by default

четверг, 29 ноября 2012 г., 2:40:19 UTC+4 пользователь Oscar Sanhueza Riveros написал:
Reply all
Reply to author
Forward
0 new messages