Skip to content

Commit 0b5fcf9

Browse files
committed
added jackson-databinding solution code
1 parent 39b2dd1 commit 0b5fcf9

File tree

24 files changed

+630
-55
lines changed

24 files changed

+630
-55
lines changed

08-spring-rest/solution-code-jackson-databinding-json-demo/data/sample.json renamed to 08-spring-rest/solution-code-jackson-databinding-json-demo-01-procesing-json/data/sample-full.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"id": 14,
3-
"firstName": "Luca",
4-
"lastName": "Fischer",
3+
"firstName": "Mario",
4+
"lastName": "Rossi",
55
"active": true,
66
"address": {
77
"street": "100 Main St",
88
"city": "Philadelphia",
99
"state": "Pennsylvania",
10-
"zip": "19403",
10+
"zip": "19103",
1111
"country": "USA"
1212
},
1313
"languages" : ["Java", "C#", "Python", "Javascript"]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": 14,
3+
"firstName": "Mario",
4+
"lastName": "Rossi",
5+
"active": true
6+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.luv2code</groupId>
6+
<artifactId>jackson-databind-json-demo-01-processing-json</artifactId>
7+
<version>1.0.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>jackson-demo</name>
11+
12+
13+
<properties>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<!-- TODO: Add your dependency here -->
21+
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-databind</artifactId>
25+
<version>2.9.5</version>
26+
</dependency>
27+
28+
</dependencies>
29+
30+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.luv2code.jackson.json.demo;
2+
3+
import java.io.File;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class Driver {
8+
9+
public static void main(String[] args) {
10+
11+
try {
12+
// create object mapper
13+
ObjectMapper mapper = new ObjectMapper();
14+
15+
// read JSON file and map/convert to Java POJO:
16+
// data/sample-lite.json
17+
18+
Student theStudent = mapper.readValue(
19+
new File("data/sample-lite.json"), Student.class);
20+
21+
// print first name and last name
22+
System.out.println("First name = " + theStudent.getFirstName());
23+
System.out.println("Last name = " + theStudent.getLastName());
24+
25+
}
26+
catch (Exception exc) {
27+
exc.printStackTrace();
28+
}
29+
}
30+
}
31+
32+
33+
34+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.luv2code.jackson.json.demo;
2+
3+
public class Student {
4+
5+
private int id;
6+
private String firstName;
7+
private String lastName;
8+
private boolean active;
9+
10+
public Student() {
11+
12+
}
13+
14+
public int getId() {
15+
return id;
16+
}
17+
18+
public void setId(int id) {
19+
this.id = id;
20+
}
21+
22+
public String getFirstName() {
23+
return firstName;
24+
}
25+
26+
public void setFirstName(String firstName) {
27+
this.firstName = firstName;
28+
}
29+
30+
public String getLastName() {
31+
return lastName;
32+
}
33+
34+
public void setLastName(String lastName) {
35+
this.lastName = lastName;
36+
}
37+
38+
public boolean isActive() {
39+
return active;
40+
}
41+
42+
public void setActive(boolean active) {
43+
this.active = active;
44+
}
45+
46+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"id": 14,
3+
"firstName": "Mario",
4+
"lastName": "Rossi",
5+
"active": true,
6+
"address": {
7+
"street": "100 Main St",
8+
"city": "Philadelphia",
9+
"state": "Pennsylvania",
10+
"zip": "19103",
11+
"country": "USA"
12+
},
13+
"languages" : ["Java", "C#", "Python", "Javascript"]
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": 14,
3+
"firstName": "Mario",
4+
"lastName": "Rossi",
5+
"active": true
6+
}

08-spring-rest/solution-code-jackson-databinding-json-demo/pom.xml renamed to 08-spring-rest/solution-code-jackson-databinding-json-demo-02-nested-objects/pom.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44

55
<groupId>com.luv2code</groupId>
6-
<artifactId>jackson-json-demo</artifactId>
6+
<artifactId>jackson-databind-json-demo-02-nested-objects</artifactId>
77
<version>1.0.0</version>
88
<packaging>jar</packaging>
99

@@ -17,12 +17,14 @@
1717

1818
<dependencies>
1919

20+
<!-- TODO: Add your dependency here -->
21+
2022
<dependency>
2123
<groupId>com.fasterxml.jackson.core</groupId>
2224
<artifactId>jackson-databind</artifactId>
23-
<version>2.9.10.4</version>
24-
</dependency>
25-
25+
<version>2.9.5</version>
26+
</dependency>
27+
2628
</dependencies>
2729

2830
</project>

08-spring-rest/solution-code-jackson-databinding-json-demo/src/main/java/com/luv2code/jacksondemo/Address.java renamed to 08-spring-rest/solution-code-jackson-databinding-json-demo-02-nested-objects/src/main/java/com/luv2code/jackson/json/demo/Address.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.luv2code.jacksondemo;
1+
package com.luv2code.jackson.json.demo;
22

33
public class Address {
44

@@ -52,4 +52,5 @@ public void setCountry(String country) {
5252
this.country = country;
5353
}
5454

55+
5556
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.luv2code.jackson.json.demo;
2+
3+
import java.io.File;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
7+
public class Driver {
8+
9+
public static void main(String[] args) {
10+
11+
try {
12+
// create object mapper
13+
ObjectMapper mapper = new ObjectMapper();
14+
15+
// read JSON file and map/convert to Java POJO:
16+
// data/sample-lite.json
17+
18+
Student theStudent = mapper.readValue(
19+
new File("data/sample-full.json"), Student.class);
20+
21+
// print first name and last name
22+
System.out.println("First name = " + theStudent.getFirstName());
23+
System.out.println("Last name = " + theStudent.getLastName());
24+
25+
}
26+
catch (Exception exc) {
27+
exc.printStackTrace();
28+
}
29+
}
30+
}
31+
32+
33+
34+

0 commit comments

Comments
 (0)