Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

修复以$和_开头的属性名无法正确序列化和反序列化的问题 #2762

Merged
merged 2 commits into from
Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/main/java/com/alibaba/fastjson/util/JavaBeanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,11 @@ public static JavaBeanInfo build(Class<?> clazz //
if (!methodName.startsWith(withPrefix)) {
continue;
}

if (methodName.length() <= withPrefix.length()) {
continue;
}

properNameBuilder = new StringBuilder(methodName.substring(withPrefix.length()));
}
}
Expand Down Expand Up @@ -709,6 +709,7 @@ public static JavaBeanInfo build(Class<?> clazz //
char c3 = methodName.charAt(3);

String propertyName;
Field field = null;
if (Character.isUpperCase(c3) //
|| c3 > 512 // for unicode method name
) {
Expand All @@ -719,15 +720,31 @@ public static JavaBeanInfo build(Class<?> clazz //
}
} else if (c3 == '_') {
propertyName = methodName.substring(4);
field = TypeUtils.getField(clazz, propertyName, declaredFields);
if (field == null) {
String temp = propertyName;
propertyName = methodName.substring(3);
field = TypeUtils.getField(clazz, propertyName, declaredFields);
if (field == null) {
propertyName = temp; //减少修改代码带来的影响
}
}
} else if (c3 == 'f') {
propertyName = methodName.substring(3);
} else if (methodName.length() >= 5 && Character.isUpperCase(methodName.charAt(4))) {
propertyName = TypeUtils.decapitalize(methodName.substring(3));
} else {
continue;
propertyName = methodName.substring(3);
field = TypeUtils.getField(clazz, propertyName, declaredFields);
if (field == null) {
continue;
}
}

if (field == null) {
field = TypeUtils.getField(clazz, propertyName, declaredFields);
}

Field field = TypeUtils.getField(clazz, propertyName, declaredFields);
if (field == null && types[0] == boolean.class) {
String isFieldName = "is" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
field = TypeUtils.getField(clazz, isFieldName, declaredFields);
Expand Down
46 changes: 41 additions & 5 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
}
char c3 = methodName.charAt(3);
String propertyName;
Field field = null;
if(Character.isUpperCase(c3) //
|| c3 > 512 // for unicode method name
){
Expand All @@ -1887,19 +1888,36 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
propertyName = getPropertyNameByCompatibleFieldName(fieldCacheMap, methodName, propertyName, 3);
} else if(c3 == '_'){
propertyName = methodName.substring(4);
field = fieldCacheMap.get(propertyName);
if (field == null) {
String temp = propertyName;
propertyName = methodName.substring(3);
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
if (field == null) {
propertyName = temp; //减少修改代码带来的影响
}
}
} else if(c3 == 'f'){
propertyName = methodName.substring(3);
} else if(methodName.length() >= 5 && Character.isUpperCase(methodName.charAt(4))){
propertyName = decapitalize(methodName.substring(3));
} else{
continue;
propertyName = methodName.substring(3);
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
if (field == null) {
continue;
}
}
boolean ignore = isJSONTypeIgnore(clazz, propertyName);
if(ignore){
continue;
}
//假如bean的field很多的情况一下,轮询时将大大降低效率
Field field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);

if (field == null) {
// 假如bean的field很多的情况一下,轮询时将大大降低效率
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
}

