Jackson serialization and deserialization of JSON data
The development of web technology today, JSON and XML has become a fact of the web data standards, but this format of data manual parsing and very cumbersome, the software engineering community is never a lack of tools, whenever there is a need for a variety of libraries, frameworks and tools to solve these fundamental problems, Jackson is one of these tools, and using this tool developers can completely free themselves from the repetitive work of manually ending JSON data. Using Jackson first needs to download the appropriate class library, as the following Maven dependency lists the complete pom dependency.
1 <Dependency>2 <groupId>Com.fasterxml.jackson.core</groupId>3 <Artifactid>Jackson-core</Artifactid>4 <version>${jackson-2-version}</version>5 </Dependency>6 7 <!--Just the annotations; Use this dependency if you want to attach annotations8 To classes without connecting them to the code. -9 <Dependency>Ten <groupId>Com.fasterxml.jackson.core</groupId> One <Artifactid>Jackson-annotations</Artifactid> A <version>${jackson-2-version}</version> - </Dependency> - the <!--DataBinding; Objectmapper, Jsonnode and related classes is here - - <Dependency> - <groupId>Com.fasterxml.jackson.core</groupId> - <Artifactid>Jackson-databind</Artifactid> + <version>${jackson-2-version}</version> - </Dependency> + A <!--Smile (binary JSON). Other artifacts the this group does other formats. - at <Dependency> - <groupId>Com.fasterxml.jackson.dataformat</groupId> - <Artifactid>Jackson-dataformat-smile</Artifactid> - <version>${jackson-2-version}</version> - </Dependency> - <!--Jax-rs provider - in <Dependency> - <groupId>Com.fasterxml.jackson.jaxrs</groupId> to <Artifactid>Jackson-jaxrs-json-provider</Artifactid> + <version>${jackson-2-version}</version> - </Dependency> the <!--Support for jax-b annotations as additional configuration - * <Dependency> $ <groupId>Com.fasterxml.jackson.module</groupId>Panax Notoginseng <Artifactid>Jackson-module-jaxb-annotations</Artifactid> - <version>${jackson-2-version}</version> the </Dependency>View Code
For example, we need to parse the JSON data as follows:
{ "id": 123, "name": "Pankaj", "permanent": true, "address": { "street": "Albany Dr", "City" : "San Jose", "ZipCode": 95129 }, "Phonenumbers": [ 123456, 987654 ], "role": " Manager ", " Cities ": [ " Los Angeles ", " New York " ], " Properties ": { " age ":" Years ", "Salary": "+ USD" }}
The corresponding model Class is as follows:
import Java.util.arrays;import java.util.list;import java.util.Map; public class Employee {private int id; private String name; Private Boolean permanent; private address address; Private long[] phonenumbers; Private String role; Private List<String>cities; Private Map<String, String> Properties public int getId () {return id; } public void setId (int id) {this.id = ID; } public String GetName () {return name; } public void SetName (String name) {this.name = name; } public boolean ispermanent () {return permanent; The public void Setpermanent (Boolean permanent) {this.permanent = permanent; } public Address getaddress () {return address; } public void Setaddress (address address) {this.address = address; } public long[] Getphonenumbers () {return phonenumbers; } public void Setphonenumbers (long[] phonenumbers) {this.phonenumbers = Phonenumbers; Public String Getrole () {return role; public void Setrole (String role) {this.role = role; } @Override Public String toString () {StringBuilder sb = new StringBuilder (); Sb.append ("* * * * Employee Details *****\n"); Sb.append ("id=" +GetId () + "\ n"); Sb.append ("Name=" +getname () + "\ n"); Sb.append ("permanent=" +ispermanent () + "\ n"); Sb.append ("role=" +getrole () + "\ n"); Sb.append ("Phone numbers=" +arrays.tostring (Getphonenumbers ()) + "\ n"); Sb.append ("address=" +getaddress () + "\ n"); Sb.append ("cities=" +arrays.tostring (GetCities (). ToArray ()) + "\ n"); Sb.append ("properties=" +getproperties () + "\ n"); Sb.append ("*****************************"); return sb.tostring (); } public List<String>GetCities () {return cities; } public void Setcities (List<String>cities) {this.cities = cities; } public Map<String, String>getProperties () {return properties; } public void SetProperties (Map<String, String>properties) {this.properties = properties; }}
When we need to manipulate JSON data, the following code demonstrates how to deserialize JSON data into objects and how to serialize objects into JSON data.
Import Java.io.file;import java.io.ioexception;import Java.io.stringwriter;import Java.nio.file.files;import Java.nio.file.paths;import Java.util.arraylist;import Java.util.hashmap;import Java.util.Iterator;import Java.util.list;import Java.util.Map; Import Com.fasterxml.jackson.core.type.typereference;import Com.fasterxml.jackson.databind.jsonnode;import Com.fasterxml.jackson.databind.objectmapper;import Com.fasterxml.jackson.databind.serializationfeature;import Com.fasterxml.jackson.databind.node.objectnode;public class Jacksonobjectmapperexample {public static void main (Stri Ng[] args) throws IOException {//read JSON file data to String byte[] Jsondata = Files.readallbyte S (Paths.get ("C:\\employee.txt")); Create Objectmapper instance Objectmapper Objectmapper = new Objectmapper (); Convert JSON string to object Employee emp = Objectmapper.readvalue (Jsondata, Employee.class); System.out.printlN ("Employee object\n" +emp); Convert Object to JSON string Employee EMP1 = Createemployee (); Configure Object mapper for pretty print objectmapper.configure (Serializationfeature.indent_output, true); Writing to console, can write to any output stream such as file StringWriter stringemp = new Stringwri ter (); Objectmapper.writevalue (Stringemp, EMP1); System.out.println ("Employee JSON is\n" +stringemp); public static employee Createemployee () {Employee EMP = new Employee (); Emp.setid (100); Emp.setname ("David"); Emp.setpermanent (FALSE); Emp.setphonenumbers (new long[] {123456, 987654}); Emp.setrole ("Manager"); Address add = new address (); Add.setcity ("Bangalore"); Add.setstreet ("BTM 1st Stage"); Add.setzipcode (560100); Emp.setaddress (add); List<String>Cities = new ArrayList<String>(); Cities.add ("Los Angeles"); Cities.add ("New York"); Emp.setcities (cities); Map<String, String>props = new HashMap<String, String>(); Props.put ("Salary", "Rs"); Props.put ("Age", "years"); Emp.setproperties (props); return EMP; } }
The results are as follows:
Employee object***** employee Details *****id=123name=pankajpermanent=truerole=managerphone numbers=[123456, 987654] Address=albany Dr, San Jose, 95129cities=[los Angeles, New york]properties={age=29 years, salary=1000 usd}************** Employee JSON is{ "id": +, "name": "David", "permanent": false, "address": { " Street ": BTM 1st Stage", "City": "Bangalore", " zipcode": 560100 }, "Phonenumbers": [123456, 98765 4], "role": "Manager", "Cities": ["Los Angeles", "New York"], "Properties": { "salary": "R S ", " age ":" Years " }}
Summarize
This article summarizes the process of serializing Java objects to JSON objects and deserializing them with a complete example, and hopefully it will help.
Jackson serialization and deserialization complete sample of JSON data