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 - Mixin Annotation
Overview
Mixin Annotation is a way to associate annotations without modifying the target class.
Create a class to be used as Mixin
Create a class where required annotation is applied.
@JsonIgnoreType
class MixInForIgnoreType {}
Create a class where annotation is to be applied indirectly.
class Name {
public String name;
Name(String name){
this.name = name;
}
}
Apply the Mixin
ObjectMapper mapper = new ObjectMapper(); mapper.addMixIn(Name.class, MixInForIgnoreType.class);
Example - Serialization without Mixin Annotation
JacksonTester.java
package com.tutorialspoint;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]) {
try {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student(1,11,"1ab","Mark");
String jsonString = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(student);
System.out.println(jsonString);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
public int id;
public String systemId;
public int rollNo;
public Name nameObj;
Student(int id, int rollNo, String systemId, String name) {
this.id = id;
this.systemId = systemId;
this.rollNo = rollNo;
nameObj = new Name(name);
}
}
class Name {
public String name;
Name(String name){
this.name = name;
}
}
Output
Run the JacksonTester and verify the output −
{
"id" : 1,
"systemId" : "1ab",
"rollNo" : 11,
"nameObj" : {
"name" : "Mark"
}
}
Example - Serialization with Mixin Annotation
JacksonTester.java
package com.tutorialspoint;
import java.io.IOException;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JacksonTester {
public static void main(String args[]) {
try {
Student student = new Student(1,11,"1ab","Mark");
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(Name.class, MixInForIgnoreType.class);
String jsonString = mapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(student);
System.out.println(jsonString);
}
catch (IOException e) {
e.printStackTrace();
}
}
}
class Student {
public int id;
public String systemId;
public int rollNo;
public Name nameObj;
Student(int id, int rollNo, String systemId, String name) {
this.id = id;
this.systemId = systemId;
this.rollNo = rollNo;
nameObj = new Name(name);
}
}
class Name {
public String name;
Name(String name){
this.name = name;
}
}
@JsonIgnoreType
class MixInForIgnoreType {}
Output
Run the JacksonTester and verify the output −
{
"id" : 1,
"systemId" : "1ab",
"rollNo" : 11
}
Here we can see, using addMixin() method, we're able to apply annotations without modify the original class.
Advertisements