Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public static Object parseNextValue(JsonReader reader, boolean convertJsonTypes)
case NULL:
reader.nextNull();
return null;

default:
throw new IllegalStateException("Unexpected token: " + nextToken);
}
Expand All @@ -130,7 +129,12 @@ public static JSONObject parseAsJsonObject(JsonReader reader, String key) throws
reader.beginObject();
while (reader.hasNext()) {
try {
result.put(reader.nextName(), parseNextValue(reader, false));
String name = reader.nextName();
Object value = parseNextValue(reader, false);
if (value == null) {
value = JSONObject.NULL;
}
result.put(name, value);
} catch (JSONException e) {
throw new RuntimeException("This should be impossible.", e);
}
Expand Down Expand Up @@ -173,11 +177,13 @@ public static <T> void parseAsMap(JsonReader reader, Map<String, T> map, Class<T
reader.endObject();
} else {
Object o = parseNextValue(reader, true);
if (!valueClass.isInstance(o)) {
if (o == null) {
map.put(name, null);
continue;
} else if (!valueClass.isInstance(o)) {
throwMapException(name, key, valueClass, o);
}
value = cast(o);

}
map.put(name, value);
}
Expand Down Expand Up @@ -287,6 +293,9 @@ private static <T> void convertJsonObjectToMap(JSONObject jsonObject,
parserTable);
} else if (valueClass.isInstance(o)) {
result = cast(o);
} else if (o == JSONObject.NULL) {
map.put(name, null);
continue;
}

