Java 类javax.json.JsonPatch 实例源码

项目:johnzon    文件:JsonPatchDiffTest.java   
@Test
public void testDiffReplaceObject() {

    JsonObject source = Json.createObjectBuilder()
                            .add("a", "value")
                            .build();

    JsonObject target = Json.createObjectBuilder()
                            .add("a", "replaced")
                            .build();

    JsonPatch patch = Json.createDiff(source, target);
    assertNotNull(patch);

    JsonArray operations = patch.toJsonArray();
    assertEquals(1, operations.size());

    containsOperation(operations, JsonPatch.Operation.REPLACE, "/a", Json.createValue("replaced"));
}
项目:ee8-sandbox    文件:JsonpTest.java   
@Test
public void testJsonPatch() {
    JsonReader reader = Json.createReader(JsonpTest.class.getResourceAsStream("/persons.json"));
    JsonArray jsonaArray = reader.readArray();

    JsonPatch patch = Json.createPatchBuilder()        
            .replace("/0/name", "Duke Oracle")
            .remove("/1")
            .build();

    JsonArray result = patch.apply(jsonaArray);
    System.out.println(result.toString());

    Type type = new ArrayList<Person>() {}.getClass().getGenericSuperclass();

    List<Person> person = JsonbBuilder.create().fromJson(result.toString(), type);
    assertEquals("Duke Oracle", person.get(0).getName());

}
项目:johnzon    文件:JsonPatchBuilderImpl.java   
JsonPatchBuilderImpl(JsonArray initialData) {
    operations = new ArrayList<>(initialData.size());

    for (JsonValue value : initialData) {

        JsonObject operation = (JsonObject) value;

        JsonPatch.Operation op = JsonPatch.Operation.fromOperationName(operation.getString("op"));
        String path = operation.getString("path");
        String from = operation.getString("from", null);
        JsonValue jsonValue = operation.get("value");

        operations.add(new JsonPatchImpl.PatchValue(op,
                                                    path,
                                                    from,
                                                    jsonValue));
    }
}
项目:johnzon    文件:JsonPatchImpl.java   
PatchValue(JsonPatch.Operation operation,
           String path,
           String from,
           JsonValue value) {
    this.operation = operation;
    this.path = new JsonPointerImpl(path);

    // ignore from if we do not need it
    if (operation == JsonPatch.Operation.MOVE || operation == JsonPatch.Operation.COPY) {
        this.from = new JsonPointerImpl(from);
    } else {
        this.from = null;
    }

    this.value = value;
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderAddJsonObject() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
            "/foo",
            null,
            Json.createObjectBuilder()
                    .add("bar", "qux")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().add("/foo", Json.createObjectBuilder()
            .add("bar", "qux")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderAddJsonArray() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
            "/path",
            null,
            Json.createArrayBuilder()
                    .add("test")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().add("/path", Json.createArrayBuilder()
            .add("test")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderAddBoolean() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
            "/path/true",
            null,
            JsonValue.TRUE),
            new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                    "/path/false",
                    null,
                    JsonValue.FALSE));

    JsonPatch patch = Json.createPatchBuilder().add("/path/true", true)
            .add("/path/false", false)
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderReplaceBoolean() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
            "/true/to/replace",
            null,
            JsonValue.FALSE),
            new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
                    "/false/to/replace",
                    null,
                    JsonValue.TRUE));

    JsonPatch patch = Json.createPatchBuilder().replace("/true/to/replace", false)
            .replace("/false/to/replace", true)
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderReplaceJsonObject() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
            "/replace/the/object",
            null,
            Json.createObjectBuilder()
                    .add("foo", "bar")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().replace("/replace/the/object", Json.createObjectBuilder()
            .add("foo", "bar")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderReplaceJsonArray() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
            "/replace/my/array",
            null,
            Json.createArrayBuilder()
                    .add("test")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().replace("/replace/my/array", Json.createArrayBuilder()
            .add("test")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderTestBoolean() {

    JsonPatchImpl exptected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
            "/true/to/test",
            null,
            JsonValue.TRUE),
            new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                    "/false/to/test",
                    null,
                    JsonValue.FALSE));

    JsonPatch patch = Json.createPatchBuilder().test("/true/to/test", true)
            .test("/false/to/test", false)
            .build();
    assertNotNull(patch);
    assertEquals(exptected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderTestJsonObject() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
            "/test/the/object",
            null,
            Json.createObjectBuilder()
                    .add("foo", "bar")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().test("/test/the/object", Json.createObjectBuilder()
            .add("foo", "bar")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderTestJsonArray() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
            "/test/my/array",
            null,
            Json.createArrayBuilder()
                    .add("element")
                    .build()));

    JsonPatch patch = Json.createPatchBuilder().test("/test/my/array", Json.createArrayBuilder()
            .add("element")
            .build())
            .build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderWithinitialData() {

    JsonPatchImpl expected = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
            "/add/an/object",
            null,
            Json.createObjectBuilder()
                    .add("name", "Cassius")
                    .build()),
            new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
                    "/replace/me",
                    null,
                    Json.createArrayBuilder()
                            .add(16)
                            .add(27)
                            .add("test")
                            .build()),
            new JsonPatchImpl.PatchValue(JsonPatch.Operation.REMOVE,
                    "/remove/it",
                    null,
                    null));

    JsonPatch patch = new JsonPatchBuilderImpl(expected.toJsonArray()).build();
    assertNotNull(patch);
    assertEquals(expected, patch);
}
项目:johnzon    文件:JsonPatchBuilderTest.java   
@Test
public void testPatchBuilderWithJsonArrayInitialData() {
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    arrayBuilder.add(Json.createObjectBuilder()
                         .add("op", JsonPatch.Operation.ADD.operationName())
                         .add("path", "/add/an/object")
                         .add("value", "someValue").build());
    arrayBuilder.add(Json.createObjectBuilder()
                         .add("op", JsonPatch.Operation.TEST.operationName())
                         .add("path", "/test/someObject")
                         .add("value", "someValue").build());

    JsonArray initialPatchData = arrayBuilder.build();

    JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder(initialPatchData);
    jsonPatchBuilder.move("/move/me/to", "/move/me/from");
    JsonPatch jsonPatch = jsonPatchBuilder.build();
    Assert.assertNotNull(jsonPatch);

    //X TODO apply the patch to some data
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testAddObjectMember() {

    JsonObject object = Json.createReader(new StringReader("{ \"foo\": \"bar\" }"))
                            .readObject();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                                                                         "/baz",
                                                                         null, // no from
                                                                         new JsonStringImpl("qux")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertEquals("bar", patched.getString("foo"));
    assertEquals("qux", patched.getString("baz"));

    assertEquals("{\"foo\":\"bar\",\"baz\":\"qux\"}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testAddArrayElementWithIndex() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", Json.createArrayBuilder()
                                            .add("bar")
                                            .add("baz"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                                                                         "/foo/1",
                                                                         null, // no from
                                                                         new JsonStringImpl("qux")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);

    JsonArray array = patched.getJsonArray("foo");
    assertNotNull(array);
    assertEquals("bar", array.getString(0));
    assertEquals("qux", array.getString(1));
    assertEquals("baz", array.getString(2));

    assertEquals("{\"foo\":[\"bar\",\"qux\",\"baz\"]}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testAddArrayElementAppend() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", Json.createArrayBuilder()
                                            .add("bar")
                                            .add("baz"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                                                                         "/foo/-",
                                                                         null, // no from
                                                                         new JsonStringImpl("qux")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);

    JsonArray array = patched.getJsonArray("foo");
    assertNotNull(array);
    assertEquals("bar", array.getString(0));
    assertEquals("baz", array.getString(1));
    assertEquals("qux", array.getString(2));

    assertEquals("{\"foo\":[\"bar\",\"baz\",\"qux\"]}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testAddArrayElementPlainArray() {
    JsonArray array = Json.createArrayBuilder()
                          .add("bar")
                          .add("baz")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                                                                         "/-",
                                                                         null, // no from
                                                                         new JsonStringImpl("qux")));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertNotSame(array, patched);
    assertEquals("bar", patched.getString(0));
    assertEquals("baz", patched.getString(1));
    assertEquals("qux", patched.getString(2));

    assertEquals("[\"bar\",\"baz\",\"qux\"]", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testRemoveObjectMember() {

    JsonObject object = Json.createObjectBuilder()
                            .add("baz", "qux")
                            .add("foo", "bar")
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REMOVE,
                                                                         "/baz",
                                                                         null,
                                                                         null));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertEquals("bar", patched.getString("foo"));
    assertFalse("patched JsonObject must no contain \"baz\"", patched.containsKey("baz"));

    assertEquals("{\"foo\":\"bar\"}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testRemoveArrayElement() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", Json.createArrayBuilder()
                                            .add("bar")
                                            .add("qux")
                                            .add("baz"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REMOVE,
                                                                         "/foo/1",
                                                                         null,
                                                                         null));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);

    JsonArray array = patched.getJsonArray("foo");
    assertNotNull(array);
    assertEquals(2, array.size());
    assertEquals("bar", array.getString(0));
    assertEquals("baz", array.getString(1));

    assertEquals("{\"foo\":[\"bar\",\"baz\"]}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testRemoveArrayElementPlainArray() {

    JsonArray array = Json.createArrayBuilder()
                          .add("bar")
                          .add("qux")
                          .add("baz")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REMOVE,
                                                                         "/1",
                                                                         null,
                                                                         null));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertEquals(2, patched.size());
    assertEquals("bar", patched.getString(0));
    assertEquals("baz", patched.getString(1));

    assertEquals("[\"bar\",\"baz\"]", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testReplacingObjectMember() {

    JsonObject object = Json.createObjectBuilder()
                            .add("baz", "qux")
                            .add("foo", "bar")
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
                                                                         "/baz",
                                                                         null,
                                                                         new JsonStringImpl("boo")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertNotSame(object, patched);
    assertEquals("boo", patched.getString("baz"));
    assertEquals("bar", patched.getString("foo"));

    assertEquals("{\"foo\":\"bar\",\"baz\":\"boo\"}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testReplacingArrayElementPlainArray() {

    JsonArray array = Json.createArrayBuilder()
                          .add("bar")
                          .add("qux")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.REPLACE,
                                                                         "/0",
                                                                         null,
                                                                         new JsonStringImpl("boo")));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertNotSame(array, patched);
    assertEquals(2, patched.size());
    assertEquals("boo", patched.getString(0));
    assertEquals("qux", patched.getString(1));

    assertEquals("[\"boo\",\"qux\"]", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testMovingArrayElementPlainArray() {

    JsonArray array = Json.createArrayBuilder()
                          .add("two")
                          .add("three")
                          .add("four")
                          .add("one")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.MOVE,
                                                                         "/0",
                                                                         "/3",
                                                                         null));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertNotSame(array, patched);
    assertEquals("one", patched.getString(0));
    assertEquals("two", patched.getString(1));
    assertEquals("three", patched.getString(2));
    assertEquals("four", patched.getString(3));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testCopyObjectMember() {

    JsonObject object = Json.createObjectBuilder()
                           .add("foo", "bar")
                           .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.COPY,
                                                                         "/baz",
                                                                         "/foo",
                                                                         null));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertEquals(2, patched.size());
    assertEquals("bar", patched.getString("foo"));
    assertEquals("bar", patched.getString("baz"));

    assertEquals("{\"foo\":\"bar\",\"baz\":\"bar\"}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testCopyArrayMember() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", Json.createArrayBuilder()
                                            .add("bar")
                                            .add("baz"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.COPY,
                                                                             "/foo/-",
                                                                             "/foo/0",
                                                                             null));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);

    JsonArray array = patched.getJsonArray("foo");
    assertEquals(3, array.size());
    assertEquals("bar", array.getString(0));
    assertEquals("baz", array.getString(1));
    assertEquals("bar", array.getString(2));

    assertEquals("{\"foo\":[\"bar\",\"baz\",\"bar\"]}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testCopyArrayMemberPlainArray() {

    JsonArray array = Json.createArrayBuilder()
                          .add("foo")
                          .add("bar")
                          .build();


    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.COPY,
                                                                         "/0",
                                                                         "/1",
                                                                         null));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertNotSame(array, patched);
    assertEquals(3, patched.size());
    assertEquals("bar", patched.getString(0));
    assertEquals("foo", patched.getString(1));
    assertEquals("bar", patched.getString(2));

    assertEquals("[\"bar\",\"foo\",\"bar\"]", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testTestingObjectMemberValueSuccess() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", "qux")
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/foo",
                                                                         null,
                                                                         new JsonStringImpl("qux")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertSame(object, patched);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testTestingArrayAsObjectMemberSuccess() {

    JsonObject object = Json.createObjectBuilder()
                            .add("name", "Thor")
                            .add("parents", Json.createArrayBuilder()
                                                .add("Odin")
                                                .add("Forjgyn"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/parents",
                                                                         null,
                                                                         Json.createArrayBuilder() // yessss, we really want to create a new JsonArray ;)
                                                                             .add("Odin")
                                                                             .add("Forjgyn")
                                                                             .build()));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertSame(object, patched);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test(expected = JsonException.class)
public void testTestingArrayAsObjectMemberFailed() {

    JsonObject object = Json.createObjectBuilder()
                            .add("magic", "array")
                            .add("numbers", Json.createArrayBuilder()
                                                .add(1)
                                                .add(2))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/numbers",
                                                                         null,
                                                                         Json.createArrayBuilder() // different ordering
                                                                             .add(2)
                                                                             .add(1)
                                                                             .build()));

    patch.apply(object);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testTestingArrayElementSuccess() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", Json.createArrayBuilder()
                                            .add("bar")
                                            .add("baz"))
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/foo/1",
                                                                         null,
                                                                         new JsonStringImpl("baz")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertSame(object, patched);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testTestingArrayElementPlainArraySuccess() {

    JsonArray array = Json.createArrayBuilder()
                          .add("foo")
                          .add("bar")
                          .add("qux")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/2",
                                                                         null,
                                                                         new JsonStringImpl("qux")));

    JsonArray patched = patch.apply(array);
    assertNotNull(patched);
    assertSame(array, patched);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test(expected = JsonException.class)
