From 696ca6c5dfbd5a545dbcdd2939aa0c2b6fe893c4 Mon Sep 17 00:00:00 2001 From: wenshao Date: Sun, 1 Oct 2017 21:27:16 +0800 Subject: [PATCH] bug fixed for issue #1494 --- .../deserializer/JavaBeanDeserializer.java | 22 +++++++------ .../json/bvt/issue_1400/Issue1494.java | 33 +++++++++++++++++++ 2 files changed, 45 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/deserializer/JavaBeanDeserializer.java b/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java index 5130721473..733737e943 100755 --- a/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java +++ b/src/main/java/com/alibaba/fastjson/parser/deserializer/JavaBeanDeserializer.java @@ -944,17 +944,19 @@ public boolean parseField(DefaultJSONParser parser, String key, Object object, T || (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..78f8cecd95 --- /dev/null +++ b/src/test/java/com/alibaba/json/bvt/issue_1400/Issue1494.java @@ -0,0 +1,33 @@ +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 com.alibaba.fastjson.parser.ParserConfig; +import com.alibaba.fastjson.serializer.SerializeConfig; +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); + System.out.println(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; + } + } +}