jackson java json Latest

Jackson – How to structure JSON – Mkyong.com

Jackson presents writeValue () and readValue () methods to convert Java objects from JSON /

mapper.writeValue – Java objects to JSON

ObjectMapper Mapper = new ObjectMapper ();

// Java object in JSON file
mapper.writeValue (new file ("c: test staff.json"), new employees ());

// Java object for JSON, default compact printing
String jsonString = mapper.writeValueAsString (new employees ());

// pretty-print
The string jsonString2 = mapper.writerWithDefaultPrettyPrinter (). WriteValueAsString (new employees ());

mapper.readValue – JSON for Java

ObjectMapper Mapper = new ObjectMapper ();

// JSON file for Java object
Employees obj = mapper.readValue (new file ("c: test staff .json"), Employees.class);

// JSON URL for Java object
Employees obj = mapper.readValue (new URL ("http: //some-domains/api/staff.json"), Employees.class);

// JSON string for Java object
Employees obj = mapper.readValue ("& # 39; name & # 39 ;: & # 39; mkyong & # 39;", Employees.class);

P.S Tested in Jackson 2.9.8

1. Obtain Jackson

Declares jackson-datasind; Terminal

$ mvn dependence: tree

com.fasterxml.jackson.core: jackson-datasind: jar: 2.9.eight: compile
[INFO] + – com.fasterxml.jackson.core: jackson: jar: 2.9.zero: compile
[INFO] – com.fasterxml.jackson.core: jackson-core: jar: 2.9.eight: compile

2. POJO

Simple Java object, POJO, to be tested later

Employees.java

public class employees

personal language identify;
personal age;
personal String [] place; // Array
personal listing ; // Record
personal map ; // Map

// getters, setters, some boring stuff

three. Java Objects for JSON

JacksonExample1.java

package deal com.mkyong;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class JacksonExample1

public Static void primary (String [] args)

ObjectMapper Mapper = new ObjectMapper ();

Employees Employees = createStaff ();

attempt

// Java objects in a JSON file
mapper.writeValue (new file ("c: test staff.json"), employees);

// Java objects for the JSON string – compact-print
String jsonString = mapper.writeValueAsString (employees);

System.out.println (jsonString);

// Java objects for the JSON string – fairly-print
JsonInString2 = mapper.writerWithDefaultPrettyPrinter (). WriteValueAsString (employees);

System.out.println (jsonInString2);

within the corridor (IOException e)
e.printStackTrace ();

personal static employees createStaff ()

Employees employees = new employees ();

employees.setName ("mkyong");
employees.setAge (38);
employees.setPosition (new string [] "Founder", "CTO", "Writer");
Map wage = new HashMap ()
put ("2010", the new BigDecimal (10000));
put ("2012", the brand new BigDecimal (12000));
put ("2018", the brand new BigDecimal (14000));
;
employees.setSalary (wage);
employees.setSkills (Arrays.asList ("java", "python", "node", "Kotlin"));

return of personnel;

Power

c: Check of Work.Json

"Name": "mkyong", "age": 38, "to position": [“Founder”,”CTO”,”Writer”] "skills": [“java”,”python”,”node”,”kotlin”] "pay": "2018": 14000, "2012": 12000, "2010": 10000

Terminal

"Name": "mkyong", "age": 38, "to position": [“Founder”,”CTO”,”Writer”] "skills": [“java”,”python”,”node”,”kotlin”] "pay": "2018": 14000, "2012": 12000, "2010": 10000

"name": "mkyong"
"age": 38,
"station": [ “Founder”, “CTO”, “Writer” ] "Skills": [ “java”, “python”, “node”, “kotlin” ] "pay":
"2018": 14000,
"2012": 12,000
"2010": 10,000

4. JSON and Java Object

JacksonExample2 and Java

package deal com.mkyong;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class JacksonExample2

public Static void principal (String [] args)

ObjectMapper Mapper = new ObjectMapper ();

attempt

// JSON file for Java object
Employees Employees = mapper.readValue (new file ("c: test staff .json"), Employees.class);

// JSON string for Java object
String jsonInString = "" identify ":" mkyong "," age ": 37," expertise ": [“java”,”python”]";
Employees Staff2 = mapper.readValue (jsonInString, Employees.class);

// compact printing
System.out.println (staff2);

// fairly print
The string prettyStaff1 = mapper.writerWithDefaultPrettyPrinter (). WriteValueAsString (staff2);

System.out.println (prettyStaff1);

in the hall (IOException e)
e.printStackTrace ();

Energy

Employees identify = & # 39; mkyong & # 39 ;, age = 37, location = null, expertise = [java, python] salary = null

"name": "mkyong"
"age": 37,
"station": zero
"Skills": [ “java”, “python” ] "salary": zero

5. @JsonProperty – Naming the JSON Subject

5.1 Default

public class employees

personal language identify;
personal age;

Energy

"name": "abc", "age": 38

5.2 Change the identify of the property to @JsonProperty

public class employees

@JsonProperty ("CUSTOM_NAME")
personal language identify;
personal age;

Power

"custom_name": "abc", "age": 38

6. @JsonInclude – Skip Null Fields

Jackson defaults to null fields.

