-
Notifications
You must be signed in to change notification settings - Fork 546
Closed
Labels
Milestone
Description
问题描述
When using @JSONType(typeKey = ..., seeAlso = {...}), if typeKey is not the first key in the json, the value is deserialized properly; however if typeKey is the first key, its value is ignored and null is stored instead.
From some debugging it seems like this line causes the issue - if I manually run jsonReader.reset(savePoint) here when i == 0 and continue the debugger, the test passes successfully. But there could be other reasons for this line that I'm not aware of.
环境信息
请填写以下信息:
- JDK信息: 21.0.2
- 版本信息:Fastjson2 2.0.57
重现步骤
import static org.junit.jupiter.api.Assertions.assertEquals;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONType;
import org.junit.jupiter.api.Test;
class TestAutoType {
@JSONType(typeKey = "type", seeAlso = {Cat.class})
interface Animal {}
@JSONType(typeName = "cat")
record Cat(String type, int age) implements Animal {}
@Test
void cat() {
String s1 = "{\"age\": 20, \"type\": \"cat\"}"; // type comes second
Cat c1 = (Cat) JSON.parseObject(s1, Animal.class);
assertEquals("cat", c1.type); // succeeds
String s2 = "{\"type\": \"cat\", \"age\": 20}"; // type comes first
Cat c2 = (Cat) JSON.parseObject(s2, Animal.class);
assertEquals("cat", c2.type); // fails
}
}期待的正确结果
Test should pass
附加信息
Related - ideally I can do something like this:
@JSONType(typeKey = "type")
sealed interface Animal permits Cat {}instead of
@JSONType(typeKey = "type", seeAlso = {Cat.class})
interface Animal {}i.e. no need to specify seeAlso for a sealed interface as theoretically we should know all possible subclasses already. This didn't seem to work so I had to add in the seeAlso.