Jackson Annotations – Useful Resources ”; Previous Next The following resources contain additional information on Jackson JSON Processor library. Please use them to get more in-depth knowledge on this topic. Useful Links on Jackson Jackson Project Home – Jackson JSON Processor Portal Page on GitHub. JSON – Official Website of JSON. Useful Books on Jackson To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;
Category: jackson Annotations
Jackson – Discussion
Discuss Jackson Annotations ”; Previous Next Jackson is a very popular and efficient java based library to serialize or map java objects to JSON and vice versa. This tutorial will teach you basic and advanced Jackson library Annotations features and their usage in a simple and intuitive way. Print Page Previous Next Advertisements ”;
Jackson – @JsonSubTypes
Jackson Annotations – @JsonSubTypes ”; Previous Next @JsonSubTypes is used to indicate subtypes of types annotated. Example – @JsonSubTypes 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; public class JacksonTester { public 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 { public String name; Shape(String name) { this.name = name; } } @JsonTypeName(“square”) static class Square extends Shape { public double length; Square(){ this(null,0.0); } Square(String name, double length){ super(name); this.length = length; } } @JsonTypeName(“circle”) static class Circle extends Shape { public 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 } CustomCircle Print Page Previous Next Advertisements ”;
Jackson – @JsonIdentityInfo
Jackson Annotations – @JsonIdentityInfo ”; Previous Next @JsonIdentityInfo is used when objects have parent child relationship. @JsonIdentityInfo is used to indicate that object identity will be used during serialization/de-serialization. Example – @JsonIdentityInfo import java.io.IOException; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException{ ObjectMapper mapper = new ObjectMapper(); Student student = new Student(1,13, “Mark”); Book book1 = new Book(1,”Learn HTML”, student); Book book2 = new Book(2,”Learn JAVA”, student); student.addBook(book1); student.addBook(book2); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(book1); System.out.println(jsonString); } } @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = “id”) class Student { public int id; public int rollNo; public String name; public List<Book> books; Student(int id, int rollNo, String name){ this.id = id; this.rollNo = rollNo; this.name = name; this.books = new ArrayList<Book>(); } public void addBook(Book book){ books.add(book); } } @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = “id”) class Book{ public int id; public String name; Book(int id, String name, Student owner){ this.id = id; this.name = name; this.owner = owner; } public Student owner; } Output { “id” : 1, “name” : “Learn HTML”, “owner” : { “id” : 1, “rollNo” : 13, “name” : “Mark”, “books” : [ 1, { “id” : 2, “name” : “Learn JAVA”, “owner” : 1 } ] } } Print Page Previous Next Advertisements ”;
Jackson – @JsonUnwrapped
Jackson Annotations – @JsonUnwrapped ”; Previous Next @JsonUnwrapped is used to unwrap values of objects during serialization or de-serialization. Example – @JsonUnwrapped import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException, ParseException{ ObjectMapper mapper = new ObjectMapper(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(“dd-MM-yyyy”); Date date = simpleDateFormat.parse(“20-12-1984”); Student.Name name = new Student.Name(); name.first = “Jane”; name.last = “Doe”; Student student = new Student(1, name); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } } class Student { public int id; @JsonUnwrapped public Name name; Student(int id, Name name){ this.id = id; this.name = name; } static class Name { public String first; public String last; } } Output { “id” : 1, “first” : “Jane”, “last” : “Doe” } Print Page Previous Next Advertisements ”;
@JsonPropertyOrder
Jackson Annotations – @JsonPropertyOrder ”; Previous Next @JsonPropertyOrder allows a specific order to be preserved while serializing a JSON object. Example without @JsonPropertyOrder import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; public Student(String name, int rollNo) { this.name = name; this.rollNo = rollNo; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “name” : “Mark”, “rollNo” : 1 } Example @JsonPropertyOrder import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonPropertyOrder; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } @JsonPropertyOrder({ “rollNo”, “name” }) class Student { private String name; private int rollNo; public Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “name” : “Mark”, “rollNo” : 1 } Print Page Previous Next Advertisements ”;
Disable Annotation
Jackson Annotations – Disable ”; Previous Next We can disable jackson annotations using disable() function of ObjectMapper. Example – Disabling Annotation import java.io.IOException; import com.fasterxml.jackson.annotation.JsonIgnoreType; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try{ Student student = new Student(1,11,”1ab”,”Mark”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); ObjectMapper mapper1 = new ObjectMapper(); mapper1.disable(MapperFeature.USE_ANNOTATIONS); jsonString = mapper1 .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); } } @JsonIgnoreType class Name { public String name; Name(String name){ this.name = name; } } Output { “id” : 1, “systemId” : “1ab”, “rollNo” : 11 } { “id” : 1, “systemId” : “1ab”, “rollNo” : 11, “nameObj” : { “name” : “Mark” } } Print Page Previous Next Advertisements ”;
Jackson – Quick Guide
Jackson Annotations – Quick Guide ”; Previous Next Jackson Annotations – @JsonAnyGetter @JsonAnyGetter allows a getter method to return Map which is then used to serialize the additional properties of JSON in the similar fashion as other properties. Example without @JsonAnyGetter import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try{ Student student = new Student(); student.add(“Name”, “Mark”); student.add(“RollNo”, “1”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private Map<String, String> properties; public Student(){ properties = new HashMap<>(); } public Map<String, String> getProperties(){ return properties; } public void add(String property, String value){ properties.put(property, value); } } Output { “properties” : { “RollNo” : “1”, “Name” : “Mark” } } Example with @JsonAnyGetter import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonAnyGetter; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try{ Student student = new Student(); student.add(“Name”, “Mark”); student.add(“RollNo”, “1”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private Map<String, String> properties; public Student(){ properties = new HashMap<>(); } @JsonAnyGetter public Map<String, String> getProperties(){ return properties; } public void add(String property, String value){ properties.put(property, value); } } Output { “RollNo” : “1”, “Name” : “Mark” } Jackson Annotations – @JsonGetter @JsonGetter allows a specific method to be marked as getter method. Example without @JsonGetter import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; public Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } public String getStudentName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “studentName” : “Mark”, “rollNo” : 1 } Example with @JsonGetter import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonGetter; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; public Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } @JsonGetter public String getStudentName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “name” : “Mark”, “rollNo” : 1 } Jackson Annotations – @JsonPropertyOrder @JsonPropertyOrder allows a specific order to be preserved while serializing a JSON object. Example without @JsonPropertyOrder import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; public Student(String name, int rollNo) { this.name = name; this.rollNo = rollNo; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “name” : “Mark”, “rollNo” : 1 } Example @JsonPropertyOrder import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonPropertyOrder; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } @JsonPropertyOrder({ “rollNo”, “name” }) class Student { private String name; private int rollNo; public Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } } Output { “name” : “Mark”, “rollNo” : 1 } Jackson Annotations – @JsonRawValue @JsonRawValue allows to serialize a text without escaping or without any decoration. Example without @JsonRawValue import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1, “{“attr”:false}”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; private String json; public Student(String name, int rollNo, String json){ this.name = name; this.rollNo = rollNo; this.json = json; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } public String getJson(){ return json; } } Output { “name” : “Mark”, “rollNo” : 1, “json” : {“attr”:false} } Example with @JsonRawValue import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.annotation.JsonRawValue; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1, “{“attr”:false}”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; @JsonRawValue private String json; public Student(String name, int rollNo, String json) { this.name = name; this.rollNo = rollNo; this.json = json; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } public String getJson(){ return json; } } Output { “name” : “Mark”, “rollNo” : 1, “json” : {“attr”:false} } Jackson Annotations – @JsonValue @JsonValue allows to serialize an entire object using its single method. Example @JsonValue import java.io.IOException; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } class Student { private String name; private int rollNo; public Student(String name, int rollNo){ this.name = name; this.rollNo = rollNo; } public String getName(){ return name; } public int getRollNo(){ return rollNo; } @JsonValue public String toString(){ return “{ name : ” + name + ” }”; } } Output “{ name : Mark }” Jackson Annotations – @JsonRootName @JsonRootName allows to have a root node specified over the JSON. We need to enable wrap root value as well. Example @JsonRootName import java.io.IOException;
Jackson – @JsonTypeName
Jackson Annotations – @JsonTypeName ”; Previous Next @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; public class JacksonTester { public 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 { public String name; Shape(String name){ this.name = name; } } @JsonTypeName(“square”) static class Square extends Shape { public double length; Square(){ this(null,0.0); } Square(String name, double length){ super(name); this.length = length; } } @JsonTypeName(“circle”) static class Circle extends Shape { public 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 } CustomCircle Print Page Previous Next Advertisements ”;
Jackson – @JsonTypeInfo
Jackson Annotations – @JsonTypeInfo ”; Previous Next @JsonTypeInfo is used to indicate details of type information which is to be included in serialization and de-serialization. Example – @JsonTypeInfo 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; public class JacksonTester { public 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 { public String name; Shape(String name){ this.name = name; } } @JsonTypeName(“square”) static class Square extends Shape { public double length; Square(){ this(null,0.0); } Square(String name, double length){ super(name); this.length = length; } } @JsonTypeName(“circle”) static class Circle extends Shape { public 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 } CustomCircle Print Page Previous Next Advertisements ”;