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 - @JsonCreator
Jackson Annotations - @JsonCreator
@JsonCreator is used to fine tune the constructor or factory method used in deseriapzation. We ll be using @JsonProperty as well to achieve the same. In the example below, we are matching an json with different format to our class by defining the required property names.
Example @JsonCreator
import java.io.IOException; import java.text.ParseException; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; pubpc class JacksonTester { pubpc static void main(String args[]) throws ParseException{ String json = "{"id":1,"theName":"Mark"}"; ObjectMapper mapper = new ObjectMapper(); try { Student student = mapper .readerFor(Student.class) .readValue(json); System.out.println(student.rollNo +", " + student.name); } catch (IOException e) { e.printStackTrace(); } } } class Student { pubpc String name; pubpc int rollNo; @JsonCreator pubpc Student(@JsonProperty("theName") String name, @JsonProperty("id") int rollNo){ this.name = name; this.rollNo = rollNo; } }
Output
1, MarkAdvertisements