From c57552489fd715ad51c118fed998bd6eb59a9be8 Mon Sep 17 00:00:00 2001 From: Joe Hansche Date: Thu, 24 Aug 2017 11:27:53 -0700 Subject: [PATCH] Properly decode JSONObject.NULL => null [Fixes #209] ParseEncoder encodes (null) => JSONObject.NULL. But ParseDecoder was not doing the inverse conversion. That resulted in JSONObject.NULL values being in the ParseObject.State map, which means any calls to State.get(key) that were casting the result to a specific type (i.e., String) without first checking the Object return type would result in a ClassCastException. ParseObject already accounts for this possibility by checking the Object type before casting it, and returning null if the type did not match. The known case that causes crashes is #209, where ParseUser.State.sessionToken() casts the result of get(KEY_SESSION_TOKEN) to String. When that was null it would be encoded as JSONObject.NULL due to the ParseEncoder; but after decoding it would not convert back to a native null, leading to the ClassCastException. --- Parse/src/main/java/com/parse/ParseDecoder.java | 4 ++++ Parse/src/test/java/com/parse/ParseDecoderTest.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Parse/src/main/java/com/parse/ParseDecoder.java b/Parse/src/main/java/com/parse/ParseDecoder.java index 9493e3c1b..a8f961f57 100644 --- a/Parse/src/main/java/com/parse/ParseDecoder.java +++ b/Parse/src/main/java/com/parse/ParseDecoder.java @@ -71,6 +71,10 @@ public Object decode(Object object) { return convertJSONArrayToList((JSONArray) object); } + if (object == JSONObject.NULL) { + return null; + } + if (!(object instanceof JSONObject)) { return object; } diff --git a/Parse/src/test/java/com/parse/ParseDecoderTest.java b/Parse/src/test/java/com/parse/ParseDecoderTest.java index 6a24d0880..5efb5de74 100644 --- a/Parse/src/test/java/com/parse/ParseDecoderTest.java +++ b/Parse/src/test/java/com/parse/ParseDecoderTest.java @@ -73,6 +73,12 @@ public void testNonJSONObject() { assertSame(obj, ParseDecoder.get().decode(obj)); } + @Test + public void testNull() { + Object object = ParseDecoder.get().decode(JSONObject.NULL); + assertNull(object); + } + @Test public void testParseFieldOperations() throws JSONException { JSONObject json = new JSONObject();