Skip to content

Commit

Permalink
add class level SerializeFilter support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 19, 2016
1 parent 1cd9d07 commit 65c7c0f
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 163 deletions.
Expand Up @@ -258,7 +258,9 @@ public ObjectSerializer createJavaBeanSerializer(Class<?> clazz, Map<String, Str
Label _else = new Label();

mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeDirect", "()Z");
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.className, "nature", JavaBeanSerializer_desc);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "writeDirect", "(" + JavaBeanSerializer_desc + ")Z");
mw.visitJumpInsn(IFNE, _else);
mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
Expand Down Expand Up @@ -1528,7 +1530,7 @@ private void _processValue(MethodVisitor mw, FieldInfo property, Context context
mw.visitMethodInsn(INVOKEVIRTUAL,
JSONSerializer,
"processValue",
"(" + JavaBeanSerializer_desc + "Ljava/lang/Object;Ljava/lang/String;" + valueDesc + ")Ljava/lang/Object;");
"(" + desc(SerializeFilterable.class) + "Ljava/lang/Object;Ljava/lang/String;" + valueDesc + ")Ljava/lang/Object;");

mw.visitVarInsn(ASTORE, Context.processValue);

Expand All @@ -1550,6 +1552,8 @@ private void _processKey(MethodVisitor mw, FieldInfo property, Context context)
Class<?> propertyClass = property.fieldClass;

mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, 0);
mw.visitFieldInsn(GETFIELD, context.className, "nature", JavaBeanSerializer_desc);
mw.visitVarInsn(ALOAD, Context.obj);
mw.visitVarInsn(ALOAD, Context.fieldName);

Expand Down Expand Up @@ -1592,7 +1596,7 @@ private void _processKey(MethodVisitor mw, FieldInfo property, Context context)
mw.visitMethodInsn(INVOKEVIRTUAL,
JSONSerializer,
"processKey",
"(Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;");
"(" + desc(SerializeFilterable.class) + "Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/String;");

mw.visitVarInsn(ASTORE, Context.fieldName);

