Skip to content

Commit c575524

Browse files
committed
Properly decode JSONObject.NULL => null [Fixes parse-community#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 parse-community#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.
1 parent 3cc9570 commit c575524

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

Parse/src/main/java/com/parse/ParseDecoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public Object decode(Object object) {
7171
return convertJSONArrayToList((JSONArray) object);
7272
}
7373

74+
if (object == JSONObject.NULL) {
75+
return null;
76+
}
77+
7478
if (!(object instanceof JSONObject)) {
7579
return object;
7680
}

Parse/src/test/java/com/parse/ParseDecoderTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public void testNonJSONObject() {
7373
assertSame(obj, ParseDecoder.get().decode(obj));
7474
}
7575

76+
@Test
77+
public void testNull() {
78+
Object object = ParseDecoder.get().decode(JSONObject.NULL);
79+
assertNull(object);
80+
}
81+
7682
@Test
7783
public void testParseFieldOperations() throws JSONException {
7884
JSONObject json = new JSONObject();

0 commit comments

Comments
 (0)