JsonNode serverState = mapper.readValue(baos.toByteArray(), JsonNode.class); JsonNode patchAsNode = mapper.readValue(readerInterceptorContext.getInputStream(), JsonNode.class); JsonPatch patch = JsonPatch.fromJson(patchAsNode);
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();
/** * Applies a JsonPatch to an object */ @SuppressWarnings("unchecked") public static <T> T applyPatch(T originalObj, String patchString) throws JsonProcessingException, IOException, JsonPatchException { // Parse the patch to JsonNode JsonNode patchNode = objectMapper.readTree(patchString); // Create the patch JsonPatch patch = JsonPatch.fromJson(patchNode); // Convert the original object to JsonNode JsonNode originalObjNode = objectMapper.valueToTree(originalObj); // Apply the patch TreeNode patchedObjNode = patch.apply(originalObjNode); // Convert the patched node to an updated obj return objectMapper.treeToValue(patchedObjNode, (Class<T>) originalObj.getClass()); }
@Override public JsonNode transform(JsonNode patched) throws IOException { Iterator<Map.Entry<String, JsonNode>> nodeIterator = patched.get("message").fields(); while (nodeIterator.hasNext()) { Map.Entry<String, JsonNode> entry = nodeIterator.next(); if (!KNOWN_KEYS.contains(entry.getKey())) { String json = format(MOVE_OP, entry.getKey()); try { patched = JsonPatch.fromJson(JacksonUtils.getReader().readTree(json)).apply(patched); } catch (JsonPatchException e) { throw new RuntimeException("move operation could not be applied", e); } } } return patched; } }
@Override public JsonPatchEdit deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException { final ObjectCodec oc = jp.getCodec(); final JsonNode edit = oc.readTree(jp); final Builder eb = JsonPatchEdit.withChecksum(edit.get("checksum").asText()); eb.clientVersion(edit.get("clientVersion").asLong()); eb.serverVersion(edit.get("serverVersion").asLong()); eb.checksum(edit.get("checksum").asText()); final JsonNode diffsNode = edit.get("diffs"); if (!diffsNode.isNull()) { eb.patch(JsonPatch.fromJson(diffsNode)); } return eb.build(); } }
@Override public JsonPatchMessage deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException { final ObjectCodec oc = jp.getCodec(); final JsonNode node = oc.readTree(jp); final String documentId = node.get("id").asText(); final String clientId = node.get("clientId").asText(); final JsonNode jsonEdits = node.get("edits"); final Queue<JsonPatchEdit> edits = new ConcurrentLinkedQueue<JsonPatchEdit>(); if (jsonEdits.isArray()) { for (JsonNode edit : jsonEdits) { if (edit.isNull()) { continue; } final Builder eb = JsonPatchEdit.withChecksum(edit.get("checksum").asText()); eb.clientVersion(edit.get("clientVersion").asLong()); eb.serverVersion(edit.get("serverVersion").asLong()); final JsonNode diffsNode = edit.get("diffs"); if (!diffsNode.isNull()) { eb.patch(JsonPatch.fromJson(diffsNode)); } edits.add(eb.build()); } } return new JsonPatchMessage(documentId, clientId, edits); } }
@Test public void nullInputsDuringBuildAreRejected() throws IOException { try { JsonPatch.fromJson(null); fail("No exception thrown!!"); } catch (NullPointerException e) { assertEquals(e.getMessage(), BUNDLE.getMessage( "jsonPatch.nullInput")); } }
@DataProvider public Iterator<Object[]> getTests() throws IOException { final List<Object[]> list = Lists.newArrayList(); boolean valid; JsonPatch patch; JsonNode source, expected; for (final JsonNode element: testNode) { if (!element.has("patch")) continue; patch = JsonPatch.fromJson(element.get("patch")); source = element.get("doc"); expected = element.get("expected"); if (expected == null) expected = source; valid = !element.has("error"); list.add(new Object[]{source, patch, expected, valid}); } return list.iterator(); }
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();