"name": "mkyong"
"age": 38,
"station": zero
"skills": zero,
"salary": zero

6.1 @ JsonIncludes class degree

Employees.java

import com.fasterxml.jackson.annotation.JsonInclude;

// skip null fields, class degree
@JsonInclude (JsonInclude.Embrace.NON_NULL) // ignores all null fields
public class employees

personal language identify;
personal age;
personal String [] place;
personal record ;
personal map ;
// …

6.2 @ JsonInclude to the Area

Employees.java

import com.fasterxml.jackson.annotation.JsonInclude;

public class employees

personal language identify;
personal age;

@JsonInclude (JsonInclude.Embrace.NON_NULL) // ignores only the null subject for this function
personal String [] place;

personal record ;

personal map ;

Energy

"name": "mkyong"
"age": 38,
"skill": zero,
"salary": zero

6.three Worldwide

ObjectMapper Mapper = new ObjectMapper ();

// ignore all null fields worldwide
mapper.setSerializationInclusion (JsonInclude.Embrace.NON_NULL);

7. @JsonView

7.1 @ JsonView is used to prohibit the display of fields for various users. Instance:

CompanyViews.java

package deal com.mkyong;

public class CompanyViews

public static class Regular ;

Public Static Class Manager Expands Normal ;

Normal view exhibits only identify and age, Manager view can present all.

Employees.java

import com.fasterxml.jackson.annotation.JsonView;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Listing;
import java.util.Map;

public class employees

@JsonView (CompanyViews.Normal.class)
personal language identify;

@JsonView (CompanyViews.Regular.class)
personal age;

@JsonView (CompanyViews.Supervisor.class)
personal String [] position;

@JsonView (CompanyViews.Supervisor.class)
personal record ;

@JsonView (CompanyViews.Manager.class)
personal map ;

JacksonExample.java

package deal com.mkyong;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class JacksonExample

public Static void most important (String [] args)

ObjectMapper Mapper = new ObjectMapper ();

Employees Employees = createStaff ();

attempt

// permit lovely printing
mapper.enable (SerializationFeature.INDENT_OUTPUT);

// regular
NormalView = Mapper
.writerWithView (CompanyViews.Normal.class)
.writeValueAsString (employees);

System.out.format ("Normal views n% s n", normal view);

// chief
String managerView = Mapper
.writerWithView (CompanyViews.Supervisor.class)
.writeValueAsString (employees);

System.out.format ("Manager views n% s n", managerView);

within the corridor (IOException e)
e.printStackTrace ();

personal static employees createStaff ()

Employees employees = new employees ();

employees.setName ("mkyong");
employees.setAge (38);
employees.setPosition (new string [] "Founder", "CTO", "Writer");
Map salary = new HashMap ()
put ("2010", the new BigDecimal (10000));
put ("2012", the brand new BigDecimal (12000));
put ("2018", the brand new BigDecimal (14000));
;
employees.setSalary (salary);
employees.setSkills (Arrays.asList ("java", "python", "node", "Kotlin"));

return of personnel;

Energy

Regular views

"name": "mkyong"
"age": 38

Supervisor Views

"name": "mkyong"
"age": 38,
"station": [ “Founder”, “CTO”, “Writer” ] "Skills": [ “java”, “python”, “node”, “kotlin” ] "pay":
"2018": 14000,
"2012": 12,000
"2010": 10,000

eight. @JsonIgnore and @JsonIgnoreProperties

Jackson accommodates all fields, even static or transient fields.

eight.1 @ JsonIgnore to take away Area Fields

import com.fasterxml.jackson.annotation.JsonIgnore;

public class employees

personal language identify;
personal age;
personal String [] position;

@JsonIgnore
personal listing ;

@JsonIgnore
personal map ;

Power

"name": "mkyong"
"age": 38,
"station": [ “Founder”, “CTO”, “Writer” ]

eight.2 @ JsonIgnoreProperties Fields Leaving Fields at Class

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties ("salary", "location")
public class employees

personal language identify;
personal age;
personal String [] position;
personal listing ;
personal map ;

Energy

"name": "mkyong"
"age": 38,
"Skills": [ “java”, “python”, “node”, “kotlin” ]

9. Incessantly Asked Questions

9.1 Changing a JSON Group String to a Listing

String json = "[“name”:”mkyong”, “age”:38, “name”:”laplap”, “age”:5]";

Record listing = Arrays.asList (mapper.readValue (json, Employees [] .class));

// or so:
// Listing record = mapper.readValue (json, new TypeReference <Record > () );

9.2 Converting a JSON String to a Chart

String json = "" identify ":" mkyong "," age ":" 33 "";

Map map = mapper.readValue (json, Map.class);

// or so:
// Map map = mapper.readValue (json, new TypeReference <Map > () );

map.forEach ((okay, v) -> System.out.format ("[key]:% s [value]:% s", okay, v));

Departure

[key]: identify [value]: mkyong
[key]: age [value]: 33

9.3 What if a posh JSON structure can’t simply map a Java class?
Reply: Attempt Jackson TreeModel to convert JSON knowledge to JsonNode so we will simply add, update or delete JSON nodes. [19659003] References