@Override JsonPatchOperation toOperation(final DiffOperation op) { return new RemoveOperation(op.from); } },
@Override JsonPatchOperation toOperation(final DiffOperation op) { return new AddOperation(op.path, op.value); } },
@Test public void whenOneOperationFailsNextOperationIsNotCalled() throws JsonPatchException { final String message = "foo"; when(op1.apply(any(JsonNode.class))) .thenThrow(new JsonPatchException(message)); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); try { patch.apply(FACTORY.nullNode()); fail("No exception thrown!!"); } catch (JsonPatchException e) { assertEquals(e.getMessage(), message); } verifyZeroInteractions(op2); } }
private static JsonNode patch(final JsonPatchEdit edit, final JsonNode target) { try { return edit.diff().jsonPatch().apply(target); } catch (final JsonPatchException e) { throw new RuntimeException(e.getMessage(), e); } }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (from.equals(path)) return node.deepCopy(); final JsonNode movedNode = from.path(node); if (movedNode.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonPatchOperation remove = new RemoveOperation(from); final JsonPatchOperation add = new AddOperation(path, movedNode); return add.apply(remove.apply(node)); } }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode dupData = from.path(node).deepCopy(); if (dupData.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); return new AddOperation(path, dupData).apply(node); } }
@Test(dataProvider = "getErrors") public final void errorsAreCorrectlyReported(final JsonNode patch, final JsonNode node, final String message) throws IOException { final JsonPatchOperation op = reader.readValue(patch); try { op.apply(node); fail("No exception thrown!!"); } catch (JsonPatchException e) { assertEquals(e.getMessage(), message); } }
@Test public void removingRootReturnsMissingNode() throws JsonPatchException { final JsonNode node = JacksonUtils.nodeFactory().nullNode(); final JsonPatchOperation op = new RemoveOperation(JsonPointer.empty()); final JsonNode ret = op.apply(node); assertTrue(ret.isMissingNode()); } }
/** * Apply this patch to a JSON value * * @param node the value to apply the patch to * @return the patched JSON value * @throws JsonPatchException failed to apply patch * @throws NullPointerException input is null */ public JsonNode apply(final JsonNode node) throws JsonPatchException { BUNDLE.checkNotNull(node, "jsonPatch.nullInput"); JsonNode ret = node; for (final JsonPatchOperation operation: operations) ret = operation.apply(ret); return ret; }
@Override JsonPatchOperation toOperation(final DiffOperation op) { return new CopyOperation(op.from, op.path); } },
@Override JsonPatchOperation toOperation(final DiffOperation op) { return new MoveOperation(op.from, op.path); } },
@Override public final void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, final TypeSerializer typeSer) throws IOException, JsonProcessingException { serialize(jgen, provider); }
@Override public void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, final TypeSerializer typeSer) throws IOException, JsonProcessingException { serialize(jgen, provider); }
@Override JsonPatchOperation toOperation(final DiffOperation op) { return new ReplaceOperation(op.from, op.value); } },
@Override public void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, final TypeSerializer typeSer) throws IOException { serialize(jgen, provider); } }
@Override public final void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, final TypeSerializer typeSer) throws IOException, JsonProcessingException { serialize(jgen, provider); }
@Override public void serialize(final JsonGenerator jgen, final SerializerProvider provider) throws IOException { jgen.writeStartArray(); for (final JsonPatchOperation op: operations) op.serialize(jgen, provider); jgen.writeEndArray(); }
ObjectMapper mapper = new ObjectMapper(); JsonNode targetJson = mapper.readValue(tmpOutputStream.toByteArray(), JsonNode.class); JsonPatch patch = JsonPatch.fromJson(mapper.readValue(request.getInputStream(), JsonNode.class)); JsonNode result = patch.apply(targetJson); ByteArrayOutputStream targetOutputStream = new ByteArrayOutputStream(); mapper.writeValue(targetOutputStream, result);