alibaba / alibaba/fastjson2

[BUG] getObject时,类型错误,未抛出异常,丢失数据精度

Open
#3,571 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
例如,程序需要动态解析具体json字段,但是由于配置Class clazz类型错误,会造成数据丢失精度;

### 环境信息
*请填写以下信息:*
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson 2.0.53]

### 重现步骤
*如何操作可以重现该问题:*

1. 原本要读取Long类型的数据值,但由于配置错误,配置成了Integer类型,造成数据精度丢失
```java
JSONObject object = new JSONObject();
object.put("val", 813462682693795987L);
//配置错误Integer类型
System.out.println(object.getObject("val", Integer.class));//147

//实际应该配置的Long类型
System.out.println(object.getObject("val", Long.class));//813462682693795987
```

### 期待的正确结果
期望类型不符应该抛出异常,而不是丢失精度后转换成错误的值;

### 相关日志输出
147
813462682693795987

Contributor guide

Open the contributing guide

Research direction

Start at the JSONObject.getObject("val", Class) call shown in the reproduction and inspect how the requested Integer type is handled. Reproduce the Long-to-Integer example, then verify that an incompatible type raises an exception instead of returning the truncated value.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.