alibaba / alibaba/fastjson2

[BUG]fastjson1对byte数组转成成字符串后fastjson2无法解析

Open
#2,066 4 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

### 问题描述
*简要描述您碰到的问题。*
fastjson1对byte数组是按照base64转换的,fastjson2解析时无法解析

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

- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.0.42]

### 重现步骤
```java
public class TestFastJsonByte {

public static void main(String[] args) {
Test test = new Test();
test.bytes = new byte[]{1, 2, 3};

String fastJson1Str = com.alibaba.fastjson.JSON.toJSONString(test);
System.out.println("使用fastjson1转换byte数组为字符串结果:" + fastJson1Str);
try {
Test fastJson1Parse = com.alibaba.fastjson.JSON.parseObject(fastJson1Str, Test.class);
System.out.println("使用fastjson1解析fastjson1转换后的字符串:" + fastJson1Parse);
} catch (Exception e) {
System.err.println("使用fastjson1解析fastjson1转换后的字符串:" + e.getMessage());
}
try {
Test fastJson2Parse = com.alibaba.fastjson2.JSON.parseObject(fastJson1Str, Test.class);
System.out.println("使用fastjson2解析fastjson1转换后的字符串:" + fastJson2Parse);
} catch (Exception e) {
System.err.println("使用fastjson2解析fastjson1转换后的字符串:" + e.getMessage());
}

String fastJson2Str = com.alibaba.fastjson2.JSON.toJSONString(test);
System.out.println("使用fastjson2转换byte数组为字符串结果:" + fastJson2Str);
try {
Test fastJson2Parse = com.alibaba.fastjson2.JSON.parseObject(fastJson2Str, Test.class);
System.out.println("使用fastjson2解析fastjson2转换后的字符串:" + fastJson2Parse);
} catch (Exception e) {
System.err.println("使用fastjson2解析fastjson2转换后的字符串:" + e.getMessage());
}
try {
Test fastJson1Parse = com.alibaba.fastjson.JSON.parseObject(fastJson2Str, Test.class);
System.out.println("使用fastjson1解析fastjson2转换后的字符串:" + fastJson1Parse);
} catch (Exception e) {
System.err.println("使用fastjson1解析fastjson2转换后的字符串:" + e.getMessage());
}
}

@Data
private static class Test {
private byte[] bytes;
}

}
```

### 期待的正确结果
fastjson1对byte数组是按照base64转换的,fastjson2解析时可以判断是byte[]类型时,如果是字符串即使用base64的方式解析,如果是数组则还是按原来byte数组解析

### 相关日志输出
![image](https://github.com/alibaba/fastjson2/assets/137134935/46c9df28-d07d-43bd-b019-5ba9cb2f3441)

Contributor guide

Open the contributing guide

Research direction

Start with the provided TestFastJsonByte reproducer and trace fastjson2 deserialization for byte[] values. Verify the behavior against both the base64 string emitted by fastjson1 and the numeric array form; done means both forms parse correctly, with a regression test covering them.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.