Merge pull request #3005 from earth001/master

Update for article BAEL-1121
This commit is contained in:
Carsten Gräf 2017-11-10 06:33:45 +01:00 committed by GitHub
commit a0efd94117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 423 additions and 38 deletions

12
jsonb/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
#folders#
.idea
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

1
jsonb/README.md Normal file
View File

@ -0,0 +1 @@
## JSON B

106
jsonb/pom.xml Normal file
View File

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>json-b</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>json-b</name>
<description>json-b sample project</description>
<profiles>
<profile>
<id>yasson</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<!-- Dependencies for Yasson -->
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>yasson</artifactId>
<version>${yasson.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${javax.json.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>johnzon</id>
<dependencies>
<!-- Dependencies for Johnzon -->
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
<version>${geronimo-json_1.1_spec.version}</version>
</dependency>
<dependency>
<groupId>org.apache.johnzon</groupId>
<artifactId>johnzon-jsonb</artifactId>
<version>${johnzon.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>javax.json.bind</groupId>
<artifactId>javax.json.bind-api</artifactId>
<version>${jsonb-api.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>${commons-collections4.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.platform.version>1.0.0</junit.platform.version>
<junit.jupiter.version>5.0.0</junit.jupiter.version>
<maven-surefire-plugin.version>2.20</maven-surefire-plugin.version>
<jsonb-api.version>1.0</jsonb-api.version>
<johnzon.version>1.1.3</johnzon.version>
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
<yasson.version>1.0</yasson.version>
<javax.json.version>1.1.2</javax.json.version>
<commons-collections4.version>4.1</commons-collections4.version>
</properties>
</project>

View File

@ -0,0 +1,127 @@
package com.baeldung.jsonb;
import java.math.BigDecimal;
import java.time.LocalDate;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbNumberFormat;
import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbTransient;
public class Person {
private int id;
@JsonbProperty("person-name")
private String name;
@JsonbProperty(nillable = true)
private String email;
@JsonbTransient
private int age;
@JsonbDateFormat("dd-MM-yyyy")
private LocalDate registeredDate;
private BigDecimal salary;
public Person() {
}
public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
super();
this.id = id;
this.name = name;
this.email = email;
this.age = age;
this.registeredDate = registeredDate;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@JsonbNumberFormat(locale = "en_US", value = "#0.0")
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
public LocalDate getRegisteredDate() {
return registeredDate;
}
public void setRegisteredDate(LocalDate registeredDate) {
this.registeredDate = registeredDate;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person [id=");
builder.append(id);
builder.append(", name=");
builder.append(name);
builder.append(", email=");
builder.append(email);
builder.append(", age=");
builder.append(age);
builder.append(", registeredDate=");
builder.append(registeredDate);
builder.append(", salary=");
builder.append(salary);
builder.append("]");
return builder.toString();
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
return true;
}
}

View File

@ -0,0 +1,158 @@
package com.baeldung.jsonb;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbConfig;
import javax.json.bind.config.PropertyNamingStrategy;
import javax.json.bind.config.PropertyOrderStrategy;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;
public class JsonbTest {
@Test
public void givenPersonList_whenSerializeWithJsonb_thenGetPersonJsonArray() {
Jsonb jsonb = JsonbBuilder.create();
// @formatter:off
List<Person> personList = Arrays.asList(
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
// @formatter:on
String jsonArrayPerson = jsonb.toJson(personList);
// @formatter:off
String jsonExpected = "[" +
"{\"email\":\"jhon@test.com\"," +
"\"id\":1,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1000.0\"}," +
"{\"email\":\"jhon1@test.com\"," +
"\"id\":2,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1500.0\"},{\"email\":null," +
"\"id\":3,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1000.0\"}," +
"{\"email\":\"tom@test.com\"," +
"\"id\":4,\"person-name\":\"Tom\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1500.0\"}"
+ "]";
// @formatter:on
assertTrue(jsonArrayPerson.equals(jsonExpected));
}
@Test
public void givenPersonJsonArray_whenDeserializeWithJsonb_thenGetPersonList() {
Jsonb jsonb = JsonbBuilder.create();
// @formatter:off
String personJsonArray =
"[" +
"{\"email\":\"jhon@test.com\"," +
"\"id\":1,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1000.0\"}," +
"{\"email\":\"jhon1@test.com\"," +
"\"id\":2,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1500.0\"},{\"email\":null," +
"\"id\":3,\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1000.0\"}," +
"{\"email\":\"tom@test.com\"," +
"\"id\":4,\"person-name\":\"Tom\"," +
"\"registeredDate\":\"09-09-2019\"," +
"\"salary\":\"1500.0\"}"
+ "]";
// @formatter:on
@SuppressWarnings("serial")
List<Person> personList = jsonb.fromJson(personJsonArray, new ArrayList<Person>() {
}.getClass()
.getGenericSuperclass());
// @formatter:off
List<Person> personListExpected = Arrays.asList(
new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)));
// @formatter:on
assertTrue(ListUtils.isEqualList(personList, personListExpected));
}
@Test
public void givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson() {
JsonbConfig config = new JsonbConfig().withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
Jsonb jsonb = JsonbBuilder.create(config);
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person);
// @formatter:off
String jsonExpected =
"{\"email\":\"jhon@test.com\"," +
"\"id\":1," +
"\"person-name\":\"Jhon\"," +
"\"registered_date\":\"07-09-2019\"," +
"\"salary\":\"1000.0\"}";
// @formatter:on
assertTrue(jsonExpected.equals(jsonPerson));
}
@Test
public void givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson() {
JsonbConfig config = new JsonbConfig().withPropertyOrderStrategy(PropertyOrderStrategy.REVERSE);
Jsonb jsonb = JsonbBuilder.create(config);
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person);
// @formatter:off
String jsonExpected =
"{\"salary\":\"1000.0\","+
"\"registeredDate\":\"07-09-2019\"," +
"\"person-name\":\"Jhon\"," +
"\"id\":1," +
"\"email\":\"jhon@test.com\"}";
// @formatter:on
assertTrue(jsonExpected.equals(jsonPerson));
}
@Test
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
Jsonb jsonb = JsonbBuilder.create();
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person);
// @formatter:off
String jsonExpected =
"{\"email\":\"jhon@test.com\"," +
"\"id\":1," +
"\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"07-09-2019\"," +
"\"salary\":\"1000.0\"}";
// @formatter:on
assertTrue(jsonExpected.equals(jsonPerson));
}
@Test
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
Jsonb jsonb = JsonbBuilder.create();
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
// @formatter:off
String jsonPerson =
"{\"email\":\"jhon@test.com\"," +
"\"id\":1," +
"\"person-name\":\"Jhon\"," +
"\"registeredDate\":\"07-09-2019\"," +
"\"salary\":\"1000.0\"}";
// @formatter:on
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
.equals(person));
}
}

View File

@ -49,6 +49,18 @@
<artifactId>javax.json.bind-api</artifactId>
<version>${jsonb-api.version}</version>
</dependency>
<!-- Dependencies for Yasson -->
<!-- <dependency> -->
<!-- <groupId>org.eclipse</groupId> -->
<!-- <artifactId>yasson</artifactId> -->
<!-- <version>1.0</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.glassfish</groupId> -->
<!-- <artifactId>javax.json</artifactId> -->
<!-- <version>1.1.2</version> -->
<!-- </dependency> -->
<!-- Dependencies for Johnzon -->
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-json_1.1_spec</artifactId>
@ -88,6 +100,13 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>

View File

@ -1,38 +0,0 @@
package com.baeldung.jsonb;
import static org.junit.Assert.assertTrue;
import java.math.BigDecimal;
import java.time.LocalDate;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import org.junit.Before;
import org.junit.Test;
public class JsonbTest {
private Jsonb jsonb;
@Before
public void setup() {
jsonb = JsonbBuilder.create();
}
@Test
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person);
assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson));
}
@Test
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}";
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
.equals(person));
}
}