Updating JsonObjects with JSON-P 1.1 and Java EE 8

Java EE 8 with JSON-P 1.1 ("JavaTM API for JSON Processing", JSR-374) introduced an overloaded Json#createObjectBuilder(JsonObject input) method which uses the JsonObject to set the initial state of the builder.

The pre-initialialization significantly simplifies updates:


import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
//...
            
@Test
public void jsonObjectUpdate() {
    String key = "update";
    String JAVA_EE_8 = "is easy with Java EE 8";

    JsonObject initial = Json.createObjectBuilder().
            add(key, "was hard in Java EE 7").
            build();

    JsonObject updated = Json.createObjectBuilder(initial).
            add(key, JAVA_EE_8).
            build();

    assertThat(updated.getString(key), is(JAVA_EE_8));
    assertThat(updated.size(), is(1));
}    

See you at Java EE 8 on Java 9 workshop, at Munich Airport, Terminal 2


NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Speaking of JSON-P - do you know of any plans to integrate an object mapper into JSON-P, Adam? Right now only parsing and 'manual' assembly of JSON objects is supported.
It'd be great to see something like Jackson's ObjectMapper which maps JSON-P's JsonObject to a POJO or vice versa.

Posted by Uwe Pachler on November 01, 2017 at 09:53 AM CET #

Awesome!!!

Posted by Taysay on November 06, 2017 at 05:38 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License