mirror of
https://github.com/alibaba/fastjson.git
synced 2026-10-21 08:46:33 +08:00
完善JSONFeild的SerializeFeature支持
git-svn-id: http://code.alibabatech.com/svn/fastjson/trunk/fastjson@1287 fb902949-7fc4-4485-a80f-4d6ff335c879
This commit is contained in:
@@ -1053,6 +1053,7 @@ public class ASMSerializerFactory implements Opcodes {
|
||||
boolean writeNull = false;
|
||||
boolean writeNullNumberAsZero = false;
|
||||
boolean writeNullStringAsEmpty = false;
|
||||
boolean writeNullBooleanAsFalse = false;
|
||||
JSONField annotation = fieldInfo.getAnnotation(JSONField.class);
|
||||
if (annotation != null) {
|
||||
for (SerializerFeature feature : annotation.serialzeFeatures()) {
|
||||
@@ -1062,6 +1063,8 @@ public class ASMSerializerFactory implements Opcodes {
|
||||
writeNullNumberAsZero = true;
|
||||
} else if (feature == SerializerFeature.WriteNullStringAsEmpty) {
|
||||
writeNullStringAsEmpty = true;
|
||||
} else if (feature == SerializerFeature.WriteNullBooleanAsFalse) {
|
||||
writeNullBooleanAsFalse = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1100,8 +1103,14 @@ public class ASMSerializerFactory implements Opcodes {
|
||||
"(CLjava/lang/String;)V");
|
||||
}
|
||||
} else if (propertyClass == Boolean.class) {
|
||||
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "writeFieldNullBoolean",
|
||||
"(CLjava/lang/String;)V");
|
||||
if (writeNullBooleanAsFalse) {
|
||||
mw.visitInsn(ICONST_0);
|
||||
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "writeFieldValue",
|
||||
"(CLjava/lang/String;Z)V");
|
||||
} else {
|
||||
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "writeFieldNullBoolean",
|
||||
"(CLjava/lang/String;)V");
|
||||
}
|
||||
} else if (Collection.class.isAssignableFrom(propertyClass) || propertyClass.isArray()) {
|
||||
mw.visitMethodInsn(INVOKEVIRTUAL, getType(SerializeWriter.class), "writeFieldNullList",
|
||||
"(CLjava/lang/String;)V");
|
||||
|
||||
@@ -28,6 +28,7 @@ public class ObjectFieldSerializer extends FieldSerializer {
|
||||
private String format;
|
||||
private boolean writeNumberAsZero = false;
|
||||
boolean writeNullStringAsEmpty = false;
|
||||
boolean writeNullBooleanAsFalse = false;
|
||||
|
||||
public ObjectFieldSerializer(FieldInfo fieldInfo) {
|
||||
super(fieldInfo);
|
||||
@@ -46,6 +47,8 @@ public class ObjectFieldSerializer extends FieldSerializer {
|
||||
writeNumberAsZero = true;
|
||||
} else if (feature == SerializerFeature.WriteNullStringAsEmpty) {
|
||||
writeNullStringAsEmpty = true;
|
||||
} else if (feature == SerializerFeature.WriteNullBooleanAsFalse) {
|
||||
writeNullBooleanAsFalse = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,6 +82,9 @@ public class ObjectFieldSerializer extends FieldSerializer {
|
||||
} else if (writeNullStringAsEmpty && String.class == runtimeFieldClass) {
|
||||
serializer.getWriter().write("\"\"");
|
||||
return;
|
||||
} else if (writeNullBooleanAsFalse && Boolean.class == runtimeFieldClass) {
|
||||
serializer.getWriter().write("false");
|
||||
return;
|
||||
}
|
||||
|
||||
fieldSerializer.write(serializer, null);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.alibaba.json.test.bvt;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.alibaba.fastjson.serializer.SerializeConfig;
|
||||
import com.alibaba.fastjson.serializer.SerializerFeature;
|
||||
|
||||
public class FeaturesTest5 extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
SerializeConfig config = new SerializeConfig();
|
||||
config.setAsmEnable(false);
|
||||
|
||||
String text = JSON.toJSONString(new Entity(), config);
|
||||
Assert.assertEquals("{\"value\":false}", text);
|
||||
}
|
||||
|
||||
public void test_1() throws Exception {
|
||||
SerializeConfig config = new SerializeConfig();
|
||||
config.setAsmEnable(true);
|
||||
|
||||
String text = JSON.toJSONString(new Entity(), config);
|
||||
Assert.assertEquals("{\"value\":false}", text);
|
||||
}
|
||||
|
||||
public static class Entity {
|
||||
|
||||
private Boolean value;
|
||||
|
||||
@JSONField(serialzeFeatures = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullBooleanAsFalse })
|
||||
public Boolean getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user