GSON Question: .isJsonNull ()

I am reading in a JSON file (using Google GSON ). One of my tests checks the behavior of the program in the event file, this key is missing.

 JsonElement value = e.getAsJsonObject().get(ENVIRONMENT); 

I expect that when .get (ing) this key, I would get null . Turns out I do. When I .get(ENVIRONMENT) , null returned.

When I test it, I get " not null ". Strange, considering that GSON javadoc says: β€œit provides a check to confirm whether this element represents a null value or not ”

 if (value.isJsonNull()) { System.out.println("null"); } else { System.out.println("not null"); } 

Please help me better understand this.

+4
json null gson
source share
1 answer

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) :

  /** * Returns the member with the specified name. * * @param memberName name of the member that is being requested. * @return the member matching the name. Null if no such member exists. */ 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:

  /** * Adds a member, which is a name-value pair, to self. The name must be a String, but the value * can be an arbitrary JsonElement, thereby allowing you to build a full tree of JsonElements * rooted at this node. * * @param property name of the member. * @param value the member object. */ 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.

+12
source share

All Articles