All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fge.jsonpatch.CopyOperation Maven / Gradle / Ivy

Go to download

JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386) implementation in Java

There is a newer version: 1.9
Show newest version
/*
 * Copyright (c) 2013, Francis Galiegue 
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * Lesser GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package com.github.fge.jsonpatch;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jackson.jsonpointer.JsonPointer;

/**
 * JSON Patch {@code copy} operation
 *
 * 

For this operation, {@code from} is the JSON Pointer of the value to copy, * and {@code path} is the destination where the value should be copied.

* *

As for {@code add}:

* *
    *
  • the value at the destination path is either created or replaced;
  • *
  • it is created only if the immediate parent exists;
  • *
  • {@code -} appends at the end of an array.
  • *
* *

It is an error if {@code from} fails to resolve to a JSON value.

*/ public final class CopyOperation extends DualPathOperation { @JsonCreator public CopyOperation(@JsonProperty("from") final JsonPointer from, @JsonProperty("path") final JsonPointer path) { super(from, path); } @Override public JsonNode apply(final JsonNode node) throws JsonPatchException { final JsonNode dupData = from.path(node).deepCopy(); if (dupData.isMissingNode()) throw new JsonPatchException(JsonPatchMessages.NO_SUCH_PATH); return new AddOperation(path, dupData).apply(node); } @Override public String toString() { return "copy: " + super.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy