[英]How can I get Jackson JsonTypeInfo to use relative path
I want to use JsonTypeInfo to for Polymorphic Type Handling, but JsonTypeInfo.Id.MINIMAL_CLASS doesn't behave the way that I think it should. 我想使用JsonTypeInfo进行多态类型处理,但是JsonTypeInfo.Id.MINIMAL_CLASS的行为与我认为的方式不符。 Here is the setup.
这是设置。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
Base Class: 基类:
package foo.jackson;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(
use=JsonTypeInfo.Id.MINIMAL_CLASS,
include=JsonTypeInfo.As.WRAPPER_OBJECT )
public class Example {
protected String example = "_example";
public String getExample() { return example; }
public void setExample(String example) { this.example = example; }
}
Child Class: 儿童班:
package foo.jackson.stuff;
public class Car extends foo.jackson.Example {
protected String make = "Duesenberg";
public String getMake() { return make; }
public void setMake(String make) { this.make = make; }
}
Invocation: 调用:
foo.jackson.Example ex = new foo.jackson.Example();
foo.jackson.Example car = new foo.jackson.stuff.Car();
try {
ObjectMapper json = new ObjectMapper();
log.info("Base {}",json.writeValueAsString(ex));
String stCar = json.writeValueAsString(car);
log.info("Child {}",stCar);
foo.jackson.Example parse = json.readValue(stCar, foo.jackson.Example.class);
log.info( "Got {}", parse.getClass().getCanonicalName() );
}
catch (JsonProcessingException jpe) {
log.error("Whoops {}", jpe.toString());
}
Which generates: 会产生:
Base {".Example":{"example":"_example"}}
Child {".Car":{"example":"_example","make":"Duesenberg"}}
Whoops com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id 'foo.jackson.Car' as a subtype of [simple type, class foo.jackson.Example]: no such class found
If I change to JsonTypeInfo.Id.CLASS, then I get: 如果更改为JsonTypeInfo.Id.CLASS,则会得到:
Base {"foo.jackson.Example":{"example":"_example"}}
Child {"foo.jackson.stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car
My interpretation of the Jackson Documentation is that MINIMAL_CLASS is supposed to use the relative path and I should get: 我对Jackson文档的解释是MINIMAL_CLASS应该使用相对路径,我应该得到:
Base {".Example":{"example":"_example"}}
Child {".stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car
Looks like I was missing a step. 好像我错过了一步。 If I use writerWithType(foo.jackson.Example.class) when serializing, then I get my expected behavior:
如果我在序列化时使用writerWithType(foo.jackson.Example.class) ,那么我会得到预期的行为:
foo.jackson.Example ex = new foo.jackson.Example();
foo.jackson.Example car = new foo.jackson.stuff.Car();
try {
ObjectMapper json = new ObjectMapper();
log.info("Base {}",json.writeValueAsString(ex));
String stCar = json
.writerWithType(foo.jackson.Example.class)
.writeValueAsString(car);
log.info("Child {}",stCar);
foo.jackson.Example parse = json.readValue(stCar, foo.jackson.Example.class);
log.info( "Got {}", parse.getClass().getCanonicalName() );
}
catch (JsonProcessingException jpe) {
log.error("Whoops {}", jpe.toString());
generates: 产生:
Base {".Example":{"example":"_example"}}
Child {".stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.