Search in sources :

Example 1 with FromOperation

use of org.springframework.sync.FromOperation in project spring-sync by spring-projects.

the class JsonPatchPatchConverter method convert.

/**
	 * Renders a {@link Patch} as a {@link JsonNode}.
	 * @param patch the patch
	 * @return a {@link JsonNode} containing JSON Patch.
	 */
public JsonNode convert(Patch patch) {
    List<PatchOperation> operations = patch.getOperations();
    JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
    ArrayNode patchNode = nodeFactory.arrayNode();
    for (PatchOperation operation : operations) {
        ObjectNode opNode = nodeFactory.objectNode();
        opNode.set("op", nodeFactory.textNode(operation.getOp()));
        opNode.set("path", nodeFactory.textNode(operation.getPath()));
        if (operation instanceof FromOperation) {
            FromOperation fromOp = (FromOperation) operation;
            opNode.set("from", nodeFactory.textNode(fromOp.getFrom()));
        }
        Object value = operation.getValue();
        if (value != null) {
            opNode.set("value", MAPPER.valueToTree(value));
        }
        patchNode.add(opNode);
    }
    return patchNode;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) FromOperation(org.springframework.sync.FromOperation) PatchOperation(org.springframework.sync.PatchOperation) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 FromOperation (org.springframework.sync.FromOperation)1 PatchOperation (org.springframework.sync.PatchOperation)1