- Jackson Annotations Tutorial
- Jackson - Home
- Serialization Annotations
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
- Deserialization Annotations
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- @JsonEnumDefaultValue
- Property Inclusion Annotations
- @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
- Type Handling Annotations
- Jackson - @JsonTypeInfo
- Jackson - @JsonSubTypes
- Jackson - @JsonTypeName
- General Annotations
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- @JsonManagedReference
- @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
- Miscellaneous
- Custom Annotation
- MixIn Annotations
- Disable Annotation
- Jackson Annotations Resources
- Jackson - Quick Guide
- Jackson - Useful Resources
- Jackson - Discussion
Jackson Annotations - @JsonCreator
@JsonCreator is used to fine tune the constructor or factory method used in deserialization. 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; public class JacksonTester { public 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 { public String name; public int rollNo; @JsonCreator public Student(@JsonProperty("theName") String name, @JsonProperty("id") int rollNo){ this.name = name; this.rollNo = rollNo; } }
Output
1, Mark
Advertisements