BAEL-8853 Merge jsonb module into another module

- Moved jsonb module resources to json module and deleted jsonb module
This commit is contained in:
Dhawal Kapil
2018-09-05 10:21:18 +05:30
parent 2449866727
commit e899b76d43
9 changed files with 22 additions and 132 deletions

View File

@@ -0,0 +1,26 @@
package com.baeldung.adapter;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.bind.adapter.JsonbAdapter;
import com.baeldung.jsonb.Person;
public class PersonAdapter implements JsonbAdapter<Person, JsonObject> {
@Override
public JsonObject adaptToJson(Person p) throws Exception {
return Json.createObjectBuilder()
.add("id", p.getId())
.add("name", p.getName())
.build();
}
@Override
public Person adaptFromJson(JsonObject adapted) throws Exception {
Person person = new Person();
person.setId(adapted.getInt("id"));
person.setName(adapted.getString("name"));
return person;
}
}

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() {
this(0, "", "", 0, LocalDate.now(), new BigDecimal(0));
}
public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
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,189 @@
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;
import com.baeldung.adapter.PersonAdapter;
import com.baeldung.jsonb.Person;
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));
}
@Test
public void givenPersonObject_whenSerializeWithAdapter_thenGetPersonJson() {
JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
Jsonb jsonb = JsonbBuilder.create(config);
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));// new Person(1, "Jhon");
String jsonPerson = jsonb.toJson(person);
// @formatter:off
String jsonExpected =
"{\"id\":1," +
"\"name\":\"Jhon\"}";
// @formatter:on
assertTrue(jsonExpected.equals(jsonPerson));
}
@Test
public void givenPersonJson_whenDeserializeWithAdapter_thenGetPersonObject() {
JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
Jsonb jsonb = JsonbBuilder.create(config);
Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));// new Person(1, "Jhon");
// @formatter:off
String jsonPerson =
"{\"id\":1," +
"\"name\":\"Jhon\"}";
// @formatter:on
assertTrue(jsonb.fromJson(jsonPerson, Person.class)
.equals(person));
}
}