if(field == null && propertyName.length() > 1){
char ch = propertyName.charAt(1);
if(ch >= 'A' && ch <= 'Z'){
Expand Down Expand Up @@ -1955,6 +1973,7 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
}
char c2 = methodName.charAt(2);
String propertyName;
Field field = null;
if(Character.isUpperCase(c2)){
if(compatibleWithJavaBean){
propertyName = decapitalize(methodName.substring(2));
Expand All @@ -1964,16 +1983,33 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, //
propertyName = getPropertyNameByCompatibleFieldName(fieldCacheMap, methodName, propertyName, 2);
} else if(c2 == '_'){
propertyName = methodName.substring(3);
field = fieldCacheMap.get(propertyName);
if (field == null) {
String temp = propertyName;
propertyName = methodName.substring(2);
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
if (field == null) {
propertyName = temp;
}
}
} else if(c2 == 'f'){
propertyName = methodName.substring(2);
} else{
continue;
propertyName = methodName.substring(2);
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
if (field == null) {
continue;
}
}
boolean ignore = isJSONTypeIgnore(clazz, propertyName);
if(ignore){
continue;
}
Field field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);

if(field == null) {
field = ParserConfig.getFieldFromCache(propertyName, fieldCacheMap);
}

if(field == null){
field = ParserConfig.getFieldFromCache(methodName, fieldCacheMap);
}
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_2400/Issue2488.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.alibaba.json.bvt.issue_2400;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import junit.framework.TestCase;

public class Issue2488 extends TestCase {
public void testForIssue_1() {
String a = "{\"$a_b\":\"a1_b2\",\"_c_d\":\"c3_d4\",\"aaaa\":\"CC\",\"__flag\":\"true\",\"$flag\":\"true\"}";
JSONObject obj = (JSONObject) JSONObject.parse(a);
TestJsonObj2 stu = JSONObject.toJavaObject(obj, TestJsonObj2.class);
assertEquals("TestJsonObj2{$a_b=\"a1_b2\",_c_d=\"c3_d4\",aaaa=\"CC\",__flag=true,$flag=true}", stu.toString());
}

public void testForIssue_2() {
String a = "{\"$a_b\":\"aa3_bb4\",\"_c_d\":\"cc1_dd2\",\"aaaa\":\"BB\",\"__flag\":\"true\",\"$flag\":\"true\"}";
TestJsonObj2 stu = JSON.parseObject(a, TestJsonObj2.class);
assertEquals("TestJsonObj2{$a_b=\"aa3_bb4\",_c_d=\"cc1_dd2\",aaaa=\"BB\",__flag=true,$flag=true}",
stu.toString());
}

public void testForIssue_3() {
TestJsonObj2 vo = new TestJsonObj2("aa_bb", "cc_dd", "AA", true, true);
String text = JSON.toJSONString(vo);
assertEquals("{\"$a_b\":\"aa_bb\",\"$flag\":true,\"__flag\":true,\"_c_d\":\"cc_dd\",\"aaaa\":\"AA\"}", text);
}

public static class TestJsonObj2 {
private String $a_b;
private String _c_d;
private String aaaa;
private boolean __flag;
private boolean $flag;

public TestJsonObj2() {
}

public TestJsonObj2(String $a_b, String _c_d, String aaaa, boolean __flag, boolean $flag) {
this.$a_b = $a_b;
this._c_d = _c_d;
this.aaaa = aaaa;
this.__flag = __flag;
this.$flag = $flag;
}

public String get$a_b() {
return $a_b;
}

public void set$a_b(String $a_b) {
this.$a_b = $a_b;
}

public String get_c_d() {
return _c_d;
}

public void set_c_d(String _c_d) {
this._c_d = _c_d;
}

public String getaaaa() {
return aaaa;
}

public void setaaaa(String aaaa) {
this.aaaa = aaaa;
}

public boolean is__flag() {
return __flag;
}

public void set__flag(boolean __flag) {
this.__flag = __flag;
}

public boolean is$flag() {
return $flag;
}

public void set$flag(boolean $flag) {
this.$flag = $flag;
}

@Override
public String toString() {
return String.format("TestJsonObj2{$a_b=\"%s\",_c_d=\"%s\",aaaa=\"%s\",__flag=%b,$flag=%b}", this.$a_b,
this._c_d, this.aaaa, this.__flag, this.$flag);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ public void setAge(int age) {
}

public void setage(int age) {
throw new UnsupportedOperationException();
this.age = age;
}

public void set(int age) {
Expand Down