如何让Jackson JsonTypeInfo使用相对路径

时间:2018-01-18 15:54:59

标签: java json jackson

我想使用JsonTypeInfo进行多态类型处理,但JsonTypeInfo.Id.MINIMAL_CLASS的行为方式与我认为的不同。这是设置。

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.3</version>
    </dependency>

基类:

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; }
}

儿童班:

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; }
}

调用:

    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());
    }

生成:

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

如果我改为JsonTypeInfo.Id.CLASS,那么我得到:

Base   {"foo.jackson.Example":{"example":"_example"}}
Child  {"foo.jackson.stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car

我对Jackson文档的解释是MINIMAL_CLASS应该使用相对路径,我应该得到:

Base   {".Example":{"example":"_example"}}
Child  {".stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car
  1. 我错过了一步吗?
  2. 我是否误解了Per-class annotations文档?
  3. 这是杰克逊问题,我应该只使用JsonTypeInfo.Id.CLASS?

1 个答案:

答案 0 :(得分:0)

看起来我错过了一步。如果我在序列化时使用 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());

产生

Base   {".Example":{"example":"_example"}}
Child  {".stuff.Car":{"example":"_example","make":"Duesenberg"}}
Got foo.jackson.stuff.Car
相关问题