-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathJsonPatch.java
193 lines (180 loc) · 6.05 KB
/
JsonPatch.java
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2014, Francis Galiegue ([email protected])
* Copyright (c) 2016, Alexander Patrikalakis ([email protected])
* Copyright (c) 2015, Daisuke Miyamoto ([email protected])
*
* This software is dual-licensed under:
*
* - the Lesser General Public License (LGPL) version 3.0 or, at your option, any
* later version;
* - the Apache Software License (ASL) version 2.0.
*
* The text of this file and of both licenses is available at the root of this
* project or, if you have the jar distribution, in directory META-INF/, under
* the names LGPL-3.0.txt and ASL-2.0.txt respectively.
*
* Direct link to the sources:
*
* - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt
* - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt
*/
package com.github.fge.jsonpatch;
import com.amazonaws.services.dynamodbv2.xspec.ExpressionSpecBuilder;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.core.JsonGenerator;
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.github.fge.jackson.JacksonUtils;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.util.List;
/**
* Implementation of JSON Patch
*
* <p><a href="http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-10">JSON
* Patch</a>, as its name implies, is an IETF draft describing a mechanism to
* apply a patch to any JSON value. This implementation covers all operations
* according to the specification; however, there are some subtle differences
* with regards to some operations which are covered in these operations'
* respective documentation.</p>
*
* <p>An example of a JSON Patch is as follows:</p>
*
* <pre>
* [
* {
* "op": "add",
* "path": "/-",
* "value": {
* "productId": 19,
* "name": "Duvel",
* "type": "beer"
* }
* }
* ]
* </pre>
*
* <p>This patch contains a single operation which adds an item at the end of
* an array. A JSON Patch can contain more than one operation; in this case, all
* operations are applied to the input JSON value in their order of appearance,
* until all operations are applied or an error condition is encountered.</p>
*
* <p>The main point where this implementation differs from the specification
* is initial JSON parsing. The draft says:</p>
*
* <pre>
* Operation objects MUST have exactly one "op" member
* </pre>
*
* <p>and:</p>
*
* <pre>
* Additionally, operation objects MUST have exactly one "path" member.
* </pre>
*
* <p>However, obeying these to the letter forces constraints on the JSON
* <b>parser</b>. Here, these constraints are not enforced, which means:</p>
*
* <pre>
* [ { "op": "add", "op": "remove", "path": "/x" } ]
* </pre>
*
* <p>is parsed (as a {@code remove} operation, since it appears last).</p>
*
* <p><b>IMPORTANT NOTE:</b> the JSON Patch is supposed to be VALID when the
* constructor for this class ({@link JsonPatch#fromJson(JsonNode)} is used.</p>
*/
public class JsonPatch
implements JsonSerializable, Supplier<ExpressionSpecBuilder>
{
private static final MessageBundle BUNDLE
= MessageBundles.getBundle(JsonPatchMessages.class);
/**
* List of operations
*/
protected final List<JsonPatchOperation> operations;
/**
* Constructor
*
* <p>Normally, you should never have to use it.</p>
*
* @param operations the list of operations for this patch
* @see JsonPatchOperation
*/
@JsonCreator
public JsonPatch(final List<JsonPatchOperation> operations)
{
this.operations = ImmutableList.copyOf(operations);
}
/**
* Static factory method to build a JSON Patch out of a JSON representation
*
* @param node the JSON representation of the generated JSON Patch
* @return a JSON Patch
* @throws IOException input is not a valid JSON patch
* @throws NullPointerException input is null
*/
public static JsonPatch fromJson(final JsonNode node)
throws IOException
{
BUNDLE.checkNotNull(node, "jsonPatch.nullInput");
return JacksonUtils.getReader().forType(JsonPatch.class)
.readValue(node);
}
/**
* Apply this patch to a JSON value
*
* @param node the value to apply the patch to
* @return the patched JSON value
* @throws JsonPatchException failed to apply patch
* @throws NullPointerException input is null
*/
public JsonNode apply(final JsonNode node)
throws JsonPatchException
{
BUNDLE.checkNotNull(node, "jsonPatch.nullInput");
JsonNode ret = node;
for (final JsonPatchOperation operation: operations)
ret = operation.apply(ret);
return ret;
}
/**
* Converts this JsonPatch into an ExpressionSpecBuilder
* @return an expression spec builder that contains the updates contained in this
* patch
*/
public ExpressionSpecBuilder get() {
ExpressionSpecBuilder builder = new ExpressionSpecBuilder();
for(JsonPatchOperation operation : operations) {
operation.applyToBuilder(builder);
}
return builder;
}
@Override
public String toString()
{
return operations.toString();
}
@Override
public void serialize(final JsonGenerator jgen,
final SerializerProvider provider)
throws IOException
{
jgen.writeStartArray();
for (final JsonPatchOperation op: operations)
op.serialize(jgen, provider);
jgen.writeEndArray();
}
@Override
public void serializeWithType(final JsonGenerator jgen,
final SerializerProvider provider, final TypeSerializer typeSer)
throws IOException
{
serialize(jgen, provider);
}
}