Merge pull request #3102 from earth001/master

Add Adapter sample & update Yasson version - earth001_BAEL-1121
This commit is contained in:
Carsten Gräf 2017-11-23 13:44:42 +01:00 committed by GitHub
commit 16814c19bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 59 additions and 3 deletions

View File

@ -98,9 +98,9 @@
<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>
<yasson.version>1.0.1</yasson.version>
<javax.json.version>1.1.2</javax.json.version>
<commons-collections4.version>4.1</commons-collections4.version>
</properties>
</project>
</project>

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

@ -22,10 +22,10 @@ public class Person {
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) {
super();
this.id = id;
this.name = name;
this.email = email;

View File

@ -17,6 +17,8 @@ import javax.json.bind.config.PropertyOrderStrategy;
import org.apache.commons.collections4.ListUtils;
import org.junit.Test;
import com.baeldung.adapter.PersonAdapter;
public class JsonbTest {
@Test
@ -155,4 +157,32 @@ public class JsonbTest {
.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));
}
}