Search in sources :

Example 1 with JsonbConfig

use of javax.json.bind.JsonbConfig in project tutorials by eugenp.

the class JsonbTest method givenPersonObject_whenNamingStrategy_thenGetCustomPersonJson.

@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));
}
Also used : JsonbConfig(javax.json.bind.JsonbConfig) Jsonb(javax.json.bind.Jsonb) Test(org.junit.Test)

Example 2 with JsonbConfig

use of javax.json.bind.JsonbConfig in project component-runtime by Talend.

the class UiSpecServiceTest method out.

/*
     * just to log the output
     */
@Test
@Disabled("debug test to log the produced model")
void out() throws Exception {
    final Ui payload = service.convert(load("jdbc.json")).toCompletableFuture().get();
    System.out.println(JsonbBuilder.create(new JsonbConfig().withFormatting(true)).toJson(payload));
}
Also used : JsonbConfig(javax.json.bind.JsonbConfig) Ui(org.talend.sdk.component.form.model.Ui) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 3 with JsonbConfig

use of javax.json.bind.JsonbConfig in project tutorials by eugenp.

the class JsonbTest method givenPersonObject_whenWithPropertyOrderStrategy_thenGetReversePersonJson.

@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));
}
Also used : JsonbConfig(javax.json.bind.JsonbConfig) Jsonb(javax.json.bind.Jsonb) Test(org.junit.Test)

Example 4 with JsonbConfig

use of javax.json.bind.JsonbConfig 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 5 with JsonbConfig

use of javax.json.bind.JsonbConfig 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

JsonbConfig (javax.json.bind.JsonbConfig)10 Jsonb (javax.json.bind.Jsonb)8 Test (org.junit.Test)4 File (java.io.File)3 PersonAdapter (com.baeldung.adapter.PersonAdapter)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 Collections.singletonList (java.util.Collections.singletonList)2 Collectors.toList (java.util.stream.Collectors.toList)2 Stream (java.util.stream.Stream)2 JsonbBuilder (javax.json.bind.JsonbBuilder)2 JsonProvider (javax.json.spi.JsonProvider)2 ComponentManager (org.talend.sdk.component.runtime.manager.ComponentManager)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FilterOutputStream (java.io.FilterOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1