/** * Build an instance from a JSON input * * @param node the input * @return a JSON Merge Patch instance * @throws JsonPatchException failed to deserialize * @throws NullPointerException node is null */ public static JsonMergePatch fromJson(final JsonNode node) throws JsonPatchException { BUNDLE.checkNotNull(node, "jsonPatch.nullInput"); try { return MAPPER.readValue(node.traverse(), JsonMergePatch.class); } catch (IOException e) { throw new JsonPatchException( BUNDLE.getMessage("jsonPatch.deserFailed"), e); } }
@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); } }
@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); } }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode tested = path.path(node); if (tested.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); if (!EQUIVALENCE.equivalent(tested, value)) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.valueTestFailure")); return node.deepCopy(); } }
private JsonNode addToArray(final JsonPointer path, final JsonNode node) throws JsonPatchException { final JsonNode ret = node.deepCopy(); final ArrayNode target = (ArrayNode) path.parent().get(ret); final TokenResolver<JsonNode> token = Iterables.getLast(path); if (token.getToken().equals(LAST_ARRAY_ELEMENT)) { target.add(value); return ret; } final int size = target.size(); final int index; try { index = Integer.parseInt(token.toString()); } catch (NumberFormatException ignored) { throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.notAnIndex")); } if (index < 0 || index > size) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchIndex")); target.insert(index, value); return ret; }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (path.isEmpty()) return value; /* * Check the parent node: it must exist and be a container (ie an array * or an object) for the add operation to work. */ final JsonNode parentNode = path.parent().path(node); if (parentNode.isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchParent")); if (!parentNode.isContainerNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.parentNotContainer")); return parentNode.isArray() ? addToArray(path, node) : addToObject(path, 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); } }
@Override public JsonNode apply(final JsonNode node) throws JsonPatchException { if (path.isEmpty()) return MissingNode.getInstance(); if (path.path(node).isMissingNode()) throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonNode ret = node.deepCopy(); final JsonNode parentNode = path.parent().get(ret); final String raw = Iterables.getLast(path).getToken().getRaw(); if (parentNode.isObject()) ((ObjectNode) parentNode).remove(raw); else ((ArrayNode) parentNode).remove(Integer.parseInt(raw)); return ret; }
throw new JsonPatchException(BUNDLE.getMessage( "jsonPatch.noSuchPath")); final JsonNode replacement = value.deepCopy();
@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)); } }