if (result != null) {
Expand Down Expand Up @@ -633,6 +642,9 @@ private static Object parseSpecificJsonObjectDelayed(JsonReader reader,

// No matching parser has been found yet; save the current name and value to the
// jsonObject.
if (value == null) {
value = JSONObject.NULL;
}
try {
jsonObject.put(name, value);
} catch (JSONException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public class TestObject extends AbstractTestObject {
@JsonValue("myEmptyCollection")
public Collection<String> myEmptyCollection;

//Nulls
@JsonValue("myNullInt")
public int myNullInt = 1;

Expand All @@ -180,6 +181,13 @@ public class TestObject extends AbstractTestObject {
@JsonValue("myCollectionWithNullValues")
public Collection<String> myCollectionWithNullValues;

@JsonValue("myStringMapWithSingleNullValue")
public Map<String, String> myStringMapWithSingleNullValue;
@JsonValue("myStringMapWithNullValues")
public Map<String, String> myStringMapWithNullValues;
@JsonValue("myObjectMapWithNullValues")
public Map<String, SimpleTestObject> myObjectMapWithNullValues;

// Fields for setters
public String stringFromSetter;
public UnannotatedObject unannotatedObjectFromSetter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ public void testCollectionWithNullValueUpdate() throws Exception {

TestObject$$JsonObjectParser.INSTANCE.updateInstanceFromMap(testObject, updates, CONTEXT);

assertEquals(testObject.myCollectionWithNullValues, CollectionUtils.newArrayList("one", null, "null"));
assertEquals("testObject.myCollectionWithNullValues", testObject.myCollectionWithNullValues,
CollectionUtils.newArrayList("one", null, "null"));
}

@Test(expected = RuntimeException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ public void testPartialDelayParse() throws Exception {
testParse("partially-delayed-object.json");
}

@Test
public void testFullDelayNullMap() throws Exception {
TestObject testObject = (TestObject) parser.parseJsonStream(getInputStream("null-in-maps.json"));

assertNotNull("testObject.myStringMapWithSingleNullValue", testObject.myStringMapWithSingleNullValue);
assertEquals("testObject.myStringMapWithSingleNullValue.size", 1, testObject.myStringMapWithSingleNullValue.size());
assertEquals("testObject.myStringMapWithSingleNullValue[key1]", null, testObject.myStringMapWithSingleNullValue.get("key1"));

assertNotNull("testObject.myStringMapWithNullValues", testObject.myStringMapWithNullValues);
assertEquals("testObject.myStringMapWithNullValues.size", 3, testObject.myStringMapWithNullValues.size());
assertEquals("testObject.myStringMapWithNullValues[key1]", null, testObject.myStringMapWithNullValues.get("key1"));
assertEquals("testObject.myStringMapWithNullValues[key2]", "value2", testObject.myStringMapWithNullValues.get("key2"));
assertEquals("testObject.myStringMapWithNullValues[key3]", "null", testObject.myStringMapWithNullValues.get("key3"));

assertNotNull("testObject.myObjectMapWithNullValues", testObject.myObjectMapWithNullValues);
assertEquals("testObject.myObjectMapWithNullValues.size", 3, testObject.myObjectMapWithNullValues.size());
assertEquals("testObject.myObjectMapWithNullValues[key1]", new SimpleTestObject(null), testObject.myObjectMapWithNullValues.get("key1"));
assertEquals("testObject.myObjectMapWithNullValues[key2]", new SimpleTestObject("post-parse:value2"), testObject.myObjectMapWithNullValues.get("key2"));
assertEquals("testObject.myObjectMapWithNullValues[key3]", new SimpleTestObject("post-parse:null"), testObject.myObjectMapWithNullValues.get("key3"));
}

private void testParse(String fileName) throws Exception {
TestObject testObject = (TestObject) parser.parseJsonStream(getInputStream(fileName));
assertNotNull("testObject", testObject);
Expand Down Expand Up @@ -256,10 +277,25 @@ private void testParse(String fileName) throws Exception {
assertEquals("testObject.myDefaultCollection", Collections.singleton("the one"),
testObject.myDefaultCollection);

Collection<String> expected = new ArrayList<>();
expected.add(null);
assertEquals(expected, testObject.myCollectionWithSingleNullValue);
assertEquals(CollectionUtils.newArrayList(null, "string", null, "null"), testObject.myCollectionWithNullValues);
assertEquals("testObject.myCollectionWithSingleNullValue", CollectionUtils.newArrayList((String) null), testObject.myCollectionWithSingleNullValue);
assertEquals("testObject.myCollectionWithNullValues",
CollectionUtils.newArrayList(null, "string", null, "null"), testObject.myCollectionWithNullValues);

assertNotNull("testObject.myStringMapWithSingleNullValue", testObject.myStringMapWithSingleNullValue);
assertEquals("testObject.myStringMapWithSingleNullValue.size", 1, testObject.myStringMapWithSingleNullValue.size());
assertEquals("testObject.myStringMapWithSingleNullValue[key1]", null, testObject.myStringMapWithSingleNullValue.get("key1"));

assertNotNull("testObject.myStringMapWithNullValues", testObject.myStringMapWithNullValues);
assertEquals("testObject.myStringMapWithNullValues.size", 3, testObject.myStringMapWithNullValues.size());
assertEquals("testObject.myStringMapWithNullValues[key1]", null, testObject.myStringMapWithNullValues.get("key1"));
assertEquals("testObject.myStringMapWithNullValues[key2]", "value2", testObject.myStringMapWithNullValues.get("key2"));
assertEquals("testObject.myStringMapWithNullValues[key3]", "null", testObject.myStringMapWithNullValues.get("key3"));

assertNotNull("testObject.myObjectMapWithNullValues", testObject.myObjectMapWithNullValues);
assertEquals("testObject.myObjectMapWithNullValues.size", 3, testObject.myObjectMapWithNullValues.size());
assertEquals("testObject.myObjectMapWithNullValues[key1]", new SimpleTestObject(null), testObject.myObjectMapWithNullValues.get("key1"));
assertEquals("testObject.myObjectMapWithNullValues[key2]", new SimpleTestObject("post-parse:value2"), testObject.myObjectMapWithNullValues.get("key2"));
assertEquals("testObject.myObjectMapWithNullValues[key3]", new SimpleTestObject("post-parse:null"), testObject.myObjectMapWithNullValues.get("key3"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,24 @@
"myDefaultCollection": null,
"myCollectionWithSingleNullValue": [null],
"myCollectionWithNullValues": [null, "string", null, "null"],
"myStringMapWithSingleNullValue": {
"key1": null
},
"myStringMapWithNullValues": {
"key1": null,
"key2": "value2",
"key3": "null"
},
"myObjectMapWithNullValues": {
"key1": {
"myString": null
},
"key2": {
"myString": "value2"
},
"key3": {
"myString": "null"
}
},
"object": "testObject"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"myStringMapWithSingleNullValue": {
"key1": null
},
"myStringMapWithNullValues": {
"key1": null,
"key2": "value2",
"key3": "null"
},
"myObjectMapWithNullValues": {
"key1": {
"myString": null
},
"key2": {
"myString": "value2"
},
"key3": {
"myString": "null"
}
},
"object": "testObject"
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,24 @@
"myNullCollection": null,
"myDefaultCollection": null,
"myCollectionWithSingleNullValue": [null],
"myCollectionWithNullValues": [null, "string", null, "null"]
"myCollectionWithNullValues": [null, "string", null, "null"],
"myStringMapWithSingleNullValue": {
"key1": null
},
"myStringMapWithNullValues": {
"key1": null,
"key2": "value2",
"key3": "null"
},
"myObjectMapWithNullValues": {
"key1": {
"myString": null
},
"key2": {
"myString": "value2"
},
"key3": {
"myString": "null"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,24 @@
"myNullCollection": null,
"myDefaultCollection": null,
"myCollectionWithSingleNullValue": [null],
"myCollectionWithNullValues": [null, "string", null, "null"]
"myCollectionWithNullValues": [null, "string", null, "null"],
"myStringMapWithSingleNullValue": {
"key1": null
},
"myStringMapWithNullValues": {
"key1": null,
"key2": "value2",
"key3": "null"
},
"myObjectMapWithNullValues": {
"key1": {
"myString": null
},
"key2": {
"myString": "value2"
},
"key3": {
"myString": "null"
}
}
}