Jackson - Serialization Annotations
- Jackson - @JsonAnyGetter
- Jackson - @JsonGetter
- Jackson - @JsonPropertyOrder
- Jackson - @JsonRawValue
- Jackson - @JsonValue
- Jackson - @JsonRootName
- Jackson - @JsonSerialize
Jackson - Deserialization Annotations
- Jackson - @JsonCreator
- Jackson - @JacksonInject
- Jackson - @JsonAnySetter
- Jackson - @JsonSetter
- Jackson - @JsonDeserialize
- Jackson - @JsonEnumDefaultValue
Jackson - Property Inclusion Annotations
- Jackson - @JsonIgnoreProperties
- Jackson - @JsonIgnore
- Jackson - @JsonIgnoreType
- Jackson - @JsonInclude
- Jackson - @JsonAutoDetect
Jackson - Type Handling Annotations
Jackson - General Annotations
- Jackson - @JsonProperty
- Jackson - @JsonFormat
- Jackson - @JsonUnwrapped
- Jackson - @JsonView
- Jackson - @JsonManagedReference
- Jackson - @JsonBackReference
- Jackson - @JsonIdentityInfo
- Jackson - @JsonFilter
Jackson - Miscellaneous
Jackson - Resources
Jackson Annotations - @JacksonInject
Overview
@JacksonInject annotation is used when a property value is to be injected instead of being parsed from Json input. In the example below, we are to insert a value into object instead of parsing from the Json.
Example - Deserialization without using @JacksonInject
JacksonTester.java
package com.tutorialspoint;
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]) throws ParseException{
String json = "{\"name\":\"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;
}
Output
Run the JacksonTester and verify the output −
0, Mark
Example - Deserialization with @JacksonInject
JacksonTester.java
package com.tutorialspoint;
import java.io.IOException;
import java.text.ParseException;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]) throws ParseException{
String json = "{\"name\":\"Mark\"}";
InjectableValues injectableValues = new InjectableValues.Std()
.addValue(int.class, 1);
ObjectMapper mapper = new ObjectMapper();
try {
Student student = mapper
.reader(injectableValues)
.forType(Student.class)
.readValue(json);
System.out.println(student.rollNo +", " + student.name);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
public String name;
@JacksonInject
public int rollNo;
}
Output
Run the JacksonTester and verify the output −
1, Mark
Here we can see, using @JacksonInject, we can a inject value in our object during deserialization easily.
Advertisements