Skip to content

Commit 75630b6

Browse files
author
juan
committed
Update for article BAEL-1121
1 parent db68114 commit 75630b6

7 files changed

Lines changed: 407 additions & 53 deletions

File tree

‎jsonb/.gitignore‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#folders#
2+
.idea
3+
/target
4+
/neoDb*
5+
/data
6+
/src/main/webapp/WEB-INF/classes
7+
*/META-INF/*
8+
9+
# Packaged files #
10+
*.jar
11+
*.war
12+
*.ear

‎jsonb/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## JSON B

‎jsonb/pom.xml‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>json-b</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>json-b</name>
12+
<description>json-b sample project</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>javax.json.bind</groupId>
17+
<artifactId>javax.json.bind-api</artifactId>
18+
<version>${jsonb-api.version}</version>
19+
</dependency>
20+
<!-- Dependencies for Yasson -->
21+
<dependency>
22+
<groupId>org.eclipse</groupId>
23+
<artifactId>yasson</artifactId>
24+
<version>1.0</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.glassfish</groupId>
28+
<artifactId>javax.json</artifactId>
29+
<version>1.1.2</version>
30+
</dependency>
31+
<!-- Dependencies for Johnzon -->
32+
<!-- <dependency> -->
33+
<!-- <groupId>org.apache.geronimo.specs</groupId> -->
34+
<!-- <artifactId>geronimo-json_1.1_spec</artifactId> -->
35+
<!-- <version>${geronimo-json_1.1_spec.version}</version> -->
36+
<!-- </dependency> -->
37+
<!-- <dependency> -->
38+
<!-- <groupId>org.apache.johnzon</groupId> -->
39+
<!-- <artifactId>johnzon-jsonb</artifactId> -->
40+
<!-- <version>${johnzon.version}</version> -->
41+
<!-- </dependency> -->
42+
<!-- utils -->
43+
<dependency>
44+
<groupId>org.apache.commons</groupId>
45+
<artifactId>commons-collections4</artifactId>
46+
<version>${commons-collections4.version}</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.junit.jupiter</groupId>
52+
<artifactId>junit-jupiter-api</artifactId>
53+
<version>${junit.jupiter.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-engine</artifactId>
58+
<version>${junit.jupiter.version}</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.junit.platform</groupId>
63+
<artifactId>junit-platform-surefire-provider</artifactId>
64+
<version>${junit.platform.version}</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.junit.platform</groupId>
69+
<artifactId>junit-platform-runner</artifactId>
70+
<version>${junit.platform.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
74+
</dependencies>
75+
76+
<properties>
77+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
78+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
79+
<maven.compiler.source>1.8</maven.compiler.source>
80+
<maven.compiler.target>1.8</maven.compiler.target>
81+
<junit.platform.version>1.0.0</junit.platform.version>
82+
<junit.jupiter.version>5.0.0</junit.jupiter.version>
83+
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
84+
<johnzon.version>1.1.3</johnzon.version>
85+
<jsonb-api.version>1.0</jsonb-api.version>
86+
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
87+
<commons-collections4.version>4.1</commons-collections4.version>
88+
</properties>
89+
90+
</project>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com.baeldung.jsonb;
2+
3+
import java.math.BigDecimal;
4+
import java.time.LocalDate;
5+
6+
import javax.json.bind.annotation.JsonbDateFormat;
7+
import javax.json.bind.annotation.JsonbNumberFormat;
8+
import javax.json.bind.annotation.JsonbProperty;
9+
import javax.json.bind.annotation.JsonbTransient;
10+
11+
public class Person {
12+
13+
private int id;
14+
@JsonbProperty("person-name")
15+
private String name;
16+
@JsonbProperty(nillable = true)
17+
private String email;
18+
@JsonbTransient
19+
private int age;
20+
@JsonbDateFormat("dd-MM-yyyy")
21+
private LocalDate registeredDate;
22+
private BigDecimal salary;
23+
24+
public Person() {
25+
}
26+
27+
public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
28+
super();
29+
this.id = id;
30+
this.name = name;
31+
this.email = email;
32+
this.age = age;
33+
this.registeredDate = registeredDate;
34+
this.salary = salary;
35+
}
36+
37+
public int getId() {
38+
return id;
39+
}
40+
41+
public void setId(int id) {
42+
this.id = id;
43+
}
44+
45+
public int getAge() {
46+
return age;
47+
}
48+
49+
public void setAge(int age) {
50+
this.age = age;
51+
}
52+
53+
public String getEmail() {
54+
return email;
55+
}
56+
57+
public void setEmail(String email) {
58+
this.email = email;
59+
}
60+
61+
public String getName() {
62+
return name;
63+
}
64+
65+
public void setName(String name) {
66+
this.name = name;
67+
}
68+
69+
@JsonbNumberFormat(locale = "en_US", value = "#0.0")
70+
public BigDecimal getSalary() {
71+
return salary;
72+
}
73+
74+
public void setSalary(BigDecimal salary) {
75+
this.salary = salary;
76+
}
77+
78+
public LocalDate getRegisteredDate() {
79+
return registeredDate;
80+
}
81+
82+
public void setRegisteredDate(LocalDate registeredDate) {
83+
this.registeredDate = registeredDate;
84+
}
85+
86+
@Override
87+
public String toString() {
88+
StringBuilder builder = new StringBuilder();
89+
builder.append("Person [id=");
90+
builder.append(id);
91+
builder.append(", name=");
92+
builder.append(name);
93+
builder.append(", email=");
94+
builder.append(email);
95+
builder.append(", age=");
96+
builder.append(age);
97+
builder.append(", registeredDate=");
98+
builder.append(registeredDate);
99+
builder.append(", salary=");
100+
builder.append(salary);
101+
builder.append("]");
102+
return builder.toString();
103+
}
104+
105+
@Override
106+
public int hashCode() {
107+
final int prime = 31;
108+
int result = 1;
109+
result = prime * result + id;
110+
return result;
111+
}
112+
113+
@Override
114+
public boolean equals(Object obj) {
115+
if (this == obj)
116+
return true;
117+
if (obj == null)
118+
return false;
119+
if (getClass() != obj.getClass())
120+
return false;
121+
Person other = (Person) obj;
122+
if (id != other.id)
123+
return false;
124+
return true;
125+
}
126+
127+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package com.baeldung.jsonb;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.math.BigDecimal;
6+
import java.time.LocalDate;
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
import javax.json.bind.Jsonb;
12+
import javax.json.bind.JsonbBuilder;
13+
import javax.json.bind.JsonbConfig;
14+
import javax.json.bind.config.PropertyNamingStrategy;
15+
import javax.json.bind.config.PropertyOrderStrategy;
16+
17+
import org.apache.commons.collections4.ListUtils;
18+
import org.junit.Test;
19+
20+
public class JsonbTest {
21+
22+
@Test
23+
public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() {
24+
Jsonb jsonb = JsonbBuilder.create();
25+
// @formatter:off
26+
List<Person> personList = Arrays.asList(
27+
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
28+
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
29+
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
30+
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
31+
// @formatter:on
32+
String jsonArrayPerson = jsonb.toJson(personList);
33+
// @formatter:off
34+
String jsonExpected = "[" +
35+
"{\"email\":\"jhon@test.com\"," +
36+
"\"id\":1,\"person-name\":\"Jhon\"," +
37+
"\"registeredDate\":\"09-09-2019\"," +
38+
"\"salary\":\"1000.0\"}," +
39+
"{\"email\":\"jhon1@test.com\"," +
40+
"\"id\":2,\"person-name\":\"Jhon\"," +
41+
"\"registeredDate\":\"09-09-2019\"," +
42+
"\"salary\":\"1500.0\"},{\"email\":null," +
43+
"\"id\":3,\"person-name\":\"Jhon\"," +
44+
"\"registeredDate\":\"09-09-2019\"," +
45+
"\"salary\":\"1000.0\"}," +
46+
"{\"email\":\"tom@test.com\"," +
47+
"\"id\":4,\"person-name\":\"Tom\"," +
48+
"\"registeredDate\":\"09-09-2019\"," +
49+
"\"salary\":\"1500.0\"}"
50+
+ "]";
51+
// @formatter:on
52+
assertTrue(jsonArrayPerson.equals(jsonExpected));
53+
}
54+
55+
@Test
56+
public void givenPersonJsonArray_whenDeserializeWithJsonb_thenGetPersonList() {
57+
Jsonb jsonb = JsonbBuilder.create();
58+
// @formatter:off
59+
String personJsonArray =
60+
"[" +
61+
"{\"email\":\"jhon@test.com\"," +
62+
"\"id\":1,\"person-name\":\"Jhon\"," +
63+
"\"registeredDate\":\"09-09-2019\"," +
64+
"\"salary\":\"1000.0\"}," +
65+
"{\"email\":\"jhon1@test.com\"," +
66+
"\"id\":2,\"person-name\":\"Jhon\"," +
67+
"\"registeredDate\":\"09-09-2019\"," +
68+
"\"salary\":\"1500.0\"},{\"email\":null," +
69+
"\"id\":3,\"person-name\":\"Jhon\"," +
70+
"\"registeredDate\":\"09-09-2019\"," +
71+
"\"salary\":\"1000.0\"}," +
72+
"{\"email\":\"tom@test.com\"," +
73+
"\"id\":4,\"person-name\":\"Tom\"," +
74+
"\"registeredDate\":\"09-09-2019\"," +
75+
"\"salary\":\"1500.0\"}"
76+
+ "]";
77+
// @formatter:on
78+
@SuppressWarnings("serial")
79+
List<Person> personList = jsonb.fromJson(personJsonArray, new ArrayList<Person>() {
80+
}.getClass()
81+
.getGenericSuperclass());
82+
// @formatter:off
83+
List<Person> personListExpected = Arrays.asList(
84+
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
85+
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
86+
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
87+
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
88+
// @formatter:on
89+
assertTrue(ListUtils.isEqualList(personList, personListExpected));
90+
}
91+
92+
@Test
93+
public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() {
94+
JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
95+
Jsonb jsonb = JsonbBuilder.create(config);
96+
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
97+
String jsonPerson = jsonb.toJson(person);
98+
// @formatter:off
99+
String jsonExpected =
100+
"{\"email\":\"jhon@test.com\"," +
101+
"\"id\":1," +
102+
"\"person-name\":\"Jhon\"," +
103+
"\"registered_date\":\"07-09-2019\"," +
104+
"\"salary\":\"1000.0\"}";
105+
// @formatter:on
106+
assertTrue(jsonExpected.equals(jsonPerson));
107+
}
108+
109+
@Test
110+
public void givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson() {
111+
JsonbConfig config = new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);
112+
Jsonb jsonb = JsonbBuilder.create(config);
113+
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
114+
String jsonPerson = jsonb.toJson(person);
115+
// @formatter:off
116+
String jsonExpected =
117+
"{\"salary\":\"1000.0\","+
118+
"\"registeredDate\":\"07-09-2019\"," +
119+
"\"person-name\":\"Jhon\"," +
120+
"\"id\":1," +
121+
"\"email\":\"jhon@test.com\"}";
122+
// @formatter:on
123+
assertTrue(jsonExpected.equals(jsonPerson));
124+
}
125+
126+
@Test
127+
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
128+
Jsonb jsonb = JsonbBuilder.create();
129+
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
130+
String jsonPerson = jsonb.toJson(person);
131+
// @formatter:off
132+
String jsonExpected =
133+
"{\"email\":\"jhon@test.com\"," +
134+
"\"id\":1," +
135+
"\"person-name\":\"Jhon\"," +
136+
"\"registeredDate\":\"07-09-2019\"," +
137+
"\"salary\":\"1000.0\"}";
138+
// @formatter:on
139+
assertTrue(jsonExpected.equals(jsonPerson));
140+
}
141+
142+
@Test
143+
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
144+
Jsonb jsonb = JsonbBuilder.create();
145+
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
146+
// @formatter:off
147+
String jsonPerson =
148+
"{\"email\":\"jhon@test.com\"," +
149+
"\"id\":1," +
150+
"\"person-name\":\"Jhon\"," +
151+
"\"registeredDate\":\"07-09-2019\"," +
152+
"\"salary\":\"1000.0\"}";
153+
// @formatter:on
154+
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
155+
.equals(person));
156+
}
157+
158+
}

0 commit comments

Comments
 (0)