Search in sources :

Example 1 with JsonMergePatch

use of javax.json.JsonMergePatch in project jackson-datatype-jsr353 by FasterXML.

the class JsonMergePatchDeserializationTest method testDeserializationAndPatching.

public void testDeserializationAndPatching() throws Exception {
    final String json = "{" + "\"name\":\"Json\"" + "}";
    final JsonMergePatch jsonMergePatch = MAPPER.readValue(json, JsonMergePatch.class);
    final JsonValue jsonPatchAsJsonValue = jsonMergePatch.toJsonValue();
    assertThat(jsonPatchAsJsonValue, instanceOf(JsonObject.class));
    final JsonObject jsonPatchAsJsonObject = jsonPatchAsJsonValue.asJsonObject();
    assertTrue(jsonPatchAsJsonObject.containsKey("name"));
    assertThat(jsonPatchAsJsonObject.get("name"), instanceOf(JsonString.class));
    assertThat(jsonPatchAsJsonObject.getString("name"), is("Json"));
    assertThat(serializeAsString(jsonPatchAsJsonValue), is(json));
    final Person person = new Person("John", "Smith");
    final JsonValue personJson = MAPPER.convertValue(person, JsonValue.class);
    final JsonValue patchedPersonJson = jsonMergePatch.apply(personJson);
    final Person patchedPerson = MAPPER.convertValue(patchedPersonJson, Person.class);
    assertThat(patchedPerson, is(new Person("Json", "Smith")));
}
Also used : JsonValue(javax.json.JsonValue) JsonMergePatch(javax.json.JsonMergePatch) JsonObject(javax.json.JsonObject) JsonString(javax.json.JsonString) JsonString(javax.json.JsonString)

Aggregations

JsonMergePatch (javax.json.JsonMergePatch)1 JsonObject (javax.json.JsonObject)1 JsonString (javax.json.JsonString)1 JsonValue (javax.json.JsonValue)1