Example usage for org.springframework.data.rest.webmvc.json.patch AddOperation AddOperation

List of usage examples for org.springframework.data.rest.webmvc.json.patch AddOperation AddOperation

Introduction

In this page you can find the example usage for org.springframework.data.rest.webmvc.json.patch AddOperation AddOperation.

Prototype

private AddOperation(UntypedSpelPath path, Object value) 

Source Link

Document

Constructs the add operation

Usage

From source file:org.springframework.data.rest.webmvc.json.patch.JsonPatchPatchConverter.java

/**
 * Constructs a {@link Patch} object given a JsonNode.
 * //from   w w  w.  ja v  a2s .  c  om
 * @param jsonNode a JsonNode containing the JSON Patch
 * @return a {@link Patch}
 */
public Patch convert(JsonNode jsonNode) {
    if (!(jsonNode instanceof ArrayNode)) {
        throw new IllegalArgumentException("JsonNode must be an instance of ArrayNode");
    }

    ArrayNode opNodes = (ArrayNode) jsonNode;
    List<PatchOperation> ops = new ArrayList<PatchOperation>(opNodes.size());
    for (Iterator<JsonNode> elements = opNodes.elements(); elements.hasNext();) {
        JsonNode opNode = elements.next();

        String opType = opNode.get("op").textValue();
        String path = opNode.get("path").textValue();

        JsonNode valueNode = opNode.get("value");
        Object value = valueFromJsonNode(path, valueNode);
        String from = opNode.has("from") ? opNode.get("from").textValue() : null;

        if (opType.equals("test")) {
            ops.add(new TestOperation(path, value));
        } else if (opType.equals("replace")) {
            ops.add(new ReplaceOperation(path, value));
        } else if (opType.equals("remove")) {
            ops.add(new RemoveOperation(path));
        } else if (opType.equals("add")) {
            ops.add(new AddOperation(path, value));
        } else if (opType.equals("copy")) {
            ops.add(new CopyOperation(path, from));
        } else if (opType.equals("move")) {
            ops.add(new MoveOperation(path, from));
        } else {
            throw new PatchException("Unrecognized operation type: " + opType);
        }
    }

    return new Patch(ops);
}