Jackson – @JsonProperty

Jackson Annotations – @JsonProperty ”; Previous Next @JsonProperty is used to mark non-standard getter/setter method to be used with respect to json property. Example – @JsonProperty import java.io.IOException; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]) throws IOException { ObjectMapper mapper = new ObjectMapper(); String json = “{“id” : 1}”; Student student = mapper.readerFor(Student.class).readValue(json); System.out.println(student.getTheId()); } } class Student { private int id; Student(){} Student(int id){ this.id = id; } @JsonProperty(“id”) public int getTheId() { return id; } @JsonProperty(“id”) public void setTheId(int id) { this.id = id; } } Output 1 Print Page Previous Next Advertisements ”;

Custom Annotation

Jackson Annotations – Custom Annotation ”; Previous Next We can create custom annotation easily using @JacksonAnnotationsInside annotation. Example – Custom Annotation import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.text.ParseException; import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonPropertyOrder; 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”); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } } @CustomAnnotation class Student { public int id; public int rollNo; public String name; public String otherDetails; Student(int id, int rollNo, String name){ this.id = id; this.rollNo = rollNo; this.name = name; } } @Retention(RetentionPolicy.RUNTIME) @JacksonAnnotationsInside @JsonInclude(value = Include.NON_NULL) @JsonPropertyOrder({ “rollNo”, “id”, “name” }) @interface CustomAnnotation {} Output { “rollNo” : 13, “id” : 1, “name” : “Mark” } Print Page Previous Next Advertisements ”;

Jackson – @JsonIgnore

Jackson Annotations – @JsonIgnore ”; Previous Next @JsonIgnore is used at field level to mark a property or list of properties to be ignored. Example – @JsonIgnore import java.io.IOException; import com.fasterxml.jackson.annotation.JsonIgnore; 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); } catch (IOException e) { e.printStackTrace(); } } } class Student { public int id; @JsonIgnore public String systemId; public int rollNo; public String name; Student(int id, int rollNo, String systemId, String name){ this.id = id; this.systemId = systemId; this.rollNo = rollNo; this.name = name; } } Output { “id” : 1, “rollNo” : 11, “name” : “Mark” } Print Page Previous Next Advertisements ”;

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 – @JsonRootName

Jackson Annotations – @JsonRootName ”; Previous Next @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; import com.fasterxml.jackson.annotation.JsonRootName; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); try { Student student = new Student(“Mark”, 1); mapper.enable(SerializationFeature.WRAP_ROOT_VALUE); String jsonString = mapper .writerWithDefaultPrettyPrinter() .writeValueAsString(student); System.out.println(jsonString); } catch (IOException e) { e.printStackTrace(); } } } @JsonRootName(value = “student”) 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 { “student” : { “name” : “Mark”, “rollNo” : 1 } } Print Page Previous Next Advertisements ”;

Jackson – @JsonIgnoreType

Jackson Annotations – @JsonIgnoreType ”; Previous Next @JsonIgnoreType is used at mark a property of special type to be ignored. Example – @JsonIgnoreType 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[]){ ObjectMapper mapper = new ObjectMapper(); try { 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; @JsonIgnore 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 } Print Page Previous Next Advertisements ”;

Jackson – @JsonAnyGetter

Jackson Annotations – @JsonAnyGetter ”; Previous Next @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” } Print Page Previous Next Advertisements ”;

Jackson – Home

Jackson Annotations Tutorial PDF Version Quick Guide Resources Job Search Discussion 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. Audience This reference has been prepared for the Java developers from beginner to expert level. After completing this tutorial you will find yourself at a moderate level of expertise in knowledge of Jackson library from where you can take yourself to next levels Prerequisites Knowledge of JAVA programming language. Print Page Previous Next Advertisements ”;

Jackson – @JsonAnySetter

Jackson Annotations – @JsonAnySetter ”; Previous Next @JsonAnySetter allows a setter method to use Map which is then used to deserialize the additional properties of JSON in the similar fashion as other properties. Example @JsonAnySetter import java.io.IOException; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonAnySetter; import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonTester { public static void main(String args[]){ ObjectMapper mapper = new ObjectMapper(); String jsonString = “{“RollNo” : “1”,”Name” : “Mark”}”; try { Student student = mapper.readerFor(Student.class).readValue(jsonString); System.out.println(student.getProperties().get(“Name”)); System.out.println(student.getProperties().get(“RollNo”)); } catch (IOException e) { e.printStackTrace(); } } } class Student { private Map<String, String> properties; public Student(){ properties = new HashMap<>(); } public Map<String, String> getProperties(){ return properties; } @JsonAnySetter public void add(String property, String value){ properties.put(property, value); } } Output Mark 1 Print Page Previous Next Advertisements ”;

Jackson – @JsonRawValue

Jackson Annotations – @JsonRawValue ”; Previous Next @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} } Print Page Previous Next Advertisements ”;