Skip to content

[BUG] 带 filter 和 Feature 参数的 JSON.toJSONString 方法,指定任意filter参数 和 Feature.ReferenceDetection Feature参数,转出的 json 自引用属性后面缺少冒号“:” ,格式错误 #2678

@anyxgit

Description

@anyxgit

问题描述

使用带 filter 参数的 JSON.toJSONString 方法,同时指定 Feature.ReferenceDetection Feature参数
转出的 json 自引用属性后面缺少冒号“:” ,格式错误

环境信息

请填写以下信息:

  • OS信息: Win10 4Core 16 GB
  • JDK信息: Openjdk 1.8.0_312
  • 版本信息:Fastjson2 2.0.51

重现步骤

见代码

  1. 使用带 filter 参数的 JSON.toJSONString 方法,同时指定 Feature.ReferenceDetection Feature参数
  2. 输入 任意带循环引用 的数据
  3. 出现 转出的 json 自引用属性后面缺少冒号“:” ,格式错误
        Map<String, Object> map = new HashMap<>();
        map.put("k", "v");
        map.put("selfRef", map);
        // 无 filter 参数的 toJSONString
        String json = com.alibaba.fastjson2.JSON.toJSONString(map, Feature.ReferenceDetection);
        System.out.println(json);  // 输出:{"selfRef":{"$ref":".."},"k":"v"} ,结果正确
        
        PropertyPreFilter propertyJsonFilter = (serializer, source, property) -> true;
        // 带 filter 参数的 toJSONString
        String badJson = com.alibaba.fastjson2.JSON.toJSONString(map, propertyJsonFilter, Feature.ReferenceDetection);
        System.out.println(badJson); // 输出:{"selfRef"{"$ref":"$"},"k":"v"} ,自引用属性后面少了冒号“:”

期待的正确结果

输出自引用属性后不缺少冒号“:”的正确格式 json 字符串

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixed

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions