184.5.4. polymorphic deserialization 的默认映射

如果一个给定的 Java 类 com.example.MyClass 没有被列入白名单,则仍可对类的实例进行序列化,但在接收过程中,实例将使用通用的默认映射进行反序列化。

当 Jackson 中启用了 polymorphic 对象映射时,对对象的编码有几种替代方法:

  • With @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY):

    {"@class":"com.example.MyClass", "property1":"value1", "property2":"value2", ...}

    在本例中,实例将反序列化为具有 属性的对象

  • With @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.WRAPPER_ARRAY):

    ["com.example.MyClass", {"property1":"value1", "property2":"value2", ...}]

    在本例中,实例将反序列化为含有两个字段的 JSON 数组:

    • 带有com.example.MyClass的字符串
    • 具有 两个属性(或更多)属性的对象
  • 使用 @JsonTypeInfo (use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.WRAPPER_OBJECT):

    {"com.example.MyClass":{"property1":"value1", "property2":"value2", ...}}

    在本例中,实例将按单个字段 com.example.MyClass 序列化为 JSON 映射,其值为具有两个(或更多)属性的对象。