|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Francis Galiegue ([email protected]) |
| 3 | + * Copyright (c) 2016, Jessica Beller ([email protected]) |
| 4 | + * |
| 5 | + * This software is dual-licensed under: |
| 6 | + * |
| 7 | + * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any |
| 8 | + * later version; |
| 9 | + * - the Apache Software License (ASL) version 2.0. |
| 10 | + * |
| 11 | + * The text of this file and of both licenses is available at the root of this |
| 12 | + * project or, if you have the jar distribution, in directory META-INF/, under |
| 13 | + * the names LGPL-3.0.txt and ASL-2.0.txt respectively. |
| 14 | + * |
| 15 | + * Direct link to the sources: |
| 16 | + * |
| 17 | + * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt |
| 18 | + * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt |
| 19 | + */ |
| 20 | + |
| 21 | +package com.github.fge.jsonpatch.operation; |
| 22 | + |
| 23 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 24 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 25 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 26 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 27 | +import com.fasterxml.jackson.databind.JsonNode; |
| 28 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 29 | +import com.fasterxml.jackson.databind.jsontype.TypeSerializer; |
| 30 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 31 | +import com.fasterxml.jackson.databind.node.MissingNode; |
| 32 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 33 | +import com.github.fge.jackson.jsonpointer.JsonPointer; |
| 34 | +import com.github.fge.jsonpatch.JsonPatchException; |
| 35 | +import com.github.fge.jsonpatch.JsonPatchMessages; |
| 36 | +import com.github.fge.jsonpatch.operation.JsonPatchOperation; |
| 37 | +import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy; |
| 38 | +import com.github.fge.msgsimple.bundle.MessageBundle; |
| 39 | +import com.github.fge.msgsimple.load.MessageBundles; |
| 40 | +import com.google.common.collect.Iterables; |
| 41 | + |
| 42 | +import java.io.IOException; |
| 43 | + |
| 44 | +/** |
| 45 | + * RemoveOperationBase implements the basic concept of removing the requested path. |
| 46 | + */ |
| 47 | +public abstract class RemoveOperationBase |
| 48 | + implements JsonPatchOperation |
| 49 | +{ |
| 50 | + protected static final MessageBundle BUNDLE |
| 51 | + = MessageBundles.getBundle(JsonPatchMessages.class); |
| 52 | + |
| 53 | + private PathMissingPolicy pathMissingPolicy; |
| 54 | + |
| 55 | + protected final String op; |
| 56 | + |
| 57 | + protected final JsonPointer path; |
| 58 | + |
| 59 | + @JsonCreator |
| 60 | + public RemoveOperationBase(final String op, |
| 61 | + @JsonProperty("path") final JsonPointer path, |
| 62 | + final PathMissingPolicy pathMissingPolicy) |
| 63 | + { |
| 64 | + this.op = op; |
| 65 | + this.path = path; |
| 66 | + this.pathMissingPolicy = pathMissingPolicy; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public JsonNode apply(final JsonNode node) |
| 71 | + throws JsonPatchException |
| 72 | + { |
| 73 | + final JsonNode ret = node.deepCopy(); |
| 74 | + if (path.isEmpty()) |
| 75 | + return MissingNode.getInstance(); |
| 76 | + if (path.path(node).isMissingNode()) { |
| 77 | + switch (pathMissingPolicy) { |
| 78 | + case THROW: |
| 79 | + throw new JsonPatchException(BUNDLE.getMessage( |
| 80 | + "jsonPatch.noSuchPath")); |
| 81 | + case SKIP: |
| 82 | + return ret; |
| 83 | + } |
| 84 | + } |
| 85 | + final JsonNode parentNode = path.parent().get(ret); |
| 86 | + final String raw = Iterables.getLast(path).getToken().getRaw(); |
| 87 | + if (parentNode.isObject()) |
| 88 | + ((ObjectNode) parentNode).remove(raw); |
| 89 | + else |
| 90 | + ((ArrayNode) parentNode).remove(Integer.parseInt(raw)); |
| 91 | + return ret; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void serialize(final JsonGenerator jgen, |
| 96 | + final SerializerProvider provider) |
| 97 | + throws IOException, JsonProcessingException |
| 98 | + { |
| 99 | + jgen.writeStartObject(); |
| 100 | + jgen.writeStringField("op", op); |
| 101 | + jgen.writeStringField("path", path.toString()); |
| 102 | + jgen.writeEndObject(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void serializeWithType(final JsonGenerator jgen, |
| 107 | + final SerializerProvider provider, final TypeSerializer typeSer) |
| 108 | + throws IOException, JsonProcessingException |
| 109 | + { |
| 110 | + serialize(jgen, provider); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() |
| 115 | + { |
| 116 | + return "op: " + op + "; path: \"" + path + '"'; |
| 117 | + } |
| 118 | +} |
0 commit comments