public void testTestingArrayElementPlainArrayFailed() {

    JsonArray array = Json.createArrayBuilder()
                          .add(1)
                          .add("2")
                          .add("qux")
                          .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.TEST,
                                                                         "/0",
                                                                         null,
                                                                         new JsonStringImpl("bar")));

    patch.apply(array);
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testAddObjectMemberAlreadyExists() {

    JsonObject object = Json.createObjectBuilder()
                            .add("foo", "bar")
                            .add("baz", "qux")
                            .build();

    JsonPatchImpl patch = new JsonPatchImpl(new JsonPatchImpl.PatchValue(JsonPatch.Operation.ADD,
                                                                         "/foo",
                                                                         null,
                                                                         new JsonStringImpl("abcd")));

    JsonObject patched = patch.apply(object);
    assertNotNull(patched);
    assertNotSame(object, patched);
    assertEquals("abcd", patched.getString("foo"));
    assertEquals("qux", patched.getString("baz"));

    assertEquals("{\"foo\":\"abcd\",\"baz\":\"qux\"}", toJsonString(patched));
}
项目:johnzon    文件:JsonPatchTest.java   
@Test
public void testCreatePatch() {
    JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
    arrayBuilder.add(Json.createObjectBuilder()
            .add("op", JsonPatch.Operation.ADD.operationName())
            .add("path", "/add")
            .add("value", "someValue").build());
    arrayBuilder.add(Json.createObjectBuilder()
            .add("op", JsonPatch.Operation.TEST.operationName())
            .add("path", "/test/someObject")
            .add("value", "someValue").build());

    JsonArray initialPatchData = arrayBuilder.build();

    JsonPatch patch = Json.createPatch(initialPatchData);

    String jsonToPatch = "{\"add\":{\"a\":\"b\"},\"test\":{\"someObject\":\"someValue\"}}";
    JsonObject jsonObjectToPatch = Json.createReader(new StringReader(jsonToPatch)).readObject();

    JsonObject patchedJsonObject = patch.apply(jsonObjectToPatch);
    Assert.assertNotNull(patchedJsonObject);
}
项目:johnzon    文件:JsonPatchDiffTest.java   
@Test
public void testAddDiff() {
    // {"a":"xa"}
    String jsonA = "{\"a\":\"xa\"}";

    // {"a":"xa","b":"xb"}
    String jsonB = "{\"a\":\"xa\",\"b\":\"xb\"}";

    // this results in 1 diff operations:
    // adding "b"
    JsonPatch jsonPatch = Json.createDiff(Json.createReader(new StringReader(jsonA)).readObject(),
                                          Json.createReader(new StringReader(jsonB)).readObject());
    assertNotNull(jsonPatch);
    JsonArray patchOperations = jsonPatch.toJsonArray();
    assertNotNull(patchOperations);
    assertEquals(1, patchOperations.size());
    containsOperation(patchOperations, JsonPatch.Operation.ADD, "/b", Json.createValue("xb"));
}
项目:johnzon    文件:JsonPatchDiffTest.java   
@Test
public void testAddDiffNewObject() {

    JsonObject target = Json.createObjectBuilder()
                            .add("a", Json.createObjectBuilder()
                                          .add("aa", "value")
                                          .add("ab", "another"))
                            .build();

    JsonPatch patch = Json.createDiff(JsonValue.EMPTY_JSON_OBJECT, target);
    assertNotNull(patch);

    JsonArray operations = patch.toJsonArray();
    assertEquals(1, operations.size());

    containsOperation(operations, JsonPatch.Operation.ADD, "/a", target.get("a"));

    // now try to apply that patch.
    JsonObject patched = patch.apply(JsonValue.EMPTY_JSON_OBJECT);
    Assert.assertEquals(target, patched);
}
项目:johnzon    文件:JsonPatchDiffTest.java   
@Test
public void testAddDiffNewObjectWithEscaping() {

    JsonObject target = Json.createObjectBuilder()
                            .add("a~/", Json.createObjectBuilder()
                                            .add("esc/aped", "value")
                                            .add("tilde", "another"))
                            .build();

    JsonPatch patch = Json.createDiff(JsonValue.EMPTY_JSON_OBJECT, target);
    assertNotNull(patch);

    JsonArray operations = patch.toJsonArray();
    assertEquals(1, operations.size());

    containsOperation(operations, JsonPatch.Operation.ADD, "/a~0~1", target.get("a"));

    // now try to apply that patch.
    JsonObject patched = patch.apply(JsonValue.EMPTY_JSON_OBJECT);
    Assert.assertEquals(target, patched);
}
项目:johnzon    文件:JsonPatchDiffTest.java   
@Test
public void testAddDiffInNestedObject() {

    JsonObject source = Json.createObjectBuilder()
                            .add("a", Json.createObjectBuilder()
                                          .add("aa", "value"))
                            .build();

    JsonObject target = Json.createObjectBuilder()
                            .add("a", Json.createObjectBuilder()
                                          .add("aa", "value")
                                          .add("bb", "another value"))
                            .build();

    JsonPatch patch = Json.createDiff(source, target);
    assertNotNull(patch);

    JsonArray operations = patch.toJsonArray();
    assertEquals(1, operations.size());

    containsOperation(operations, JsonPatch.Operation.ADD, "/a/bb", Json.createValue("another value"));
}