lightcouch / LightCouch

CouchDB Java API
www.lightcouch.org
Apache License 2.0
67 stars 70 forks source link

Json Null not being handled correctly #37

Closed rama0120 closed 9 years ago

rama0120 commented 9 years ago

Hi,

We ran into issues in our project when we enabled GsonBuilder's serializeNulls() option and feed that Gson object to this client.

Specifically, this method in CouchDbUtils.java is problematic: public static String getAsString(JsonObject j, String e) { return j.get(e) == null ? null : j.get(e).getAsString(); }

CouchDbClientBase invokes this method to get the _id and _rev properties. The problem we're facing is when Gson serializes nulls, (say {"_id": null}) - the logic above falls short. It invokes getAsString() on JsonNull, which doesn't override the base implementation of getAsString() from JsonElement. This ends up throwing UnsupportedOperationException().

Shouldn't the logic be more like this? public static String getAsString(JsonObject j, String e) { return (j.get(e) == null || j.get(e).isJsonNull) ? null : j.get(e).getAsString(); }

Same applies to other getAs..() methods. What are your thoughts? Why was isJsonNull check skipped?

ahmedyha commented 9 years ago

Fixed in 0.1.8