forked from java-json-tools/json-patch
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathJsonMergePatch.java
116 lines (109 loc) · 4.22 KB
/
JsonMergePatch.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
/*
* Copyright (c) 2014, Francis Galiegue ([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.gravity9.jsonpatch.mergepatch;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonSerializable;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.github.fge.jackson.JacksonUtils;
import com.github.fge.msgsimple.bundle.MessageBundle;
import com.github.fge.msgsimple.load.MessageBundles;
import com.gravity9.jsonpatch.JsonPatch;
import com.gravity9.jsonpatch.JsonPatchException;
import com.gravity9.jsonpatch.JsonPatchMessages;
import com.gravity9.jsonpatch.Patch;
import java.io.IOException;
import javax.annotation.ParametersAreNonnullByDefault;
/**
* Implementation of JSON Merge Patch (RFC 7386)
*
* <p><a href="http://tools.ietf.org/html/rfc7386">JSON Merge Patch</a> is a
* "toned down" version of JSON Patch. However, it covers a very large number of
* use cases for JSON value modifications; its focus is mostly on patching
* JSON Objects, which are by far the most common type of JSON texts exchanged
* on the Internet.</p>
*
* <p>Applying a JSON Merge Patch is defined by a single, pseudo code function
* as follows (quoted from the RFC; indentation fixed):</p>
*
* <pre>
* define MergePatch(Target, Patch):
* if Patch is an Object:
* if Target is not an Object:
* Target = {} # Ignore the contents and set it to an empty Object
* for each Name/Value pair in Patch:
* if Value is null:
* if Name exists in Target:
* remove the Name/Value pair from Target
* else:
* Target[Name] = MergePatch(Target[Name], Value)
* return Target
* else:
* return Patch
* </pre>
*/
@ParametersAreNonnullByDefault
@JsonDeserialize(using = JsonMergePatchDeserializer.class)
public abstract class JsonMergePatch implements JsonSerializable, Patch {
protected static final MessageBundle BUNDLE = MessageBundles.getBundle(JsonPatchMessages.class);
private static final ObjectMapper DEFAULT_MAPPER = JacksonUtils.newMapper();
/**
* Build an instance from a JSON input
*
* @param node the input
* @return a JSON Merge Patch instance
* @throws JsonPatchException failed to deserialize
* @throws NullPointerException node is null
*/
public static JsonMergePatch fromJson(final JsonNode node) throws JsonPatchException {
return fromJson(node, DEFAULT_MAPPER);
}
/**
* Build an instance from a JSON input with a custom ObjectMapper.
* This allows to customize the mapper used in deserialization of nodes.
*
* @param node the input
* @param mapper custom ObjectMapper
* @return a JSON Merge Patch instance
* @throws JsonPatchException failed to deserialize
* @throws NullPointerException node or mapper is null
*/
public static JsonMergePatch fromJson(final JsonNode node, ObjectMapper mapper) throws JsonPatchException {
BUNDLE.checkNotNull(node, "jsonPatch.nullInput");
BUNDLE.checkNotNull(mapper, "jsonPatch.nullInput");
try {
return mapper.readValue(node.traverse(), JsonMergePatch.class);
} catch (IOException e) {
throw new JsonPatchException(
BUNDLE.getMessage("jsonPatch.deserFailed"), e);
}
}
/**
* Apply the patch to a given JSON value
*
* @param input the value to patch
* @return the patched value
* @throws JsonPatchException never thrown; only for consistency with
* {@link JsonPatch}
* @throws NullPointerException value is null
*/
@Override
public abstract JsonNode apply(final JsonNode input)
throws JsonPatchException;
}