alibaba / alibaba/fastjson2

fastjson2反序列化时有的名称会失败

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

Description


com.alibaba.fastjson2
fastjson2
2.0.23

```
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.reader.ObjectReader;
import java.lang.reflect.Type;

public class OssUrlReader implements ObjectReader {

@Override
public T readObject(JSONReader jsonReader, Type type, Object o, long l) {
System.out.println("执行到这里了");
return (T) jsonReader.read(JSONArray.class);
}
}

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor

public class Goods {
@JSONField(deserializeUsing = OssUrlReader.class)
private JSONArray books;
}

import com.alibaba.fastjson2.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController(value = "test")
@RequestMapping(value = "test")

public class TestController {

@GetMapping(value = "goods")
public Goods goods() throws IOException {
var string = """
{"books":[2]}
""";
var goods = JSONObject.parseObject(string, Goods.class);
return goods;
}

}

问题和重现步骤:反序列化失败
1、修改字符串中的key和goods对象中的成员属性名称为一致时再测试
2、key和goods对象中的成员属性值为books时反序列化失败
3、修改key和goods对象中的成员属性值为image或images时反序列化成功

```

[java17.zip](https://github.com/alibaba/fastjson2/files/10765207/java17.zip)

问题描述

Contributor guide

Open the contributing guide

Research direction

Start with the Java 17 reproducer in the issue and run the JSONObject.parseObject call for Goods with the books key, then compare it with image and images. Trace the deserialization entry point, the JSONField deserializeUsing annotation, and OssUrlReader to identify why books differs; done means the books example deserializes successfully without breaking the other names.

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.