Bugfix for json map with null entry

This commit is contained in:
Ziver Koc 2015-10-29 20:03:28 +01:00
parent 5fa807e50b
commit ef173f479b
3 changed files with 23 additions and 6 deletions

View file

@ -6,7 +6,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST"> <orderEntry type="module-library" scope="TEST">
<library name="JUnit4"> <library name="JUnit4">

4
src/zutil/parser/json/JSONObjectInputStream.java Normal file → Executable file
View file

@ -130,6 +130,8 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
protected Object readType(Class<?> type, Class<?>[] genType, String key, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, UnsupportedDataTypeException, NoSuchFieldException { protected Object readType(Class<?> type, Class<?>[] genType, String key, DataNode json) throws IllegalAccessException, ClassNotFoundException, InstantiationException, UnsupportedDataTypeException, NoSuchFieldException {
if(json == null)
return null;
// Field type is a primitive? // Field type is a primitive?
if(type.isPrimitive() || String.class.isAssignableFrom(type)){ if(type.isPrimitive() || String.class.isAssignableFrom(type)){
return readPrimitive(type, json); return readPrimitive(type, json);
@ -171,7 +173,7 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C
protected Object readObject(Class<?> type, String key, DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException, NoSuchFieldException { protected Object readObject(Class<?> type, String key, DataNode json) throws IllegalAccessException, InstantiationException, ClassNotFoundException, IllegalArgumentException, UnsupportedDataTypeException, NoSuchFieldException {
// Only parse if json is a map // Only parse if json is a map
if(!json.isMap()) if(json == null || !json.isMap())
return null; return null;
// See if the Object id is in the cache before continuing // See if the Object id is in the cache before continuing
if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt(MD_OBJECT_ID))) if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt(MD_OBJECT_ID)))

View file

@ -48,7 +48,7 @@ public class JSONSerializerTest{
data = data.replace("\"", "'"); data = data.replace("\"", "'");
assertEquals( assertEquals(
"{'str': 'abcd', '@class': 'zutil.test.JSONSerializerTest$TestClass', 'obj2': {'@class': 'zutil.test.JSONSerializerTest$TestObj', '@object_id': 3, 'value': 42}, '@object_id': 1, 'obj1': {'@class': 'zutil.test.JSONSerializerTest$TestObj', '@object_id': 2, 'value': 42}, 'decimal': 1.1}\n", "{'str': 'abcd', '@class': 'zutil.test.JSONSerializerTest$TestClass', 'obj1': {'@class': 'zutil.test.JSONSerializerTest$TestObj', 'value': 42, '@object_id': 2}, 'obj2': {'@class': 'zutil.test.JSONSerializerTest$TestObj', 'value': 42, '@object_id': 3}, 'decimal': 1.1, '@object_id': 1}\n",
data); data);
} }
@ -69,7 +69,7 @@ public class JSONSerializerTest{
data = data.replace("\"", "'"); data = data.replace("\"", "'");
assertEquals( assertEquals(
"{'@class': 'zutil.test.JSONSerializerTest$TestClassObjClone', 'obj2': {'@object_id': 2}, '@object_id': 1, 'obj1': {'@class': 'zutil.test.JSONSerializerTest$TestObj', '@object_id': 2, 'value': 42}}\n", "{'@class': 'zutil.test.JSONSerializerTest$TestClassObjClone', 'obj1': {'@class': 'zutil.test.JSONSerializerTest$TestObj', 'value': 42, '@object_id': 2}, 'obj2': {'@object_id': 2}, '@object_id': 1}\n",
data); data);
} }
@ -113,7 +113,7 @@ public class JSONSerializerTest{
} }
@Test @Test
public void testSerializerWithNullFields() throws InterruptedException, IOException, ClassNotFoundException{ public void testSerializerWithNullFieldsHidden() throws InterruptedException, IOException, ClassNotFoundException{
TestClass sourceObj = new TestClass(); TestClass sourceObj = new TestClass();
String data = writeObjectToJson(sourceObj, false); String data = writeObjectToJson(sourceObj, false);
@ -133,13 +133,28 @@ public class JSONSerializerTest{
String data = writeObjectToJson(sourceObj, false); String data = writeObjectToJson(sourceObj, false);
data = data.replace("\"", "'"); data = data.replace("\"", "'");
assertEquals( assertEquals(
"{'map': {'key1': 'value1', 'key2': 'value2'}}\n", "{'map': {'key2': 'value2', 'key1': 'value1'}}\n",
data); data);
TestClassMap targetObj = sendReceiveObject(sourceObj); TestClassMap targetObj = sendReceiveObject(sourceObj);
TestClassMap.assertEquals(sourceObj, targetObj); TestClassMap.assertEquals(sourceObj, targetObj);
} }
@Test
public void testSerializerWithMapFieldWithNullEntry() throws InterruptedException, IOException, ClassNotFoundException{
TestClassMap sourceObj = new TestClassMap().init();
sourceObj.map.put("key1", null);
String data = writeObjectToJson(sourceObj, false);
data = data.replace("\"", "'");
assertEquals(
"{'map': {'key2': 'value2', 'key1': null}}\n",
data);
TestClassMap targetObj = sendReceiveObject(sourceObj);
TestClassMap.assertEquals(sourceObj, targetObj);
}
@Test @Test
public void testSerializerWithListField() throws InterruptedException, IOException, ClassNotFoundException{ public void testSerializerWithListField() throws InterruptedException, IOException, ClassNotFoundException{
TestClassList sourceObj = new TestClassList().init(); TestClassList sourceObj = new TestClassList().init();