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.
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.
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.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.
> For more options, visit this group at
> http://groups.google.com/group/google-gson?hl=en.