[alibaba/fastjson]AfterFilter附加对象时$ref引用出现null属性名($ref: "$.data[0].null")

2024-05-23 750 views
5

category:是通过AfterFilter加入的 category2:对象本身定义的属性

image

https://github.com/alibaba/fastjson/blob/e2cfb4f3461b965e18aa941321ff08ff127abcc8/src/main/java/com/alibaba/fastjson/serializer/AfterFilter.java#L24

  protected final void writeKeyValue(char seperator, String key, Object value) {
        if (seperator != '\0') {
            out.write(seperator);
        }
        out.writeFieldName(key);
        write(value);
    }

JSONSerializer.write 方法未传递fieldName,此处可以改为writeWithFieldName

image

最终JSONSerializer.setContext 时fieldName为空。

回答

4

能否提供一个重现问题的testcase

8

能否提供一个重现问题的testcase

RefAfterFilterTest.zip 已提供RefAfterFilterTest.main,执行结果 [{"category":{"name":"category"},"name":"item1","afterFilterCategory":{"name":"afterFilterCategory"}},{"category":{"$ref":"$[0].category"},"name":"item2","afterFilterCategory":{"$ref":"$[0].null"}}]

3

@wenshao 另外一个问题由于当前线程变量serializerLocal置空引发的

RefAfterFilterTest-null.zip Exception in thread "main" java.lang.NullPointerException at com.alibaba.fastjson.serializer.AfterFilter.writeKeyValue(AfterFilter.java:24) at com.alibaba.fastjson.serializer.RefAfterFilterTest.writeAfter(RefAfterFilterTest.java:27) at com.alibaba.fastjson.serializer.AfterFilter.writeAfter(AfterFilter.java:16) at com.alibaba.fastjson.serializer.JavaBeanSerializer.writeAfter(JavaBeanSerializer.java:834)

image