序列化支持Public Field

git-svn-id: http://code.alibabatech.com/svn/fastjson/trunk/fastjson@1425 fb902949-7fc4-4485-a80f-4d6ff335c879
This commit is contained in:
wenshao
2011-10-06 23:28:56 +00:00
parent 922838f16d
commit 8cd5f94a9c
6 changed files with 84 additions and 8 deletions
@@ -342,8 +342,7 @@ public class ASMSerializerFactory implements Opcodes {
for (int i = 0; i < size; ++i) {
FieldInfo property = getters.get(i);
Method method = property.getMethod();
Class<?> propertyClass = method.getReturnType();
Class<?> propertyClass = property.getFieldClass();
mw.visitLdcInsn(property.getName());
mw.visitVarInsn(ASTORE, context.fieldName());
@@ -741,7 +740,7 @@ public class ASMSerializerFactory implements Opcodes {
private void _list(Class<?> clazz, MethodVisitor mw, FieldInfo property, Context context) {
Method method = property.getMethod();
Type propertyType = method.getGenericReturnType();
Type propertyType = property.getFieldType();
Type elementType;
if (propertyType instanceof Class) {
@@ -36,7 +36,7 @@ public abstract class FieldSerializer implements Comparable<FieldSerializer> {
public FieldSerializer(FieldInfo fieldInfo){
super();
this.fieldInfo = fieldInfo;
fieldInfo.getMethod().setAccessible(true);
fieldInfo.setAccessible(true);
this.double_quoted_fieldPrefix = '"' + fieldInfo.getName() + "\":";
@@ -89,7 +89,7 @@ public abstract class FieldSerializer implements Comparable<FieldSerializer> {
}
public Object getPropertyValue(Object object) throws Exception {
return getMethod().invoke(object);
return fieldInfo.get(object);
}
public abstract void writeProperty(JSONSerializer serializer, Object propertyValue) throws Exception;
@@ -184,7 +184,7 @@ public class JavaBeanSerializer implements ObjectSerializer {
}
public FieldSerializer createFieldSerializer(FieldInfo fieldInfo) {
Class<?> clazz = fieldInfo.getMethod().getReturnType();
Class<?> clazz = fieldInfo.getFieldClass();
if (clazz == Number.class) {
return new NumberFieldSerializer(fieldInfo);
@@ -93,7 +93,9 @@ public class FieldInfo implements Comparable<FieldInfo> {
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
T annotation = null;
annotation = method.getAnnotation(annotationClass);
if (method != null) {
annotation = method.getAnnotation(annotationClass);
}
if (annotation == null) {
if (field != null) {
@@ -109,7 +111,16 @@ public class FieldInfo implements Comparable<FieldInfo> {
Object value = method.invoke(javaObject, new Object[0]);
return value;
}
return field.get(javaObject);
}
public void setAccessible(boolean flag) throws SecurityException {
if (method != null) {
method.setAccessible(flag);
return;
}
field.setAccessible(flag);
}
}
@@ -26,6 +26,7 @@ public class Bug_101_for_rongganlin extends TestCase {
structure.groups = groups;
System.out.println(JSON.toJSON(structure));
System.out.println(JSON.toJSONString(structure));
}
class Structure {
@@ -0,0 +1,65 @@
package com.alibaba.json.test.bvt.bug;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import com.alibaba.fastjson.JSON;
public class Bug_101_for_rongganlin_case2 extends TestCase {
public void test_for_bug() throws Exception {
Structure structure = new Structure();
List<Group> groups = new ArrayList<Group>();
List<Element> elemA = new ArrayList<Element>();
elemA.add(new Text().set("t.a", "v.t.a"));
elemA.add(new Image().set("i.a", "v.i.a"));
groups.add(new Group().set(elemA));
List<Element> elemB = new ArrayList<Element>();
elemB.add(new Text().set("t.b", "v.t.b"));
elemB.add(new Image().set("i.b", "v.i.b"));
groups.add(new Group().set(elemB));
structure.groups = groups;
System.out.println(JSON.toJSONString(structure));
}
public static class Structure {
public List<Group> groups;
}
public static class Group implements Object {
public List<Element> elements;
public Group set(List<Element> items) {
this.elements = items;
return this;
}
}
public static interface Object {
}
public static abstract class Element {
public String key, value;
public Element set(String key, String value) {
this.key = key;
this.value = value;
return this;
}
}
public static class Text extends Element {
}
public static class Image extends Element {
}
}