简体   繁体   English

如何获得Jackson JsonTypeInfo以使用相对路径

[英]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
  1. Am I missing a step? 我错过了一步吗?
  2. Did I misinterpret the Per-class annotations documentation? 我是否误解了每班注释文档?
  3. Is this a Jackson issue and I should just use JsonTypeInfo.Id.CLASS? 这是一个Jackson问题,我应该只使用JsonTypeInfo.Id.CLASS?

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.

相关问题 如何使用@JsonTypeInfo和@JsonSubTypes来实例化具有不同配置的类? - How can I use @JsonTypeInfo and @JsonSubTypes to instantiate Classes with different configurations? 使用 Jackson 的 @JsonTypeInfo 反序列化时如何保留 type 属性? - How can I retain the type property when deserializing with Jackson's @JsonTypeInfo? 如何从 jar 下的相对路径获取 URL? - How can I get a URL from relative path under a jar? 如何在我的android项目中获取文件夹的相对路径? - How can I get relative path of the folders in my android project? 如何获取 jackson JsonNode 的路径 - How do I get the path of a jackson JsonNode 使用 @JsonTypeInfo 时的 Jackson 序列化/反序列化问题。 有人可以向我解释这是如何工作的吗? - Jackson serialization/deserialization problems when using @JsonTypeInfo. Can someone explain to me how this is working? 杰克逊:阵列中缺少@JsonTypeInfo - Jackson: @JsonTypeInfo missing in Arrays 杰克逊JsonView和JSonTypeInfo - Jackson JsonView and JSonTypeInfo Jackson - JsonTypeInfo,可以处理骆驼和蛇案例的属性 - Jackson - JsonTypeInfo, property that can handles both camel and snake case Jackson @JsonValue 与 @JsonTypeInfo 冲突; 如何让他们一起工作 - Jackson @JsonValue is conflicting with @JsonTypeInfo; How to make them work together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM