Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Commit

Permalink
bug fixed for BeanToArray with PropertyFilters. for issue #1580
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 21, 2018
1 parent 2d52f4c commit 122a965
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,23 @@ public JavaBeanSerializer createJavaBeanSerializer(SerializeBeanInfo beanInfo) t
private void generateWriteAsArray(Class<?> clazz, MethodVisitor mw, FieldInfo[] getters,
Context context) throws Exception {

Label nonPropertyFilters_ = new Label();
mw.visitVarInsn(ALOAD, Context.serializer);
mw.visitVarInsn(ALOAD, 0);
mw.visitMethodInsn(INVOKEVIRTUAL, JSONSerializer, "hasPropertyFilters", "(" + SerializeFilterable_desc + ")Z");
mw.visitJumpInsn(IFNE, nonPropertyFilters_);
mw.visitVarInsn(ALOAD, 0);
mw.visitVarInsn(ALOAD, 1);
mw.visitVarInsn(ALOAD, 2);
mw.visitVarInsn(ALOAD, 3);
mw.visitVarInsn(ALOAD, 4);
mw.visitVarInsn(ILOAD, 5);
mw.visitMethodInsn(INVOKESPECIAL, JavaBeanSerializer,
"writeNoneASM", "(L" + JSONSerializer
+ ";Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/reflect/Type;I)V");
mw.visitInsn(RETURN);

mw.visitLabel(nonPropertyFilters_);
mw.visitVarInsn(ALOAD, context.var("out"));
mw.visitVarInsn(BIPUSH, '[');
mw.visitMethodInsn(INVOKEVIRTUAL, SerializeWriter, "write", "(I)V");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public boolean hasNameFilters(SerializeFilterable filterable) {
|| (filterable.nameFilters != null && filterable.nameFilters.size() > 0);
}

public boolean hasPropertyFilters(SerializeFilterable filterable) {
return (propertyFilters != null && propertyFilters.size() > 0) //
|| (filterable.propertyFilters != null && filterable.propertyFilters.size() > 0);
}

public int getIndentCount() {
return indentCount;
}
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_1500/Issue1580.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.alibaba.json.bvt.issue_1500;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import junit.framework.TestCase;

public class Issue1580 extends TestCase {
public void test_for_issue() throws Exception {
SimplePropertyPreFilter classAFilter = new SimplePropertyPreFilter(Model.class, "code");
SerializeFilter[] filters =new SerializeFilter[]{classAFilter};

Model model = new Model();
model.code = 1001;
model.name = "N1";

String json = JSON.toJSONString(model, filters, SerializerFeature.BeanToArray );
assertEquals("[1001,null]", json);
}

public static class Model {
private int code;
private String name;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}

0 comments on commit 122a965

Please sign in to comment.