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] 使用TypeReference转换成自定义对象时,json中有注释会报错 #3304

Open
dingpeng1107 opened this issue Jan 21, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@dingpeng1107
Copy link

问题描述

使用TypeReference转换成自定义对象时,json中有注释会报错

环境信息

请填写以下信息:

  • OS信息: win11 23H2 i5-13500 2.5GHz 16G
  • JDK信息: jdk 1.8.0_171
  • 版本信息:fastjson2 2.0.54

重现步骤

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

  1. 使用main方法
  2. 出现 illegal fieldName input/ 错误
public class TempTest {

    public static void main(String[] args) {
        final String json = "{\n" + "  \"mysql\": {\n" + "    \"instruction\": \"mysql 测试\",\n" + "    // test\n" + "    \"source\": \"LOCAL\",\n" + "    \"Size\": 1\n" + "  }\n" + "}";
        Map<String, TParam> paramMap = JSON.parseObject(json, new TypeReference<Map<String, TParam>>(TParam.class) {});
        paramMap.forEach((s, param) -> {
            System.out.println(param);
        });
    }

    @Data
    class TParam {
        private String instruction;
        private String source;
        private String Size;
    }
}

json数据如下

{
  "mysql": {
    "instruction": "mysql 测试",
    // test
    "source": "LOCAL",
    "Size": 1
  }
}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

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

Exception in thread "main" com.alibaba.fastjson2.JSONException: illegal fieldName input/, offset 51, character /, line 4, column 6, fastjson-version 2.0.54
{
  "mysql": {
    "instruction": "mysql 测试",
    // test
    "source": "LOCAL",
    "Size": 1
  }
}
	at com.alibaba.fastjson2.JSONReaderUTF16.readFieldNameHashCode(JSONReaderUTF16.java:1155)
	at com.alibaba.fastjson2.reader.ORG_1_4_TParam.readObject(Unknown Source)
	at com.alibaba.fastjson2.reader.ObjectReaderImplMapTyped.readObject(ObjectReaderImplMapTyped.java:408)
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1100)
	at com.thunisoft.hyydetl.TempTest.main(TempTest.java:18)

附加信息

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

出乎意料的是,如果我不指定自定义的对象,使用Map,程序就正常运行

        final String json = "{\n" + "  \"mysql\": {\n" + "    \"instruction\": \"mysql 测试\",\n" + "    // test\n" + "    \"source\": \"LOCAL\",\n" + "    \"Size\": 1\n" + "  }\n" + "}";
        Map<String, Map> paramMap = JSON.parseObject(json, new TypeReference<Map<String, Map>>(Map.class) {});
        paramMap.forEach((s, param) -> {
            System.out.println(param);
        });
@dingpeng1107 dingpeng1107 added the bug Something isn't working label Jan 21, 2025
@dingpeng1107 dingpeng1107 changed the title [BUG] [BUG] 使用TypeReference转换成自定义对象时,json中有注释会报错 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant