Skip to content

Commit

Permalink
refactor SerializeFilter implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Apr 30, 2016
1 parent 093b1b7 commit a46f5fe
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
Expand Up @@ -1418,11 +1418,11 @@ private void _apply(MethodVisitor mw, FieldInfo property, Context context) {
"(" + desc(SerializeFilterable.class) + "Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Z"); "(" + desc(SerializeFilterable.class) + "Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;)Z");
} }


private void _processValue(MethodVisitor mw, FieldInfo property, Context context, Label _end) { private void _processValue(MethodVisitor mw, FieldInfo fieldInfo, Context context, Label _end) {
Label _else_processKey = new Label(); Label _else_processKey = new Label();




Class<?> propertyClass = property.fieldClass; Class<?> propertyClass = fieldInfo.fieldClass;


if (propertyClass.isPrimitive()) { if (propertyClass.isPrimitive()) {
Label _end_checkValue = new Label(); Label _end_checkValue = new Label();
Expand All @@ -1440,6 +1440,9 @@ private void _processValue(MethodVisitor mw, FieldInfo property, Context context


mw.visitVarInsn(ALOAD, Context.serializer); mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 0);
mw.visitLdcInsn(fieldInfo.name);
mw.visitMethodInsn(INVOKEVIRTUAL, JavaBeanSerializer, "getBeanContext", "(Ljava/lang/String;)" + desc(BeanContext.class));
mw.visitVarInsn(ALOAD, Context.obj); mw.visitVarInsn(ALOAD, Context.obj);
mw.visitVarInsn(ALOAD, Context.fieldName); mw.visitVarInsn(ALOAD, Context.fieldName);


Expand Down Expand Up @@ -1509,14 +1512,17 @@ private void _processValue(MethodVisitor mw, FieldInfo property, Context context
mw.visitMethodInsn(INVOKEVIRTUAL, mw.visitMethodInsn(INVOKEVIRTUAL,
JSONSerializer, JSONSerializer,
"processValue", "processValue",
"(" + desc(SerializeFilterable.class) + "Ljava/lang/Object;Ljava/lang/String;" + valueDesc + ")Ljava/lang/Object;"); "(" + desc(SerializeFilterable.class) //
+ desc(BeanContext.class) //
+ "Ljava/lang/Object;Ljava/lang/String;" //
+ valueDesc + ")Ljava/lang/Object;");


mw.visitVarInsn(ASTORE, Context.processValue); mw.visitVarInsn(ASTORE, Context.processValue);


mw.visitVarInsn(ALOAD, Context.original); mw.visitVarInsn(ALOAD, Context.original);
mw.visitVarInsn(ALOAD, Context.processValue); mw.visitVarInsn(ALOAD, Context.processValue);
mw.visitJumpInsn(IF_ACMPEQ, _else_processKey); mw.visitJumpInsn(IF_ACMPEQ, _else_processKey);
_writeObject(mw, property, context, _end); _writeObject(mw, fieldInfo, context, _end);
mw.visitJumpInsn(GOTO, _end); mw.visitJumpInsn(GOTO, _end);


mw.visitLabel(_else_processKey); mw.visitLabel(_else_processKey);
Expand Down
Expand Up @@ -335,6 +335,7 @@ public FieldInfo getFieldInfo() {
} }


public Object processValue(SerializeFilterable javaBeanDeser, // public Object processValue(SerializeFilterable javaBeanDeser, //
BeanContext beanContext,
Object object, // Object object, //
String key, // String key, //
Object propertyValue) { Object propertyValue) {
Expand All @@ -360,21 +361,20 @@ public Object processValue(SerializeFilterable javaBeanDeser, //
} }


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


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


return propertyValue; return propertyValue;
} }



public String processKey(SerializeFilterable javaBeanDeser, // public String processKey(SerializeFilterable javaBeanDeser, //
Object object, // Object object, //
Expand Down
Expand Up @@ -186,9 +186,6 @@ public void write(JSONSerializer serializer, //
final boolean skipTransient = out.skipTransientField; final boolean skipTransient = out.skipTransientField;
final boolean ignoreNonFieldGetter = out.ignoreNonFieldGetter; final boolean ignoreNonFieldGetter = out.ignoreNonFieldGetter;


final List<ValueFilter> valueFilters = serializer.valueFilters;
final List<ContextValueFilter> contextValueFilters = serializer.contextValueFilters;

for (int i = 0; i < getters.length; ++i) { for (int i = 0; i < getters.length; ++i) {
FieldSerializer fieldSerializer = getters[i]; FieldSerializer fieldSerializer = getters[i];


Expand All @@ -211,17 +208,12 @@ public void write(JSONSerializer serializer, //
} }
} }


{ if ((!serializer.applyName(this, object, fieldInfo.name)) //
boolean apply = serializer.applyName(this, object, fieldInfo.name); || !serializer.applyLabel(this, fieldInfo.label)) {
if (!apply) {
continue;
}
}

if (!serializer.applyLabel(this, fieldInfo.label)) {
continue; continue;
} }



Object propertyValue; Object propertyValue;


try { try {
Expand All @@ -234,51 +226,16 @@ public void write(JSONSerializer serializer, //
} }
} }


boolean apply = serializer.apply(this, object, fieldInfoName, propertyValue); if (!serializer.apply(this, object, fieldInfoName, propertyValue)) {


if (!apply) {
continue; continue;
} }


String key = fieldInfoName; String key = fieldInfoName;
key = serializer.processKey(this, object, key, propertyValue); key = serializer.processKey(this, object, key, propertyValue);


if (out.writeNonStringValueAsString) {
if (propertyValue instanceof Number || propertyValue instanceof Boolean) {
propertyValue = propertyValue.toString();
}
}

Object originalValue = propertyValue; Object originalValue = propertyValue;
{ propertyValue = serializer.processValue(this, fieldSerializer.fieldContext, object, fieldInfoName,
if (valueFilters != null) { propertyValue);
for (ValueFilter valueFilter : valueFilters) {
propertyValue = valueFilter.process(object, fieldInfoName, propertyValue);
}
}

if (this.valueFilters != null) {
for (ValueFilter valueFilter : this.valueFilters) {
propertyValue = valueFilter.process(object, fieldInfoName, propertyValue);
}
}
}
{
if (contextValueFilters != null) {
for (ContextValueFilter valueFilter : contextValueFilters) {
propertyValue = valueFilter.process(fieldSerializer.fieldContext, object, fieldInfoName,
propertyValue);
}
}

if (this.contextValueFilters != null) {
for (ContextValueFilter valueFilter : this.contextValueFilters) {
propertyValue = valueFilter.process(fieldSerializer.fieldContext, object, fieldInfoName,
propertyValue);
}
}
}


if (propertyValue == null && !writeAsArray) { if (propertyValue == null && !writeAsArray) {
if ((!fieldSerializer.writeNull) && (!out.writeMapNullValue)) { if ((!fieldSerializer.writeNull) && (!out.writeMapNullValue)) {
Expand Down Expand Up @@ -471,9 +428,11 @@ public Map<String, Object> getFieldValuesMap(Object object) throws Exception {
return map; return map;
} }


@Override
public BeanContext getBeanContext(String key) { public BeanContext getBeanContext(String key) {
return getFieldSerializer(key).fieldContext; return getFieldSerializer(key).fieldContext;
} }


protected BeanContext getBeanContext(int orinal) {
return sortedGetters[orinal].fieldContext;
}
} }
Expand Up @@ -136,10 +136,10 @@ public void write(JSONSerializer serializer, Object object, Object fieldName, Ty
if ((valueFilters != null && valueFilters.size() > 0) // if ((valueFilters != null && valueFilters.size() > 0) //
|| (contextValueFilters != null && contextValueFilters.size() > 0)) { || (contextValueFilters != null && contextValueFilters.size() > 0)) {
if (entryKey == null || entryKey instanceof String) { if (entryKey == null || entryKey instanceof String) {
value = serializer.processValue(this, object, (String) entryKey, value); value = serializer.processValue(this, null, object, (String) entryKey, value);
} else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) { } else if (entryKey.getClass().isPrimitive() || entryKey instanceof Number) {
String strKey = JSON.toJSONString(entryKey); String strKey = JSON.toJSONString(entryKey);
value = serializer.processValue(this, object, strKey, value); value = serializer.processValue(this, null, object, strKey, value);
} }
} }
} }
Expand Down
Expand Up @@ -130,7 +130,4 @@ public void addFilter(SerializeFilter filter) {
} }
} }


public BeanContext getBeanContext(String key) {
return null;
}
} }

0 comments on commit a46f5fe

Please sign in to comment.