1 | /*
|
---|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
---|
3 | *
|
---|
4 | * Copyright (c) 2015-2017 Oracle and/or its affiliates. All rights reserved.
|
---|
5 | *
|
---|
6 | * The contents of this file are subject to the terms of either the GNU
|
---|
7 | * General Public License Version 2 only ("GPL") or the Common Development
|
---|
8 | * and Distribution License("CDDL") (collectively, the "License"). You
|
---|
9 | * may not use this file except in compliance with the License. You can
|
---|
10 | * obtain a copy of the License at
|
---|
11 | * https://oss.oracle.com/licenses/CDDL+GPL-1.1
|
---|
12 | * or LICENSE.txt. See the License for the specific
|
---|
13 | * language governing permissions and limitations under the License.
|
---|
14 | *
|
---|
15 | * When distributing the software, include this License Header Notice in each
|
---|
16 | * file and include the License file at LICENSE.txt.
|
---|
17 | *
|
---|
18 | * GPL Classpath Exception:
|
---|
19 | * Oracle designates this particular file as subject to the "Classpath"
|
---|
20 | * exception as provided by Oracle in the GPL Version 2 section of the License
|
---|
21 | * file that accompanied this code.
|
---|
22 | *
|
---|
23 | * Modifications:
|
---|
24 | * If applicable, add the following below the License Header, with the fields
|
---|
25 | * enclosed by brackets [] replaced by your own identifying information:
|
---|
26 | * "Portions Copyright [year] [name of copyright owner]"
|
---|
27 | *
|
---|
28 | * Contributor(s):
|
---|
29 | * If you wish your version of this file to be governed by only the CDDL or
|
---|
30 | * only the GPL Version 2, indicate your decision by adding "[Contributor]
|
---|
31 | * elects to include this software in this distribution under the [CDDL or GPL
|
---|
32 | * Version 2] license." If you don't indicate a single choice of license, a
|
---|
33 | * recipient has the option to distribute your version of this file under
|
---|
34 | * either the CDDL, the GPL Version 2 or to extend the choice of license to
|
---|
35 | * its licensees as provided above. However, if you add GPL Version 2 code
|
---|
36 | * and therefore, elected the GPL Version 2 license, then the option applies
|
---|
37 | * only if the new code is made subject to such option by the copyright
|
---|
38 | * holder.
|
---|
39 | */
|
---|
40 |
|
---|
41 | package org.glassfish.json;
|
---|
42 |
|
---|
43 | import javax.json.Json;
|
---|
44 | import javax.json.JsonMergePatch;
|
---|
45 | import javax.json.JsonObject;
|
---|
46 | import javax.json.JsonObjectBuilder;
|
---|
47 | import javax.json.JsonValue;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * This class is an implementation of a JSON Merge Patch as specified in
|
---|
51 | * <a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>.
|
---|
52 | *
|
---|
53 | * @since 1.1
|
---|
54 | */
|
---|
55 |
|
---|
56 | public final class JsonMergePatchImpl implements JsonMergePatch {
|
---|
57 |
|
---|
58 | private JsonValue patch;
|
---|
59 |
|
---|
60 | public JsonMergePatchImpl(JsonValue patch) {
|
---|
61 | this.patch = patch;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public JsonValue apply(JsonValue target) {
|
---|
66 | return mergePatch(target, patch);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public JsonValue toJsonValue() {
|
---|
71 | return patch;
|
---|
72 | }
|
---|
73 | /**
|
---|
74 | * Applies the specified patch to the specified target.
|
---|
75 | * The target is not modified by the patch.
|
---|
76 | *
|
---|
77 | * @param target the {@code JsonValue} to apply the patch operations
|
---|
78 | * @param patch the patch
|
---|
79 | * @return the {@code JsonValue} as the result of applying the patch
|
---|
80 | * operations on the target.
|
---|
81 | */
|
---|
82 | private static JsonValue mergePatch(JsonValue target, JsonValue patch) {
|
---|
83 |
|
---|
84 | if (patch.getValueType() != JsonValue.ValueType.OBJECT) {
|
---|
85 | return patch;
|
---|
86 | }
|
---|
87 | if (target.getValueType() != JsonValue.ValueType.OBJECT) {
|
---|
88 | target = JsonValue.EMPTY_JSON_OBJECT;
|
---|
89 | }
|
---|
90 | JsonObject targetJsonObject = target.asJsonObject();
|
---|
91 | JsonObjectBuilder builder =
|
---|
92 | Json.createObjectBuilder(targetJsonObject);
|
---|
93 | patch.asJsonObject().forEach((key, value) -> {
|
---|
94 | if (value == JsonValue.NULL) {
|
---|
95 | if (targetJsonObject.containsKey(key)) {
|
---|
96 | builder.remove(key);
|
---|
97 | }
|
---|
98 | } else if (targetJsonObject.containsKey(key)) {
|
---|
99 | builder.add(key, mergePatch(targetJsonObject.get(key), value));
|
---|
100 | } else {
|
---|
101 | builder.add(key, mergePatch(JsonValue.EMPTY_JSON_OBJECT, value));
|
---|
102 | }
|
---|
103 | });
|
---|
104 | return builder.build();
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Generate a JSON Merge Patch from the source and target {@code JsonValue}.
|
---|
109 | * @param source the source
|
---|
110 | * @param target the target
|
---|
111 | * @return a JSON Patch which when applied to the source, yields the target
|
---|
112 | */
|
---|
113 | static JsonValue diff(JsonValue source, JsonValue target) {
|
---|
114 | if (source.getValueType() != JsonValue.ValueType.OBJECT ||
|
---|
115 | target.getValueType() != JsonValue.ValueType.OBJECT) {
|
---|
116 | return target;
|
---|
117 | }
|
---|
118 | JsonObject s = (JsonObject) source;
|
---|
119 | JsonObject t = (JsonObject) target;
|
---|
120 | JsonObjectBuilder builder = Json.createObjectBuilder();
|
---|
121 | // First find members to be replaced or removed
|
---|
122 | s.forEach((key, value) -> {
|
---|
123 | if (t.containsKey(key)) {
|
---|
124 | // key present in both.
|
---|
125 | if (! value.equals(t.get(key))) {
|
---|
126 | // If the values are equal, nop, else get diff for the values
|
---|
127 | builder.add(key, diff(value, t.get(key)));
|
---|
128 | }
|
---|
129 | } else {
|
---|
130 | builder.addNull(key);
|
---|
131 | }
|
---|
132 | });
|
---|
133 | // Then find members to be added
|
---|
134 | t.forEach((key, value) -> {
|
---|
135 | if (! s.containsKey(key))
|
---|
136 | builder.add(key, value);
|
---|
137 | });
|
---|
138 | return builder.build();
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|