mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-22 09:34:13 +08:00
bug fixed for JSONType support
This commit is contained in:
@@ -130,13 +130,7 @@ public class ASMSerializerFactory implements Opcodes {
|
||||
}
|
||||
|
||||
String className = getGenClassName(clazz);
|
||||
int beanSerializeFeatures = 0;
|
||||
{
|
||||
JSONType jsonType = TypeUtils.getJSONType(clazz);
|
||||
if (jsonType != null) {
|
||||
beanSerializeFeatures = SerializerFeature.of(jsonType.serialzeFeatures());
|
||||
}
|
||||
}
|
||||
int beanSerializeFeatures = TypeUtils.getSerializeFeatures(clazz);
|
||||
|
||||
ClassWriter cw = new ClassWriter();
|
||||
cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, className, "java/lang/Object",
|
||||
|
||||
@@ -125,10 +125,6 @@ public class JSONSerializer {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public void setContext(SerialContext parent, Object object, Object fieldName) {
|
||||
this.setContext(parent, object, fieldName, 0);
|
||||
}
|
||||
|
||||
public void setContext(SerialContext parent, Object object, Object fieldName, int features) {
|
||||
if (isEnabled(SerializerFeature.DisableCircularReferenceDetect)) {
|
||||
return;
|
||||
|
||||
@@ -39,7 +39,7 @@ public class JavaBeanSerializer implements ObjectSerializer {
|
||||
private final FieldSerializer[] getters;
|
||||
private final FieldSerializer[] sortedGetters;
|
||||
|
||||
private int features = JSON.DEFAULT_GENERATE_FEATURE;
|
||||
private int features = 0;
|
||||
|
||||
public FieldSerializer[] getGetters() {
|
||||
return getters;
|
||||
@@ -63,6 +63,8 @@ public class JavaBeanSerializer implements ObjectSerializer {
|
||||
}
|
||||
|
||||
public JavaBeanSerializer(Class<?> clazz, Map<String, String> aliasMap){
|
||||
this.features = TypeUtils.getSerializeFeatures(clazz);
|
||||
|
||||
{
|
||||
List<FieldSerializer> getterList = new ArrayList<FieldSerializer>();
|
||||
List<FieldInfo> fieldInfoList = TypeUtils.computeGetters(clazz, aliasMap, false);
|
||||
@@ -83,15 +85,6 @@ public class JavaBeanSerializer implements ObjectSerializer {
|
||||
|
||||
sortedGetters = getterList.toArray(new FieldSerializer[getterList.size()]);
|
||||
}
|
||||
|
||||
{
|
||||
JSONType annotation = clazz.getAnnotation(JSONType.class);
|
||||
if (annotation != null) {
|
||||
for (SerializerFeature feature : annotation.serialzeFeatures()) {
|
||||
features = SerializerFeature.config(features, feature, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isWriteClassName(JSONSerializer serializer, Object obj, Type fieldType, Object fieldName) {
|
||||
@@ -119,7 +112,7 @@ public class JavaBeanSerializer implements ObjectSerializer {
|
||||
}
|
||||
|
||||
SerialContext parent = serializer.getContext();
|
||||
serializer.setContext(parent, object, fieldName, 0);
|
||||
serializer.setContext(parent, object, fieldName, features);
|
||||
|
||||
final boolean writeAsArray = serializer.isWriteAsArray(object, fieldType);
|
||||
|
||||
|
||||
@@ -1309,6 +1309,16 @@ public class TypeUtils {
|
||||
return clazz.getAnnotation(JSONType.class);
|
||||
}
|
||||
|
||||
public static int getSerializeFeatures(Class<?> clazz) {
|
||||
JSONType annotation = clazz.getAnnotation(JSONType.class);
|
||||
|
||||
if (annotation == null) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return SerializerFeature.of(annotation.serialzeFeatures());
|
||||
}
|
||||
|
||||
public static String decapitalize(String name) {
|
||||
if (name == null || name.length() == 0) {
|
||||
return name;
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.alibaba.json.bvt.bug;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.annotation.JSONType;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
public class Issue101_NoneASM extends TestCase {
|
||||
|
||||
public void test_for_issure() throws Exception {
|
||||
VO vo = new VO();
|
||||
vo.a = new Object();
|
||||
vo.b = vo.a;
|
||||
vo.c = vo.a;
|
||||
|
||||
String text = JSON.toJSONString(vo);
|
||||
Assert.assertEquals("{\"a\":{},\"b\":{},\"c\":{}}", text);
|
||||
}
|
||||
|
||||
@JSONType(serialzeFeatures=SerializerFeature.DisableCircularReferenceDetect)
|
||||
private static class VO {
|
||||
|
||||
private Object a;
|
||||
private Object b;
|
||||
private Object c;
|
||||
|
||||
public Object getA() {
|
||||
return a;
|
||||
}
|
||||
|
||||
public void setA(Object a) {
|
||||
this.a = a;
|
||||
}
|
||||
|
||||
public Object getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public void setB(Object b) {
|
||||
this.b = b;
|
||||
}
|
||||
|
||||
public Object getC() {
|
||||
return c;
|
||||
}
|
||||
|
||||
public void setC(Object c) {
|
||||
this.c = c;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user