From 95930a1147a50881906a4239c33b184eef9f08a1 Mon Sep 17 00:00:00 2001 From: wenshao Date: Thu, 16 May 2013 00:08:58 +0800 Subject: [PATCH 1/3] bug fixed for JSONField.serialize=false --- .../com/alibaba/fastjson/util/TypeUtils.java | 104 +++++++++--------- .../json/bvt/serializer/JSONFieldTest.java | 43 ++++++++ 2 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/serializer/JSONFieldTest.java diff --git a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java index af672bfca..e41d0db63 100755 --- a/src/main/java/com/alibaba/fastjson/util/TypeUtils.java +++ b/src/main/java/com/alibaba/fastjson/util/TypeUtils.java @@ -445,9 +445,9 @@ public class TypeUtils { if (obj == null) { return null; } - + if (clazz == null) { - throw new IllegalArgumentException("clazz is null"); + throw new IllegalArgumentException("clazz is null"); } if (clazz == obj.getClass()) { @@ -707,9 +707,9 @@ public class TypeUtils { String className = (String) iClassObject; clazz = (Class) loadClass(className); - + if (clazz == null) { - throw new ClassNotFoundException(className + " not found"); + throw new ClassNotFoundException(className + " not found"); } } } @@ -760,7 +760,7 @@ public class TypeUtils { mappings.put(className, clazz); } - + public static void addBaseClassMappings() { mappings.put("byte", byte.class); mappings.put("short", short.class); @@ -779,10 +779,10 @@ public class TypeUtils { mappings.put("[double", double[].class); mappings.put("[boolean", boolean[].class); mappings.put("[char", char[].class); - + mappings.put(HashMap.class.getName(), HashMap.class); } - + public static void clearClassMapping() { mappings.clear(); addBaseClassMappings(); @@ -864,9 +864,9 @@ public class TypeUtils { } JSONField annotation = method.getAnnotation(JSONField.class); - + if (annotation == null) { - annotation = getSupperMethodAnnotation(clazz, method); + annotation = getSupperMethodAnnotation(clazz, method); } if (annotation != null) { @@ -912,19 +912,25 @@ public class TypeUtils { Field field = ParserConfig.getField(clazz, propertyName); if (field == null) { - field = ParserConfig.getField(clazz, methodName.substring(3)); + field = ParserConfig.getField(clazz, methodName.substring(3)); } - + if (field != null) { JSONField fieldAnnotation = field.getAnnotation(JSONField.class); - if (fieldAnnotation != null && fieldAnnotation.name().length() != 0) { - propertyName = fieldAnnotation.name(); + if (fieldAnnotation != null) { + if (!fieldAnnotation.serialize()) { + continue; + } + + if (fieldAnnotation.name().length() != 0) { + propertyName = fieldAnnotation.name(); - if (aliasMap != null) { - propertyName = aliasMap.get(propertyName); - if (propertyName == null) { - continue; + if (aliasMap != null) { + propertyName = aliasMap.get(propertyName); + if (propertyName == null) { + continue; + } } } } @@ -1032,38 +1038,38 @@ public class TypeUtils { return fieldInfoList; } - public static JSONField getSupperMethodAnnotation(Class clazz, Method method) { - for (Class interfaceClass : clazz.getInterfaces()) { - for (Method interfaceMethod : interfaceClass.getMethods()) { - if (!interfaceMethod.getName().equals(method.getName())) { - continue; - } - - if (interfaceMethod.getParameterTypes().length != method.getParameterTypes().length) { - continue; - } - - boolean match = true; - for (int i = 0; i < interfaceMethod.getParameterTypes().length; ++i) { - if (!interfaceMethod.getParameterTypes()[i].equals(method.getParameterTypes()[i])) { - match = false; - break; - } - } - - if (!match) { - continue; - } - - JSONField annotation = interfaceMethod.getAnnotation(JSONField.class); - if (annotation != null) { - return annotation; - } - } - } - - return null; - } + public static JSONField getSupperMethodAnnotation(Class clazz, Method method) { + for (Class interfaceClass : clazz.getInterfaces()) { + for (Method interfaceMethod : interfaceClass.getMethods()) { + if (!interfaceMethod.getName().equals(method.getName())) { + continue; + } + + if (interfaceMethod.getParameterTypes().length != method.getParameterTypes().length) { + continue; + } + + boolean match = true; + for (int i = 0; i < interfaceMethod.getParameterTypes().length; ++i) { + if (!interfaceMethod.getParameterTypes()[i].equals(method.getParameterTypes()[i])) { + match = false; + break; + } + } + + if (!match) { + continue; + } + + JSONField annotation = interfaceMethod.getAnnotation(JSONField.class); + if (annotation != null) { + return annotation; + } + } + } + + return null; + } private static boolean isJSONTypeIgnore(Class clazz, String propertyName) { JSONType jsonType = clazz.getAnnotation(JSONType.class); diff --git a/src/test/java/com/alibaba/json/bvt/serializer/JSONFieldTest.java b/src/test/java/com/alibaba/json/bvt/serializer/JSONFieldTest.java new file mode 100644 index 000000000..cbac4849c --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/serializer/JSONFieldTest.java @@ -0,0 +1,43 @@ +package com.alibaba.json.bvt.serializer; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.annotation.JSONField; + +public class JSONFieldTest extends TestCase { + public void test_jsonField() throws Exception { + VO vo = new VO(); + + vo.setId(123); + vo.setName("xx"); + + String text = JSON.toJSONString(vo); + Assert.assertEquals("{\"id\":123}", text); + } + + public static class VO { + private int id; + + @JSONField(serialize=false) + private String name; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + } +} From 7aaa6e6dbb7fab9d856b4b874d96fccf068bf9ec Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 18 May 2013 17:06:35 +0800 Subject: [PATCH 2/3] bug fixed for Exception Parser --- .../fastjson/parser/deserializer/ThrowableDeserializer.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/alibaba/fastjson/parser/deserializer/ThrowableDeserializer.java b/src/main/java/com/alibaba/fastjson/parser/deserializer/ThrowableDeserializer.java index a2c0a8501..2dcbaaa34 100755 --- a/src/main/java/com/alibaba/fastjson/parser/deserializer/ThrowableDeserializer.java +++ b/src/main/java/com/alibaba/fastjson/parser/deserializer/ThrowableDeserializer.java @@ -23,6 +23,11 @@ public class ThrowableDeserializer extends JavaBeanDeserializer { @SuppressWarnings("unchecked") public T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONScanner lexer = (JSONScanner) parser.getLexer(); + + if (lexer.token() == JSONToken.NULL) { + lexer.nextToken(); + return null; + } if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) { parser.setResolveStatus(DefaultJSONParser.NONE); From 30cd24bf12ac6b119fbc675ac61f91a065e22f86 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 18 May 2013 17:06:43 +0800 Subject: [PATCH 3/3] bug fixed for Exception Parser --- .../alibaba/json/bvt/bug/Bug_for_wsky.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/test/java/com/alibaba/json/bvt/bug/Bug_for_wsky.java diff --git a/src/test/java/com/alibaba/json/bvt/bug/Bug_for_wsky.java b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_wsky.java new file mode 100644 index 000000000..7516af5a9 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/bug/Bug_for_wsky.java @@ -0,0 +1,19 @@ +package com.alibaba.json.bvt.bug; + +import junit.framework.TestCase; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.serializer.SerializerFeature; + +public class Bug_for_wsky extends TestCase { + + public void test_writeMapNull() throws Exception { + JSON.parseObject(JSON.toJSONString(new MethodReturn(), SerializerFeature.WriteMapNullValue), MethodReturn.class); + } + + public static class MethodReturn { + + public Object ReturnValue; + public Throwable Exception; + } +}