alibaba / alibaba/fastjson2

[QUESTION] fastjson1.x DefaultJSONParser.getContext().object 如何迁移为FastJson2等价代码?

Open
#3,648 3 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

### 请描述您的问题
请问如何将fastjson1.x的应用里的自定义序列化的代码替换为等价的fastjson2代码?
FastJson1: com.alibaba.fastjson.parser.DefaultJSONParser.getContext().object
Fastjson2: JSONReader的context里如何拿当前上下文里的Bean Instance?
1. DTO:
``` java
@Data
public class UserDto{
@JSONField(deserializeUsing = AgeParamConverter.class)
private Integer age;
@JSONField(deserializeUsing = AgeCodeParamConverter.class)
private AgeCodeEnum ageCodeEnum;
}
```

2. 自定义反序列化:
``` java
public class AgeParamConverter extends IntegerCodec {
@Override
public Integer deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
UserDto user = (UserDto) parser.getContext().object;
Integer age = super.deserialze(parser, type, fieldName);
if (user == null){
return age;
}

if (user.getAgeCodeEnum() == null){
user.setAgeCodeEnum(getAgeCodeEnum(age)
}
return age;
}
```

Contributor guide

Open the contributing guide

Research direction

Start by comparing the fastjson1.x DefaultJSONParser.getContext().object entry point with the fastjson2 JSONReader context APIs, then inspect existing custom deserialization examples in the repository. Done means documenting or demonstrating a confirmed fastjson2 equivalent for retrieving the current UserDto instance during AgeParamConverter deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.