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 AfterFilter, fix #3217
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jul 26, 2020
1 parent e970545 commit b86ca3c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public abstract class AfterFilter implements SerializeFilter {
private final static Character COMMA = Character.valueOf(',');

final char writeAfter(JSONSerializer serializer, Object object, char seperator) {
JSONSerializer last = serializerLocal.get();
serializerLocal.set(serializer);
seperatorLocal.set(seperator);
writeAfter(object);
serializerLocal.set(null);
serializerLocal.set(last);
return seperatorLocal.get();
}

Expand Down
98 changes: 98 additions & 0 deletions src/test/java/com/alibaba/json/bvt/issue_3300/Issue3217.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.alibaba.json.bvt.issue_3300;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.AfterFilter;
import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;

public class Issue3217 extends TestCase {

public void test_for_issue() throws Exception {
RefAfterFilterTest refAfterFilterTest = new RefAfterFilterTest();

List<Item> items = new ArrayList<Item>(2);
Category category = new Category("category");
items.add(new Item("item1",category));
items.add(new Item("item2",category));

System.out.println(JSON.toJSONString(items,refAfterFilterTest));
}

public static class RefAfterFilterTest extends AfterFilter {

private Category category = new Category("afterFilterCategory");

@Override
public void writeAfter(Object object) {

if (object instanceof Item) {

this.writeKeyValue("afterFilterCategory", category);
/*多加一个属性报错,原因是category是object也触发了writeAfter,当前线程变量serializer被设置为null了serializerLocal.set(null);
*这两个write换个顺序就不会报错
*/
this.writeKeyValue("afterFilterTwo", "two");

}
}
}

public static class Category {

private String name;

public Category(String name){
this.name = name;
}

public String getName() {
return name;
}

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

public static class Item {

private String name;

private Category category;

private String barcode;


public Item(String name,Category category){
this.name = name;
this.category = category;
}

public String getName() {
return name;
}

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

public Category getCategory() {
return category;
}

public void setCategory(Category category) {
this.category = category;
}

public String getBarcode() {
return barcode;
}

public void setBarcode(String barcode) {
this.barcode = barcode;
}

}
}

0 comments on commit b86ca3c

Please sign in to comment.