alibaba / alibaba/fastjson2

反序列化,字段解析不出来

Open
#2,970 5 comments 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

### 问题描述
*反序列化,字段解析不出来*

### 环境信息
*请填写以下信息:*

- OS信息: [e.g.:操作系统名称 Microsoft Windows 11 企业版]
- JDK信息: [e.g.:graalvm-ce-java17-22.3.1][jdk1.8.0_301](java8也会有同样问题)
- 版本信息:[e.g.:Fastjson2 2.0.53]

### 重现步骤

![image](https://github.com/user-attachments/assets/f0058071-1981-40da-919b-b6897c43331c)
程序运行解析不出来
--------------------------------------------------------------------------------------------------------
但是单独写的解析单元测试,是没有问题。我一直以为是编码有问题
![image](https://github.com/user-attachments/assets/a39224aa-af0c-4a54-b739-4f9c73205535)

但是设置【@JSONField(name = "signedMsg")】或者开启【JSONReader.Feature.SupportSmartMatch】是正常的。很奇怪的问题
用fastjson1.x是没有问题的。【signedMsg】跟属性名是可以对上的都解析不出来

```java
@SneakyThrows
@Test
public void gbkParseObjectTest() {
String footer = "{\"signedMsg\":\"0DF581889549E07BBB2A854D7EC9BA38532C78F47EB53857967B9E366DB4BC1CAE72506F2BD9FDB60A344CA43760354899F7A6E96B778310795C5039A27F6C6E3A77524DB0EC674ADA928EA08B463A2DC0F5B4D22B2D9416730BD3F68AFB556714A43DEAE9463F06B9C8FDE00D6F3598BF59BEDF87A8CF616E42C084F6A91A51DA89A88F23D926A1618E7A18FAE4CA3D2B4B76AF966E89DA1C57BC33D9B2A66AEE40A1689DD0B129D8176EEBD107AE4F4F8431A0F7E3F75AF1245463C7CB144BB9B72A769F0E389C622CC0D6E8AAB0CFDA83212AFAB5BB0C719F35B1F4A79E64464E2B47325520A30BB524BB6EF3DBDE9C00873338E922B1B0826E30D9C59424\"}";
// 使用指定的UTF-8编码来创建String对象
// footer = new String(text.getBytes(), Charset.forName("GBK"));
ResponseFooter responseFooter = JSON.parseObject(footer, ResponseFooter.class);
Assertions.assertFalse(StringUtils.isBlank(responseFooter.getSignedMsg()));
}
```

```json
{
"signedMsg" : "A81EF804DEE3BAAC38CAA06C35C0D6A7B9E37373598BA8C09B4C449469CB3301F0DA488D3F0190A2121C467B244405CCB234A75895B8015221EF51BCCFCBAA94262EFCAD947E58D2943D18E84C03A91A83EF375921CBCFD436C1C47AD3ACDADA34666D7B344E837903A093DB5B36A3A99D805D26CDAAF17EBBAD4A3415ECDC209B8AEE565B6742097B13512829AA436999326149B462EE7B122F49FCB721AC1FFE84462BCC85AD9A3E0D438F45A1E9A44A1EDA3FEF5ED6EB9B34F4DD1EA7C8A8D3F53DF5195FDAA1C138B6A5AB9051B823403C9CD00B2D687B80B265A5B62AB811C2E2255335317B777B405532C52C3AFC338F61AE30C4E368192FCE05EB4FE2"
}
```

```java
package com.tilchinalink.tilchinalink_backend.pay.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;

/**
* @author mjl
* @version v1.0
* @Package : com.jctopinfo.fleet.pay.model
* @Description : 响应尾类
* @Create on : 2024-03-06 18:57
*/
@Data
public class ResponseFooter implements Serializable {
private static final long serialVersionUID = 1L;

/**
* 签名信息. 必填项. 长度为2048. Y S 2048
*/
private String signedMsg = "";

/**
* 原响应尾信息
*/
@JsonIgnore
private String originalData;

/**
* 是否验签成功
*/
Boolean signSuccess = false;

/**
* 未使用
* @param json
* @return
*/
public ResponseFooter fromJson(String json){
return null;
}

/**
* 未使用
* @param xml
* @return
*/
public ResponseFooter fromXml(String xml){

return null;
}
}

```

### 期待的正确结果
*能正常解析*

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

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

Contributor guide

Open the contributing guide

Research direction

Start with the supplied gbkParseObjectTest example and the ResponseFooter model, then inspect how JSON.parseObject maps the signedMsg field. Compare the failing application behavior with the standalone test and the cases using @JSONField(name = "signedMsg") or JSONReader.Feature.SupportSmartMatch. Done means reproducing the missing field and adding a regression test that verifies signedMsg is populated.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.