FastJson 与 putDeserializer 导致 StackOverflowError
FastJson with putDeserializer leads to StackOverflowError
使用 FastJson 和 putDeserializer 反序列化对象会导致 WhosebugError。我做错了什么?
test.json
{
"openapi": "3.0.1",
"info": {
"title": "Swagger Petstore",
"version": "1.0.0",
"description": "This is a sample server Petstore server"
}
}
Spec.java
@Data
public class Spec {
private String openapi;
private Info info;
}
Info.java
@Data
public class Info {
private String title;
private String version;
private String description;
}
InfoDeserializer.java
public class InfoDeserializer implements ObjectDeserializer {
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
Info info = parser.parseObject(Info.class);
return (T) info;
}
@Override
public int getFastMatchToken() {
return 0;
}
}
FastJsonTest.java
public class FastJsonTest {
@Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
ParserConfig config = new ParserConfig();
config.putDeserializer(Info.class, new InfoDeserializer());
// This line leads to Whosebug
Spec spec = JSON.parseObject(json, Spec.class, config);
// Assertion
assertNotNull(spec);
}
}
但是,如果我将反序列化器从 ParserConfig
移到 JSONField
注释中,它会起作用,但是使用这种方法我无法将自定义参数传递到反序列化器中。
Spec.java
@Data
public class Spec {
private String openapi;
@JSONField(deserializeUsing = InfoDeserializer.class)
private Info info;
}
FastJsonTest.java
public class FastJsonTest {
@Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
Spec spec = JSON.parseObject(json, Spec.class);
// Assertion
assertNotNull(spec);
}
}
调试后发现应该这样用
public class InfoDeserializer implements ObjectDeserializer {
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
// Parse to JSONObject first, then parse to your object
Info info = JSON.parseObject(parser.parseObject().toJSONString(), type);
return (T) info;
}
@Override
public int getFastMatchToken() {
return 0;
}
}
使用 FastJson 和 putDeserializer 反序列化对象会导致 WhosebugError。我做错了什么?
test.json
{
"openapi": "3.0.1",
"info": {
"title": "Swagger Petstore",
"version": "1.0.0",
"description": "This is a sample server Petstore server"
}
}
Spec.java
@Data
public class Spec {
private String openapi;
private Info info;
}
Info.java
@Data
public class Info {
private String title;
private String version;
private String description;
}
InfoDeserializer.java
public class InfoDeserializer implements ObjectDeserializer {
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
Info info = parser.parseObject(Info.class);
return (T) info;
}
@Override
public int getFastMatchToken() {
return 0;
}
}
FastJsonTest.java
public class FastJsonTest {
@Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
ParserConfig config = new ParserConfig();
config.putDeserializer(Info.class, new InfoDeserializer());
// This line leads to Whosebug
Spec spec = JSON.parseObject(json, Spec.class, config);
// Assertion
assertNotNull(spec);
}
}
但是,如果我将反序列化器从 ParserConfig
移到 JSONField
注释中,它会起作用,但是使用这种方法我无法将自定义参数传递到反序列化器中。
Spec.java
@Data
public class Spec {
private String openapi;
@JSONField(deserializeUsing = InfoDeserializer.class)
private Info info;
}
FastJsonTest.java
public class FastJsonTest {
@Test
public void test() throws IOException {
// Read json file
File file = new ClassPathResource("test.json").getFile();
String json = new String(Files.readAllBytes(file.toPath()));
// Parse json
Spec spec = JSON.parseObject(json, Spec.class);
// Assertion
assertNotNull(spec);
}
}
调试后发现应该这样用
public class InfoDeserializer implements ObjectDeserializer {
@Override
public <T> T deserialze(DefaultJSONParser parser, Type type, Object o) {
// Parse to JSONObject first, then parse to your object
Info info = JSON.parseObject(parser.parseObject().toJSONString(), type);
return (T) info;
}
@Override
public int getFastMatchToken() {
return 0;
}
}