Jackson Annotations Tutorial
Selected Reading
- Jackson - Discussion
- Jackson - Useful Resources
- Jackson - Quick Guide
- Disable Annotation
- MixIn Annotations
- Custom Annotation
- Jackson - @JsonFilter
- Jackson - @JsonIdentityInfo
- @JsonBackReference
- @JsonManagedReference
- Jackson - @JsonView
- Jackson - @JsonUnwrapped
- Jackson - @JsonFormat
- Jackson - @JsonProperty
- Jackson - @JsonTypeName
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeInfo
- Jackson - @JsonAutoDetect
- Jackson - @JsonInclude
- Jackson - @JsonIgnoreType
- Jackson - @JsonIgnore
- @JsonIgnoreProperties
- @JsonEnumDefaultValue
- Jackson - @JsonDeserialize
- Jackson - @JsonSetter
- Jackson - @JsonAnySetter
- Jackson - @JacksonInject
- Jackson - @JsonCreator
- Jackson - @JsonSerialize
- Jackson - @JsonRootName
- Jackson - @JsonValue
- Jackson - @JsonRawValue
- @JsonPropertyOrder
- Jackson - @JsonGetter
- Jackson - @JsonAnyGetter
- Jackson - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Jackson - @JsonTypeName
Jackson Annotations - @JsonTypeName
@JsonTypeName is used to set type names to be used for annotated class.
Example - @JsonTypeName
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; pubpc class JacksonTester { pubpc 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 { pubpc String name; Shape(String name){ this.name = name; } } @JsonTypeName("square") static class Square extends Shape { pubpc double length; Square(){ this(null,0.0); } Square(String name, double length){ super(name); this.length = length; } } @JsonTypeName("circle") static class Circle extends Shape { pubpc double radius; Circle(){ this(null,0.0); } Circle(String name, double radius){ super(name); this.radius = radius; } } }
Output
{ "type" : "circle", "name" : "CustomCircle", "radius" : 1.0 } CustomCircleAdvertisements