Debian Codegraph @

src/org/eclipse/parsson/JsonMergePatchImpl.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2015, 2023 Oracle and/or its affiliates. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17 package org.eclipse.parsson;
18
19 import java.util.Objects;
20
21 import jakarta.json.JsonMergePatch;
22 import jakarta.json.JsonObject;
23 import jakarta.json.JsonObjectBuilder;
24 import jakarta.json.JsonValue;
25
26 /**
27 * This class is an implementation of a JSON Merge Patch as specified in
28 * <a href="http://tools.ietf.org/html/rfc7396">RFC 7396</a>.
29 *
30 * @since 1.1
31 */
32
33 public final class JsonMergePatchImpl implements JsonMergePatch {
34
35 private final JsonValue patch;
36 private final JsonContext jsonContext;
37
38 public JsonMergePatchImpl(JsonValue patch, JsonContext jsonContext) {
39 this.jsonContext = jsonContext;
40 this.patch = patch;
41 }
42
43 @Override
44 public JsonValue apply(JsonValue target) {
45 return mergePatch(target, patch);
46 }
47
48 @Override
49 public JsonValue toJsonValue() {
50 return patch;
51 }
52 /**
53 * Applies the specified patch to the specified target.
54 * The target is not modified by the patch.
55 *
56 * @param target the {@code JsonValue} to apply the patch operations
57 * @param patch the patch
58 * @return the {@code JsonValue} as the result of applying the patch
59 * operations on the target.
60 */
61 private JsonValue mergePatch(JsonValue target, JsonValue patch) {
62
63 if (patch.getValueType() != JsonValue.ValueType.OBJECT) {
64 return patch;
65 }
66 if (target.getValueType() != JsonValue.ValueType.OBJECT) {
67 target = JsonValue.EMPTY_JSON_OBJECT;
68 }
69 JsonObject targetJsonObject = target.asJsonObject();
70 JsonObjectBuilder builder =
71 new JsonObjectBuilderImpl(targetJsonObject, jsonContext);
72 patch.asJsonObject().forEach((key, value) -> {
73 if (value == JsonValue.NULL) {
74 if (targetJsonObject.containsKey(key)) {
75 builder.remove(key);
76 }
77 } else if (targetJsonObject.containsKey(key)) {
78 builder.add(key, mergePatch(targetJsonObject.get(key), value));
79 } else {
80 builder.add(key, mergePatch(JsonValue.EMPTY_JSON_OBJECT, value));
81 }
82 });
83 return builder.build();
84 }
85
86 /**
87 * Generate a JSON Merge Patch from the source and target {@code JsonValue}.
88 * @param source the source
89 * @param target the target
90 * @return a JSON Patch which when applied to the source, yields the target
91 */
92 static JsonValue diff(JsonValue source, JsonValue target, JsonContext jsonContext) {
93 if (source.getValueType() != JsonValue.ValueType.OBJECT ||
94 target.getValueType() != JsonValue.ValueType.OBJECT) {
95 return target;
96 }
97 JsonObject s = (JsonObject) source;
98 JsonObject t = (JsonObject) target;
99 JsonObjectBuilder builder = new JsonObjectBuilderImpl(jsonContext);
100 // First find members to be replaced or removed
101 s.forEach((key, value) -> {
102 if (t.containsKey(key)) {
103 // key present in both.
104 if (! value.equals(t.get(key))) {
105 // If the values are equal, nop, else get diff for the values
106 builder.add(key, diff(value, t.get(key), jsonContext));
107 }
108 } else {
109 builder.addNull(key);
110 }
111 });
112 // Then find members to be added
113 t.forEach((key, value) -> {
114 if (! s.containsKey(key))
115 builder.add(key, value);
116 });
117 return builder.build();
118 }
119
120 /**
121 * Compares this {@code JsonMergePatchImpl} with another object.
122 * @param obj the object to compare this {@code JsonMergePatchImpl} against
123 * @return true if the given object is a {@code JsonMergePatchImpl} with the same
124 * reference tokens as this one, false otherwise.
125 */
126 @Override
127 public boolean equals(Object obj) {
128 if (this == obj) {
129 return true;
130 } else if (obj == null) {
131 return false;
132 } else if (getClass() != JsonMergePatchImpl.class) {
133 return false;
134 } else {
135 JsonMergePatchImpl other = (JsonMergePatchImpl) obj;
136 return Objects.equals(patch, other.patch);
137 }
138 }
139
140 /**
141 * Returns the hash code value for this {@code JsonMergePatchImpl}.
142 *
143 * @return the hash code value for this {@code JsonMergePatchImpl} object
144 */
145 @Override
146 public int hashCode() {
147 return Objects.hashCode(patch);
148 }
149
150 /**
151 * Returns the JSON Patch text
152 * @return the JSON Patch text
153 */
154 @Override
155 public String toString() {
156 return patch.toString();
157 }
158 }
159