Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]fastjson2.0.19传入多个ValueFilter的实现类时,只生效最后一个 #1078

Closed
MOSLX opened this issue Jan 17, 2023 · 2 comments
Closed
Labels
bug Something isn't working fixed
Milestone

Comments

@MOSLX
Copy link

MOSLX commented Jan 17, 2023

问题描述

项目升级了fastjson2.0.19,配置多个ValueFilter的实现类,目前只会生效最后一个加入的,升级到2.0.23也是有同样的问题。
翻看源码发现JSONWriter.Context.configFilter中后一个ValueFilter的实现类会覆盖前一个。是否是ValueFilter的生效机制出现了变化,如何像fastjson1一样让所有的ValueFilter都生效呢?请大佬帮忙解答一下,多谢!

环境信息

  • OS信息: [Windows 10 1809 4Core 1.8GHz 16 GB]
  • JDK信息: [jdk 1.8.0_172]
  • 版本信息:[Fastjson2 2.0.19兼容升级]
public class Test{
  
  public static class TestValueFilter1 implements ValueFilter{
    @Override
    public Object apply(Object o, String s, Object o1){
        System.out.println("print test1 value filter");
        return o1;
    }    
  }

 public static class TestValueFilter2 implements ValueFilter{
    @Override
    public Object apply(Object o, String s, Object o1){
        System.out.println("print test2 value filter");
        return o1;
    }    
  }

  public static void main(String[] args){
    Map<String, String> map = new HashMap<>();
    map.put("a", "b");
    
    ValueFilter[] filters = new ValueFilter[]{new TestValueFilter1(), new TestValueFilter2()};
    System.out.println(JSON.toJSONString(map, filters, JSONWriter.Feature.WriteMapNullValue));
  }
}

期待的正确结果

print test1 value filter
print test2 value filter
{"a":"b"}

实际日志输出

print test2 value filter
{"a":"b"}

@MOSLX MOSLX added the bug Something isn't working label Jan 17, 2023
@wenshao wenshao added this to the 2.0.24 milestone Jan 18, 2023
@wenshao
Copy link
Member

wenshao commented Jan 29, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.24-SNAPSHOT/
问题已修复,请用2.0.24-SNAPSHOT版本帮忙验证,2.0.24-SNAPSHOT预计在2月中旬发布。在此之前,你也可以用ValueFilter#compose方法组合两个ValueFilter。

@wenshao wenshao added the fixed label Jan 29, 2023
@wenshao
Copy link
Member

wenshao commented Feb 18, 2023

@wenshao wenshao closed this as completed Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants