[BUG] 使用TypeReference转换成自定义对象时,json中有注释会报错
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
使用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/` 错误
```java
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 paramMap = JSON.parseObject(json, new TypeReference>(TParam.class) {});
paramMap.forEach((s, param) -> {
System.out.println(param);
});
}
@Data
class TParam {
private String instruction;
private String source;
private String Size;
}
}
```
json数据如下
```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,程序就正常运行
```java
final String json = "{\n" + " \"mysql\": {\n" + " \"instruction\": \"mysql 测试\",\n" + " // test\n" + " \"source\": \"LOCAL\",\n" + " \"Size\": 1\n" + " }\n" + "}";
Map paramMap = JSON.parseObject(json, new TypeReference>(Map.class) {});
paramMap.forEach((s, param) -> {
System.out.println(param);
});
```
Contributor guide
Research direction
Start with the JSON.parseObject call using TypeReference> and trace the reported failure at JSONReaderUTF16.readFieldNameHashCode line 1155. Compare this typed-object path with the Map parsing example, then verify the supplied JSON containing // test parses without an exception and preserves the expected fields.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100