Skip to content

Commit

Permalink
bug fix for multi ValueFilter, for issue #1078
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 29, 2023
1 parent 6a729b0 commit 8bab508
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1475,7 +1475,11 @@ protected void configFilter(Filter... filters) {
}

if (filter instanceof ValueFilter) {
this.valueFilter = (ValueFilter) filter;
if (this.valueFilter == null) {
this.valueFilter = (ValueFilter) filter;
} else {
this.valueFilter = ValueFilter.compose(this.valueFilter, (ValueFilter) filter);
}
}

if (filter instanceof PropertyFilter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.alibaba.fastjson2.issues_1000;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.ValueFilter;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class Issue1078 {
@Test
public void test() {
Map<String, String> map = new HashMap<>();
map.put("a", "b");

assertEquals(
"{\"a\":\"b12\"}",
JSON.toJSONString(
map,
new ValueFilter[]{new TestValueFilter1(), new TestValueFilter2()},
JSONWriter.Feature.WriteMapNullValue
)
);

assertEquals(
"{\"a\":\"b21\"}",
JSON.toJSONString(
map,
new ValueFilter[]{new TestValueFilter2(), new TestValueFilter1()},
JSONWriter.Feature.WriteMapNullValue
)
);
}

public static class TestValueFilter1 implements ValueFilter {
@Override
public Object apply(Object o, String s, Object o1) {
return o1.toString() + "1";
}
}

public static class TestValueFilter2 implements ValueFilter {
@Override
public Object apply(Object o, String s, Object o1) {
return o1.toString() + "2";
}
}
}

0 comments on commit 8bab508

Please sign in to comment.