mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-28 22:04:04 +08:00
add compatible with JavaBean
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.fastjson.util;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -204,7 +205,11 @@ public class DeserializeBeanInfo {
|
||||
|
||||
String propertyName;
|
||||
if (Character.isUpperCase(c3)) {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
|
||||
if (TypeUtils.compatibleWithJavaBean) {
|
||||
propertyName = Introspector.decapitalize(methodName.substring(3));
|
||||
} else {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
|
||||
}
|
||||
} else if (c3 == '_') {
|
||||
propertyName = methodName.substring(4);
|
||||
} else if (c3 == 'f') {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.alibaba.fastjson.util;
|
||||
|
||||
import java.beans.Introspector;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Field;
|
||||
@@ -56,6 +57,8 @@ import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
|
||||
*/
|
||||
public class TypeUtils {
|
||||
|
||||
public static boolean compatibleWithJavaBean = false;
|
||||
|
||||
public static final String castToString(Object value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
@@ -950,7 +953,11 @@ public class TypeUtils {
|
||||
|
||||
String propertyName;
|
||||
if (Character.isUpperCase(c3)) {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
|
||||
if (compatibleWithJavaBean) {
|
||||
propertyName = Introspector.decapitalize(methodName.substring(3));
|
||||
} else {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4);
|
||||
}
|
||||
} else if (c3 == '_') {
|
||||
propertyName = methodName.substring(4);
|
||||
} else if (c3 == 'f') {
|
||||
@@ -1007,7 +1014,11 @@ public class TypeUtils {
|
||||
|
||||
String propertyName;
|
||||
if (Character.isUpperCase(c2)) {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3);
|
||||
if (compatibleWithJavaBean) {
|
||||
propertyName = Introspector.decapitalize(methodName.substring(2));
|
||||
} else {
|
||||
propertyName = Character.toLowerCase(methodName.charAt(2)) + methodName.substring(3);
|
||||
}
|
||||
} else if (c2 == '_') {
|
||||
propertyName = methodName.substring(3);
|
||||
} else if (c2 == 'f') {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.alibaba.json.bvt.parser;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
|
||||
public class TypeUtilsTest_compatibleWithJavaBean extends TestCase {
|
||||
|
||||
private boolean origin_compatibleWithJavaBean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
origin_compatibleWithJavaBean = TypeUtils.compatibleWithJavaBean;
|
||||
TypeUtils.compatibleWithJavaBean = true;
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
TypeUtils.compatibleWithJavaBean = origin_compatibleWithJavaBean;
|
||||
}
|
||||
|
||||
public void test_true() throws Exception {
|
||||
String text = JSON.toJSONString(new VO(123));
|
||||
Assert.assertEquals("{\"ID\":123}", text);
|
||||
Assert.assertEquals(123, JSON.parseObject(text, VO.class).getID());
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private int id;
|
||||
|
||||
public VO(){
|
||||
|
||||
}
|
||||
|
||||
public VO(int id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setID(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.alibaba.json.bvt.parser;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.util.TypeUtils;
|
||||
|
||||
public class TypeUtilsTest_compatibleWithJavaBean_boolean extends TestCase {
|
||||
|
||||
private boolean origin_compatibleWithJavaBean;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
origin_compatibleWithJavaBean = TypeUtils.compatibleWithJavaBean;
|
||||
TypeUtils.compatibleWithJavaBean = true;
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
TypeUtils.compatibleWithJavaBean = origin_compatibleWithJavaBean;
|
||||
}
|
||||
|
||||
public void test_true() throws Exception {
|
||||
String text = JSON.toJSONString(new VO(true));
|
||||
Assert.assertEquals("{\"ID\":true}", text);
|
||||
Assert.assertEquals(true, JSON.parseObject(text, VO.class).isID());
|
||||
}
|
||||
|
||||
public static class VO {
|
||||
|
||||
private boolean id;
|
||||
|
||||
public VO(){
|
||||
|
||||
}
|
||||
|
||||
public VO(boolean id){
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public boolean isID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setID(boolean id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user