diff --git a/Zutil.iml b/Zutil.iml index 789635f..28d731d 100644 --- a/Zutil.iml +++ b/Zutil.iml @@ -6,7 +6,7 @@ - + diff --git a/src/zutil/parser/json/JSONObjectInputStream.java b/src/zutil/parser/json/JSONObjectInputStream.java old mode 100644 new mode 100755 index 1f49358..2070c9e --- a/src/zutil/parser/json/JSONObjectInputStream.java +++ b/src/zutil/parser/json/JSONObjectInputStream.java @@ -130,6 +130,8 @@ public class JSONObjectInputStream extends InputStream implements ObjectInput, C @SuppressWarnings({ "rawtypes", "unchecked" }) 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? if(type.isPrimitive() || String.class.isAssignableFrom(type)){ 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 { // Only parse if json is a map - if(!json.isMap()) + if(json == null || !json.isMap()) return null; // See if the Object id is in the cache before continuing if(json.getString("@object_id") != null && objectCache.containsKey(json.getInt(MD_OBJECT_ID))) diff --git a/test/zutil/test/JSONSerializerTest.java b/test/zutil/test/JSONSerializerTest.java index c692f36..802f4ca 100755 --- a/test/zutil/test/JSONSerializerTest.java +++ b/test/zutil/test/JSONSerializerTest.java @@ -48,7 +48,7 @@ public class JSONSerializerTest{ data = data.replace("\"", "'"); 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); } @@ -69,7 +69,7 @@ public class JSONSerializerTest{ data = data.replace("\"", "'"); 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); } @@ -113,7 +113,7 @@ public class JSONSerializerTest{ } @Test - public void testSerializerWithNullFields() throws InterruptedException, IOException, ClassNotFoundException{ + public void testSerializerWithNullFieldsHidden() throws InterruptedException, IOException, ClassNotFoundException{ TestClass sourceObj = new TestClass(); String data = writeObjectToJson(sourceObj, false); @@ -133,13 +133,28 @@ public class JSONSerializerTest{ String data = writeObjectToJson(sourceObj, false); data = data.replace("\"", "'"); assertEquals( - "{'map': {'key1': 'value1', 'key2': 'value2'}}\n", + "{'map': {'key2': 'value2', 'key1': 'value1'}}\n", data); TestClassMap targetObj = sendReceiveObject(sourceObj); 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 public void testSerializerWithListField() throws InterruptedException, IOException, ClassNotFoundException{ TestClassList sourceObj = new TestClassList().init();