Serialization of Java objects using Jackson Library
This post will discuss the serialization and deserialization of Java objects using the Jackson library by converting Java objects into their JSON representation and then JSON to an equivalent Java object.
Nowadays, almost all RESTful web services consume and produce JSON data instead of XML. Unfortunately, Java SE doesn’t support converting JSON to Java Object (and vice-versa). But there are many third-party libraries available that are very reliable and offer high performance. One such library is Jackson.
Jackson is a multi-purpose, high-performance Java library for processing JSON. It provides Data Binding functionality that can be used to convert Java objects into their JSON representation. We can also use it to convert a JSON string to an equivalent Java object.
Jackson org.codehaus.jackson.map.ObjectMapper has writeValueAsString() and readValue() methods to convert Java objects to JSON string and vice-versa.
But before we use it, we want to list out a few drawbacks of using the Jackson library for Data Binding:
- It requires the class to have a default no-args constructor to instantiate Java Object from JSON string. If no-args constructor is not provided, it will throw
JsonMappingException. - Unlike Gson, Jackson will require getters for all private fields, otherwise serialization and deserialization won’t work.
- Jackson requires Java SE 1.5 or more, so we can’t use it on legacy projects.
The following program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Student { private String id; private Set<String> subjects; public Student(String id) { this.id = id; subjects = new HashSet<>(Arrays.asList("Maths", "Science")); } private Student() {} @Override public String toString() { return Arrays.asList(id, subjects.toString()).toString(); } public String getId() { return id; } public Set<String> getSubjects() { return subjects; } public void setSubjects(Set<String> sub) { this.subjects = sub; } public void setId(String id) { this.id = id; } } class Person { private String name; private int age; private Student student; public Person(String name, int age) { this.name = name; this.age = age; student = new Student(name.replaceAll("\\s+", "_") + "_" + String.valueOf(age)); } private Person() {} @Override public String toString() { return Arrays.asList(name, String.valueOf(age), student.toString()) .toString(); } public Student getStudent() { return student; } public String getName() { return name; } public int getAge() { return age; } public void setStudent(Student student) { this.student = student; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } } // Demonstrate serialization/deserialization of Java objects using // Jackson library class Main { public static void main(String[] args) { Person person = new Person("Jon Snow", 22); String jsonString = null; Person p = null; try { ObjectMapper mapper = new ObjectMapper(); // Serialize object – Convert `Person` object to JSON string jsonString = mapper.writeValueAsString(person); System.out.println("Converting Person object to JSON string:\n" + jsonString + "\n"); // Unserialize object – Convert JSON string to `Person` object p = mapper.readValue(jsonString, Person.class); System.out.println("Converting JSON string to Person object:\n" + p.toString()); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
Converting Person object to JSON string:
{"name":"Jon Snow","age":22,"student":{"id":"Jon_Snow_22","subjects":["Maths","Science"]}}
Converting JSON string to Person object:
[Jon Snow, 22, [Jon_Snow_22, [Maths, Science]]]
That’s all about serialization of Java objects using Jackson Library.
Also See:
Useful JSON Tools:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)