[BUG] 使用 JSONReader 无法正常解序列化对象
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
我通过 HTTP Client 拉取到一个 `InputStream`,同过如下代码进行解序列化:
```kotlin
internal fun none(input: InputStream, clazz: KClass): T {
if (clazz == String::class) return InputStreamReader(input).use { it.readText() as T }
// val text = InputStreamReader(input).use { it.readText() }
// return JSON.parseObject(text, clazz.java)
return JSONReader.of(input, StandardCharsets.UTF_8).use {
val objectReader = it.getObjectReader(clazz.java)
objectReader.createInstance() as T
}
}
```
代码会报错:
```
Exception in thread "ForkJoinPool-1-worker-1" com.alibaba.fastjson2.JSONException: create instance error, class com.bonbonwork.code.httpclient.QwenResponseBody
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:415)
at com.alibaba.fastjson2.reader.ObjectReader.createInstance(ObjectReader.java:21)
at -- 这里省略我的代码的对战信息 --
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1423)
at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:387)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1312)
at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1843)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1808)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:188)
Caused by: com.alibaba.fastjson2.JSONException: create instance error, class com.bonbonwork.code.httpclient.QwenResponseBody
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance0(ObjectReaderAdapter.java:344)
at com.alibaba.fastjson2.reader.ObjectReaderAdapter.createInstance(ObjectReaderAdapter.java:393)
... 12 more
```
但是如果使用上述代码中被注释掉的两行代码进行解序列化就没有问题。
### 环境信息
*请填写以下信息:*
- OS信息: Mac Os 15.1.1 (24B91) 2.6 GHz 六核Intel Core i7 16GB
- JDK信息: graalvm-jdk-21.0.5
- 版本信息:FastJson2 2.0.53
### 重现步骤
1. 构建一个 `InputStream`,其中包含 Json 的数据
2. 使用我上面给到的 `none` 函数解序列化
3. 出现 `JSONException` 错误
Contributor guide
Research direction
Start with the provided Kotlin none function and reproduce the failure using an InputStream containing JSON. Read the ObjectReader.createInstance and ObjectReaderAdapter.createInstance stack-trace locations, then compare this path with the commented InputStreamReader and JSON.parseObject path. Done means the reported QwenResponseBody input can be deserialized without JSONException.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100