Skip to content

Commit 3d2be28

Browse files
authored
Code for Jackson post (thombergs#197)
* add the initial maven project * add a few code examples * add a few code examples * add a few code examples * add a few code examples * add a few code examples * add JsonAnyGetter example
1 parent 937fee0 commit 3d2be28

File tree

15 files changed

+405
-0
lines changed

15 files changed

+405
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
target/*
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/nbbuild/
22+
/dist/
23+
/nbdist/
24+
build/
25+
!**/src/main/**/build/
26+
!**/src/test/**/build/
27+
28+
### VS Code ###
29+
.vscode/

core-java/jackson/jackson/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.reflectoring</groupId>
8+
<artifactId>jackson</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.fasterxml.jackson.core</groupId>
19+
<artifactId>jackson-databind</artifactId>
20+
<version>2.13.3</version>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.datatype</groupId>
25+
<artifactId>jackson-datatype-jsr310</artifactId>
26+
<version>2.13.3</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<version>5.9.0-M1</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.projectlombok</groupId>
38+
<artifactId>lombok</artifactId>
39+
<version>1.18.24</version>
40+
<scope>provided</scope>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>org.assertj</groupId>
45+
<artifactId>assertj-core</artifactId>
46+
<version>3.23.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
51+
</dependencies>
52+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.reflectoring.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonAnySetter;
4+
import com.fasterxml.jackson.annotation.JsonSetter;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Getter
15+
public class Car {
16+
@JsonSetter("carBrand")
17+
private String brand;
18+
private Map<String, String> unrecognizedFields = new HashMap<>();
19+
20+
@JsonAnySetter
21+
public void allSetter(String fieldName, String fieldValue) {
22+
unrecognizedFields.put(fieldName, fieldValue);
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.reflectoring.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4+
import com.fasterxml.jackson.annotation.JsonGetter;
5+
import lombok.AllArgsConstructor;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.Map;
9+
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class Cat {
13+
private String name;
14+
15+
@JsonAnyGetter
16+
Map<String, String> map = Map.of(
17+
"name", "Jack",
18+
"surname", "wolfskin"
19+
);
20+
21+
@JsonGetter("catName")
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public Cat(String name) {
27+
this.name = name;
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.reflectoring.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import lombok.NoArgsConstructor;
7+
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
@Getter
11+
public class Dog {
12+
private String name;
13+
@JsonIgnore
14+
private Integer age;
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.reflectoring.pojo;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
7+
import java.util.Date;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class Employee {
13+
private String firstName;
14+
private String lastName;
15+
private int age;
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.reflectoring.pojo;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonSetter;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
9+
import java.time.LocalDate;
10+
11+
@NoArgsConstructor
12+
@AllArgsConstructor
13+
@Getter
14+
public class Order {
15+
private int id;
16+
@JsonFormat(pattern = "dd/MM/yyyy")
17+
private LocalDate date;
18+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.reflectoring;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.DeserializationFeature;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.reflectoring.pojo.*;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.time.LocalDate;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
18+
public class JacksonTest {
19+
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
20+
21+
@Test
22+
void jsonStringToPojo() throws JsonProcessingException {
23+
String employeeJson = "{\n" +
24+
" \"firstName\" : \"Jalil\",\n" +
25+
" \"lastName\" : \"Jarjanazy\",\n" +
26+
" \"age\" : 30\n" +
27+
"}";
28+
29+
Employee employee = objectMapper.readValue(employeeJson, Employee.class);
30+
31+
assertThat(employee.getFirstName()).isEqualTo("Jalil");
32+
}
33+
34+
@Test
35+
void pojoToJsonString() throws JsonProcessingException {
36+
Employee employee = new Employee("Mark", "James", 20);
37+
38+
String json = objectMapper.writeValueAsString(employee);
39+
40+
assertThat(json).isEqualTo("{\"firstName\":\"Mark\",\"lastName\":\"James\",\"age\":20}");
41+
}
42+
43+
@Test
44+
void jsonFileToPojo() throws IOException {
45+
File file = new File("src/test/resources/employee.json");
46+
47+
Employee employee = objectMapper.readValue(file, Employee.class);
48+
49+
assertThat(employee.getAge()).isEqualTo(44);
50+
assertThat(employee.getLastName()).isEqualTo("Simpson");
51+
assertThat(employee.getFirstName()).isEqualTo("Homer");
52+
}
53+
54+
@Test
55+
void byteArrayToPojo() throws IOException {
56+
String employeeJson = "{\n" +
57+
" \"firstName\" : \"Jalil\",\n" +
58+
" \"lastName\" : \"Jarjanazy\",\n" +
59+
" \"age\" : 30\n" +
60+
"}";
61+
62+
Employee employee = objectMapper.readValue(employeeJson.getBytes(), Employee.class);
63+
64+
assertThat(employee.getFirstName()).isEqualTo("Jalil");
65+
}
66+
67+
@Test
68+
void fileToListOfPojos() throws IOException {
69+
File file = new File("src/test/resources/employeeList.json");
70+
List<Employee> employeeList = objectMapper.readValue(file, new TypeReference<>(){});
71+
72+
assertThat(employeeList).hasSize(2);
73+
assertThat(employeeList.get(0).getAge()).isEqualTo(33);
74+
assertThat(employeeList.get(0).getLastName()).isEqualTo("Simpson");
75+
assertThat(employeeList.get(0).getFirstName()).isEqualTo("Marge");
76+
}
77+
78+
@Test
79+
void fileToMap() throws IOException {
80+
File file = new File("src/test/resources/employee.json");
81+
Map<String, Object> employee = objectMapper.readValue(file, new TypeReference<>(){});
82+
83+
assertThat(employee.keySet()).containsExactly("firstName", "lastName", "age");
84+
85+
assertThat(employee.get("firstName")).isEqualTo("Homer");
86+
assertThat(employee.get("lastName")).isEqualTo("Simpson");
87+
assertThat(employee.get("age")).isEqualTo(44);
88+
}
89+
90+
@Test
91+
void fileToPojoWithUnknownProperties() throws IOException {
92+
File file = new File("src/test/resources/employeeWithUnknownProperties.json");
93+
94+
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
95+
96+
Employee employee = objectMapper.readValue(file, Employee.class);
97+
98+
assertThat(employee.getFirstName()).isEqualTo("Homer");
99+
assertThat(employee.getLastName()).isEqualTo("Simpson");
100+
assertThat(employee.getAge()).isEqualTo(44);
101+
}
102+
103+
@Test
104+
void orderToJson() throws JsonProcessingException {
105+
Order order = new Order(1, LocalDate.of(1900,2,1));
106+
107+
String json = objectMapper.writeValueAsString(order);
108+
109+
System.out.println(json);
110+
}
111+
112+
@Test
113+
void orderToJsonWithDate() throws JsonProcessingException {
114+
Order order = new Order(1, LocalDate.of(2023, 1, 1));
115+
116+
String json = objectMapper.writeValueAsString(order);
117+
118+
System.out.println(json);
119+
}
120+
@Test
121+
void fileToOrder() throws IOException {
122+
File file = new File("src/test/resources/order.json");
123+
124+
Order order = objectMapper.readValue(file, Order.class);
125+
126+
assertThat(order.getDate().getYear()).isEqualTo(2000);
127+
assertThat(order.getDate().getMonthValue()).isEqualTo(4);
128+
assertThat(order.getDate().getDayOfMonth()).isEqualTo(30);
129+
}
130+
@Test
131+
void fileToCar() throws IOException {
132+
File file = new File("src/test/resources/car.json");
133+
134+
Car car = objectMapper.readValue(file, Car.class);
135+
136+
assertThat(car.getBrand()).isEqualTo("BMW");
137+
}
138+
139+
@Test
140+
void fileToUnrecognizedCar() throws IOException {
141+
File file = new File("src/test/resources/carUnrecognized.json");
142+
143+
Car car = objectMapper.readValue(file, Car.class);
144+
145+
assertThat(car.getUnrecognizedFields()).containsKey("productionYear");
146+
}
147+
148+
@Test
149+
void catToJson() throws JsonProcessingException {
150+
Cat cat = new Cat("Monica");
151+
152+
String json = objectMapper.writeValueAsString(cat);
153+
154+
System.out.println(json);
155+
156+
}
157+
158+
@Test
159+
void catToJsonWithMap() throws JsonProcessingException {
160+
Cat cat = new Cat("Monica");
161+
162+
String json = objectMapper.writeValueAsString(cat);
163+
164+
System.out.println(json);
165+
}
166+
167+
@Test
168+
void dogToJson() throws JsonProcessingException {
169+
Dog dog = new Dog("Max", 3);
170+
171+
String json = objectMapper.writeValueAsString(dog);
172+
173+
System.out.println(json);
174+
}
175+
@Test
176+
void fileToDog() throws IOException {
177+
File file = new File("src/test/resources/dog.json");
178+
179+
Dog dog = objectMapper.readValue(file, Dog.class);
180+
181+
assertThat(dog.getName()).isEqualTo("bobby");
182+
assertThat(dog.getAge()).isNull();
183+
}
184+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"carBrand" : "BMW"
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"carBrand" : "BMW",
3+
"productionYear": 1996
4+
}

0 commit comments

Comments
 (0)