Search in sources :

Example 1 with PersonAdapter

use of com.baeldung.adapter.PersonAdapter in project tutorials by eugenp.

the class JsonbTest method givenPersonJson_whenDeserializeWithAdapter_thenGetPersonObject.

@Test
public void givenPersonJson_whenDeserializeWithAdapter_thenGetPersonObject() {
    JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
    Jsonb jsonb = JsonbBuilder.create(config);
    // new Person(1, "Jhon");
    Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
    // @formatter:off
    String jsonPerson = "{\"id\":1," + "\"name\":\"Jhon\"}";
    // @formatter:on
    assertTrue(jsonb.fromJson(jsonPerson, Person.class).equals(person));
}
Also used : JsonbConfig(javax.json.bind.JsonbConfig) Jsonb(javax.json.bind.Jsonb) PersonAdapter(com.baeldung.adapter.PersonAdapter) Test(org.junit.Test)

Example 2 with PersonAdapter

use of com.baeldung.adapter.PersonAdapter in project tutorials by eugenp.

the class JsonbTest method givenPersonObject_whenSerializeWithAdapter_thenGetPersonJson.

@Test
public void givenPersonObject_whenSerializeWithAdapter_thenGetPersonJson() {
    JsonbConfig config = new JsonbConfig().withAdapters(new PersonAdapter());
    Jsonb jsonb = JsonbBuilder.create(config);
    // new Person(1, "Jhon");
    Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
    String jsonPerson = jsonb.toJson(person);
    // @formatter:off
    String jsonExpected = "{\"id\":1," + "\"name\":\"Jhon\"}";
    // @formatter:on
    assertTrue(jsonExpected.equals(jsonPerson));
}
Also used : JsonbConfig(javax.json.bind.JsonbConfig) Jsonb(javax.json.bind.Jsonb) PersonAdapter(com.baeldung.adapter.PersonAdapter) Test(org.junit.Test)

Aggregations

PersonAdapter (com.baeldung.adapter.PersonAdapter)2 Jsonb (javax.json.bind.Jsonb)2 JsonbConfig (javax.json.bind.JsonbConfig)2 Test (org.junit.Test)2