Ignore my first answer below. I would read this question too quickly.
This seems to be a simple case of lying documents - or at least misunderstood. Fortunately, the code doesn't lie that easily, and Gson is an open source project.
Here's JsonObject.get(String) :
public JsonElement get(String memberName) { if (members.containsKey(memberName)) { JsonElement member = members.get(memberName); return member == null ? JsonNull.INSTANCE : member; } return null; }
and here, where members populated:
public void add(String property, JsonElement value) { if (value == null) { value = JsonNull.INSTANCE; } members.put($Gson$Preconditions.checkNotNull(property), value); }
Calls to add to members are made for each member defined in the Java class - it is not based on what is in JSON. (For those interested, the visitFieldsReflectively method in the ReflectingFieldNavigator populates the elements.)
So, I believe that confusion surrounds the meaning of the βmemberβ in the sentence βif such a member does not existβ. Based on the code, I understand that the author of the JavaDoc referred to an element defined in the Java class. For the casual user of the Gson API β like me β I assumed that the βmemberβ refers to an object element in JSON.
Now, is this problem clear?
====
The first answer is based on a quick review of the question (saved for useful links):
A null reference is not a JsonNull value. (value == null) does not match value.isJsonNull() . They are very different.
The docs describe that calling JsonObject.get(String) returns "[n] ull if such a member does not exist." They do not say that JsonNull returning.
The JsonElement.isJsonNull() call does not check if the JsonElement reference is a null reference. In fact, if it were a null reference, a method call on it would throw a NullPointerException . It checks if this is a JsonNull instance.