- Jackson 标注教程
- Jackson - 主页
- 序列化标注
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
- 反序列化标注
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- @JsonEnumDefaultValue
- 属性包含标注
- @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
- 类型处理标注
- Jackson - @JsonTypeInfo
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeName
- 常规标注
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- @JsonManagedReference
- @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
- 其他
- 自定义标注
- MixIn 标注
- 禁用标注
- Jackson 标注资源
- Jackson - 快速指南
- Jackson - 有用资源
- Jackson - 讨论
Jackson 标注 - @JsonTypeInfo
@JsonTypeInfo 用于指示在序列化和反序列化中包含的类型信息的详细信息。
示例 - @JsonTypeInfo
import java.io.IOException; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException { Shape shape = new JacksonTester.Circle("CustomCircle", 1); String result = new ObjectMapper() .writerWithDefaultPrettyPrinter() .writeValueAsString(shape); System.out.println(result); String json = "{\"name\":\"CustomCircle\",\"radius\":1.0, \"type\":\"circle\"}"; Circle circle = new ObjectMapper().readerFor(Shape.class).readValue(json); System.out.println(circle.name); } @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = As.PROPERTY, property = "type") @JsonSubTypes({ @JsonSubTypes.Type(value = Square.class, name = "square"), @JsonSubTypes.Type(value = Circle.class, name = "circle") }) static class Shape { public String name; Shape(String name){ this.name = name; } } @JsonTypeName("square") static class Square extends Shape { public double length; Square(){ this(null,0.0); } Square(String name, double length){ super(name); this.length = length; } } @JsonTypeName("circle") static class Circle extends Shape { public double radius; Circle(){ this(null,0.0); } Circle(String name, double radius) { super(name); this.radius = radius; } } }
输出
{ "type" : "circle", "name" : "CustomCircle", "radius" : 1.0 } CustomCircle
广告