Expand Down
181 changes: 40 additions & 141 deletions src/main/java/com/alibaba/fastjson/serializer/JSONSerializer.java
Expand Up @@ -20,7 +20,6 @@
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.List;
Expand All @@ -34,32 +33,22 @@
/**
* @author wenshao[szujobs@hotmail.com]
*/
public class JSONSerializer {
public class JSONSerializer extends SerializeFilterable {

private final SerializeConfig config;

public final SerializeWriter out;

protected List<BeforeFilter> beforeFilters = null;
protected List<AfterFilter> afterFilters = null;
protected List<PropertyFilter> propertyFilters = null;
protected List<ValueFilter> valueFilters = null;
protected List<NameFilter> nameFilters = null;
protected List<PropertyPreFilter> propertyPreFilters = null;
protected List<LabelFilter> labelFilters = null;
protected List<ContextValueFilter> contextValueFilters = null;

private int indentCount = 0;
private String indent = "\t";
private int indentCount = 0;
private String indent = "\t";

private String dateFormatPattern;
private DateFormat dateFormat;

protected IdentityHashMap<Object, SerialContext> references = null;
protected IdentityHashMap<Object, SerialContext> references = null;
protected SerialContext context;
protected TimeZone timeZone = JSON.defaultTimeZone;
protected Locale locale = JSON.defaultLocale;

protected TimeZone timeZone = JSON.defaultTimeZone;
protected Locale locale = JSON.defaultLocale;

public JSONSerializer(){
this(new SerializeWriter(), SerializeConfig.getGlobalInstance());
Expand Down Expand Up @@ -189,29 +178,13 @@ public void writeReference(Object object) {
out.write("\"}");
}
}

public List<ValueFilter> getValueFilters() {
if (valueFilters == null) {
valueFilters = new ArrayList<ValueFilter>();
}

return valueFilters;
}

public boolean checkValue() {
return (valueFilters != null && valueFilters.size() > 0) //
|| (contextValueFilters != null && contextValueFilters.size() > 0)
|| out.writeNonStringValueAsString;
}

public List<ContextValueFilter> getContextValueFilters() {
if (contextValueFilters == null) {
contextValueFilters = new ArrayList<ContextValueFilter>();
}

return contextValueFilters;
}

public int getIndentCount() {
return indentCount;
}
Expand All @@ -231,58 +204,6 @@ public void println() {
}
}

public List<BeforeFilter> getBeforeFilters() {
if (beforeFilters == null) {
beforeFilters = new ArrayList<BeforeFilter>();
}

return beforeFilters;
}

public List<AfterFilter> getAfterFilters() {
if (afterFilters == null) {
afterFilters = new ArrayList<AfterFilter>();
}

return afterFilters;
}

public boolean hasNameFilters() {
return nameFilters != null && nameFilters.size() > 0;
}

public List<NameFilter> getNameFilters() {
if (nameFilters == null) {
nameFilters = new ArrayList<NameFilter>();
}

return nameFilters;
}

public List<PropertyPreFilter> getPropertyPreFilters() {
if (propertyPreFilters == null) {
propertyPreFilters = new ArrayList<PropertyPreFilter>();
}

return propertyPreFilters;
}

public List<LabelFilter> getLabelFilters() {
if (labelFilters == null) {
labelFilters = new ArrayList<LabelFilter>();
}

return labelFilters;
}

public List<PropertyFilter> getPropertyFilters() {
if (propertyFilters == null) {
propertyFilters = new ArrayList<PropertyFilter>();
}

return propertyFilters;
}

public SerializeWriter getWriter() {
return out;
}
Expand Down Expand Up @@ -400,24 +321,18 @@ public void close() {
* only invoke by asm byte
* @return
*/
public boolean writeDirect() {
public boolean writeDirect(JavaBeanSerializer javaBeanDeser) {
return out.writeDirect //
&& beforeFilters == null
&& afterFilters == null
&& valueFilters == null //
&& contextValueFilters == null //
&& propertyFilters == null //
&& nameFilters == null
&& propertyPreFilters == null
&& labelFilters == null
&& this.writeDirect
&& javaBeanDeser.writeDirect
;
}

public FieldInfo getFieldInfo() {
return null;
}

public Object processValue(JavaBeanSerializer javaBeanDeser, //
public Object processValue(SerializeFilterable javaBeanDeser, //
Object object, //
String key, //
Object propertyValue) {
Expand All @@ -436,21 +351,41 @@ public Object processValue(JavaBeanSerializer javaBeanDeser, //
}
}

List<ContextValueFilter> contextValueFilters = this.contextValueFilters;
if (contextValueFilters != null) {
BeanContext fieldContext = javaBeanDeser.getFieldSerializer(key).fieldContext;
for (ContextValueFilter valueFilter : contextValueFilters) {
if (javaBeanDeser.valueFilters != null) {
for (ValueFilter valueFilter : javaBeanDeser.valueFilters) {
propertyValue = valueFilter.process(object, key, propertyValue);
}
}

if (this.contextValueFilters != null) {
BeanContext fieldContext = javaBeanDeser.getBeanContext(key);
for (ContextValueFilter valueFilter : this.contextValueFilters) {
propertyValue = valueFilter.process(fieldContext, object, key, propertyValue);
}
}

if (javaBeanDeser.contextValueFilters != null) {
BeanContext fieldContext = javaBeanDeser.getBeanContext(key);
for (ContextValueFilter valueFilter : javaBeanDeser.contextValueFilters) {
propertyValue = valueFilter.process(fieldContext, object, key, propertyValue);
}
}

return propertyValue;
}

public String processKey(Object object, String key, Object propertyValue) {
List<NameFilter> nameFilters = this.nameFilters;
if (nameFilters != null) {
for (NameFilter nameFilter : nameFilters) {
public String processKey(SerializeFilterable javaBeanDeser, //
Object object, //
String key, //
Object propertyValue) {
if (this.nameFilters != null) {
for (NameFilter nameFilter : this.nameFilters) {
key = nameFilter.process(object, key, propertyValue);
}
}

if (javaBeanDeser.nameFilters != null) {
for (NameFilter nameFilter : javaBeanDeser.nameFilters) {
key = nameFilter.process(object, key, propertyValue);
}
}
Expand Down Expand Up @@ -528,41 +463,5 @@ public boolean applyLabel(String label) {
return true;
}

public void addFilter(SerializeFilter filter) {
if (filter == null) {
return;
}

if (filter instanceof PropertyPreFilter) {
this.getPropertyPreFilters().add((PropertyPreFilter) filter);
}

if (filter instanceof NameFilter) {
this.getNameFilters().add((NameFilter) filter);
}

if (filter instanceof ValueFilter) {
this.getValueFilters().add((ValueFilter) filter);
}

if (filter instanceof ContextValueFilter) {
this.getContextValueFilters().add((ContextValueFilter) filter);
}

if (filter instanceof PropertyFilter) {
this.getPropertyFilters().add((PropertyFilter) filter);
}

if (filter instanceof BeforeFilter) {
this.getBeforeFilters().add((BeforeFilter) filter);
}

if (filter instanceof AfterFilter) {
this.getAfterFilters().add((AfterFilter) filter);
}

if (filter instanceof LabelFilter) {
this.getLabelFilters().add((LabelFilter) filter);
}
}

}

0 comments on commit 65c7c0f

Please sign in to comment.