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]将java对象序列化为json字符串时报错,fastjson 1.2.83运行正常,fastjson2 2.0.50运行报错 #2639

Closed
bulain opened this issue May 29, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@bulain
Copy link

bulain commented May 29, 2024

问题描述

将java对象序列化为json字符串时报错,fastjson 1.2.83运行正常,fastjson2 2.0.50运行报错

环境信息

请填写以下信息:

  • OS信息: [Windows 11]
  • JDK信息: [Oracle OpenJDK 17.0.7]
  • 版本信息:[Fastjson2 2.0.50]

重现步骤

如何操作可以重现该问题:

  1. 使用 JSON.toJSONString() 方法
  2. 输入 test.json 数据
{
  "dets": [
    {
      "fields": [
        {
          "fname": "字段A",
          "value": ""
        },
        {
          "fname": "字段B",
          "value": ""
        }
      ]
    },
    {
      "fields": [
        {
          "fname": "字段C",
          "value": ""
        },
        {
          "fname": "字段D",
          "value": ""
        }
      ]
    }
  ]
}
  1. 出现 com.alibaba.fastjson.JSONException: toJSONString error 错误
// DynPage.java
@Data
public class DynPage {
    private List<DynArea> dets;
}
// DynArea.java
@Data
public class DynArea {
    private List<DynField> fields;
}
// DynField.java
@Data
public class DynField {
    private String fname;
    private Serializable value;
}

// FastjsonTest.java
@Slf4j
class FastjsonTest {
    @Test
    @SneakyThrows
    void dynPageToJSONString() {
        ClassPathResource resource = new ClassPathResource("test.json");
        try (InputStream is = resource.getInputStream()) {
            String json = IOUtils.toString(is, StandardCharsets.UTF_8);
            DynPage data = JSON.parseObject(json, DynPage.class);
            log.info("{}", data);
            Assertions.assertNotNull(data);

            String jsonv = JSON.toJSONString(data);
            log.info(jsonv);
            Assertions.assertNotNull(jsonv);
        }
    }
}

期待的正确结果

能够正确的序列化java对象为json字符串

相关日志输出

请复制并粘贴任何相关的日志输出。

com.alibaba.fastjson.JSONException: toJSONString error

	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1527)
	at com.bulain.fastjson.FastjsonTest.dynPageToJSONString(FastjsonTest.java:60)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.lang.NullPointerException: Cannot read field "child1" because "this.path" is null
	at com.alibaba.fastjson2.JSONWriter.setPath(JSONWriter.java:189)
	at com.alibaba.fastjson2.JSONWriter.writeReference(JSONWriter.java:172)
	at com.alibaba.fastjson2.writer.FieldWriterList.writeListValue(FieldWriterList.java:219)
	at com.alibaba.fastjson2.writer.OWG_1_1_DynPage.write(Unknown Source)
	at com.alibaba.fastjson.JSON.toJSONString(JSON.java:1519)
	... 4 more

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@bulain bulain added the bug Something isn't working label May 29, 2024
@wenshao wenshao added this to the 2.0.51 milestone May 30, 2024
@wenshao
Copy link
Member

wenshao commented May 30, 2024

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson/2.0.51-SNAPSHOT/
问题已修复,请帮忙用2.0.51-SNAPSHOT版本验证

@wenshao wenshao added the fixed label May 30, 2024
@lunxian8
Copy link

我也遇到了这个问题,用快照试了一下 解决了

@wenshao
Copy link
Member

wenshao commented Jun 1, 2024

@wenshao wenshao closed this as completed Jun 1, 2024
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

3 participants