From 569d7ecba43f81380bb51992975388b22f27bd6a Mon Sep 17 00:00:00 2001 From: wenshao Date: Sat, 7 Oct 2017 01:04:56 +0800 Subject: [PATCH] bug fixed for Feature.SupportNonPublicField. for issue #1494 --- .../fastjson/parser/JavaBeanDeserializer.java | 22 +++++++------ .../json/bvt/issue_1400/Issue1494.java | 31 +++++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/alibaba/json/bvt/issue_1400/Issue1494.java diff --git a/src/main/java/com/alibaba/fastjson/parser/JavaBeanDeserializer.java b/src/main/java/com/alibaba/fastjson/parser/JavaBeanDeserializer.java index c63400f396..7927ca30b8 100755 --- a/src/main/java/com/alibaba/fastjson/parser/JavaBeanDeserializer.java +++ b/src/main/java/com/alibaba/fastjson/parser/JavaBeanDeserializer.java @@ -961,17 +961,19 @@ private boolean parseField(DefaultJSONParser parser, String key, Object object, || (this.beanInfo.parserFeatures & mask) != 0)) { if (this.extraFieldDeserializers == null) { ConcurrentHashMap extraFieldDeserializers = new ConcurrentHashMap(1, 0.75f, 1); - Field[] fields = this.clazz.getDeclaredFields(); - for (Field field : fields) { - String fieldName = field.getName(); - if (this.getFieldDeserializer(fieldName) != null) { - continue; - } - int fieldModifiers = field.getModifiers(); - if ((fieldModifiers & Modifier.FINAL) != 0 || (fieldModifiers & Modifier.STATIC) != 0) { - continue; + for (Class c = this.clazz; c != null && c != Object.class; c = c.getSuperclass()) { + Field[] fields = c.getDeclaredFields(); + for (Field field : fields) { + String fieldName = field.getName(); + if (this.getFieldDeserializer(fieldName) != null) { + continue; + } + int fieldModifiers = field.getModifiers(); + if ((fieldModifiers & Modifier.FINAL) != 0 || (fieldModifiers & Modifier.STATIC) != 0) { + continue; + } + extraFieldDeserializers.put(fieldName, field); } - extraFieldDeserializers.put(fieldName, field); } this.extraFieldDeserializers = extraFieldDeserializers; } diff --git a/src/test/java/com/alibaba/json/bvt/issue_1400/Issue1494.java b/src/test/java/com/alibaba/json/bvt/issue_1400/Issue1494.java new file mode 100644 index 0000000000..a411656137 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1400/Issue1494.java @@ -0,0 +1,31 @@ +package com.alibaba.json.bvt.issue_1400; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.annotation.JSONType; +import com.alibaba.fastjson.parser.Feature; +import junit.framework.TestCase; + +public class Issue1494 extends TestCase { + public void test_for_issue() throws Exception { + String json = "{\"id\":1001,\"name\":\"wenshao\"}"; + B b = JSON.parseObject(json, B.class); + assertEquals("{\"id\":1001,\"name\":\"wenshao\"}", JSON.toJSONString(b)); + } + + public static class A { + private int id; + + public int getId() { + return id; + } + } + + @JSONType(parseFeatures = Feature.SupportNonPublicField) + public static class B extends A { + private String name; + + public String getName() { + return name; + } + } +}