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