forked from java-json-tools/json-patch
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOmitAction.java
More file actions
93 lines (83 loc) · 3.41 KB
/
OmitAction.java
File metadata and controls
93 lines (83 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.github.fge.jsonpatch.action;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jackson.JsonNumEquals;
import com.github.fge.jackson.jsonpointer.JsonPointer;
import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.JsonPatchMessages;
import com.github.fge.jsonpatch.operation.policy.PathMissingPolicy;
import com.github.fge.jsonpatch.properties.PathValueProperties;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.base.Equivalence;
import com.google.common.collect.Iterables;
import java.io.IOException;
public final class OmitAction
implements JsonPatchOperationAction
{
private final PathValueProperties properties;
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonPatchMessages.class);
private static final Equivalence<JsonNode> EQUIVALENCE
= JsonNumEquals.getInstance();
public OmitAction(final PathValueProperties properties)
{
this.properties = properties;
}
public JsonNode apply(final JsonNode node, final PathMissingPolicy pathMissingPolicy)
throws JsonPatchException
{
final String op = properties.getOp();
final JsonPointer path = properties.getPath();
final JsonNode value = properties.getValue();
final JsonNode ret = node.deepCopy();
if (path.isEmpty()) {
if (EQUIVALENCE.equivalent(ret, value)) {
return MissingNode.getInstance();
} else {
return ret;
}
}
final JsonNode valueAtPath = path.path(ret);
if (valueAtPath.isMissingNode()) {
switch (pathMissingPolicy) {
case THROW:
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.noSuchPath"));
case SKIP:
return ret;
default:
throw new JsonPatchException(BUNDLE.getMessage(
"jsonPatch.invalidPolicy"));
}
}
if (EQUIVALENCE.equivalent(valueAtPath, value)) {
final JsonNode parent = path.parent().get(ret);
final String rawToken = Iterables.getLast(path).getToken().getRaw();
if (parent.isObject())
((ObjectNode) parent).remove(rawToken);
else
((ArrayNode) parent).remove(Integer.parseInt(rawToken));
}
return ret;
}
public final void serialize(final JsonGenerator jgen,
final SerializerProvider provider)
throws IOException, JsonProcessingException
{
properties.serialize(jgen, provider);
}
public final void serializeWithType(final JsonGenerator jgen,
final SerializerProvider provider, final TypeSerializer typeSer)
throws IOException, JsonProcessingException
{
properties.serializeWithType(jgen, provider, typeSer);
}
}