Skip to content

Commit 4139d87

Browse files
authored
Feature/json object merge (#12)
* added stub for new method to merge json objects together * implemented JsonObject#merge method
1 parent e0fe873 commit 4139d87

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/main/java/technology/sola/json/JsonObject.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,60 @@ public JsonElement put(String key, Boolean value) {
164164
return put(key, new JsonElement(value));
165165
}
166166

167+
/**
168+
* Puts {@link JsonElement} of type {@link JsonElementType#NULL} into this {@link JsonObject}.
169+
*
170+
* @param key the key to put a NULL JSON value into
171+
* @return the previous {@link JsonElement} associated with this key
172+
*/
167173
public JsonElement putNull(String key) {
168174
return put(key, new JsonElement());
169175
}
170176

177+
/**
178+
* Merges this {@link JsonObject} with another resulting in a new {@code JsonObject}.
179+
* <ol>
180+
* <li>If key in both and each value is an object then it will recursively merge and add to result</li>
181+
* <li>If key in both and each value is not an object, value from the right is added to the result</li>
182+
* <li>If key in this only, then this value is added to result</li>
183+
* <li>If key in other only, then other value is added to result</li>
184+
* </ol>
185+
* This does not create a deep copy of nested arrays and objects.
186+
*
187+
* @param other the object to merge with
188+
* @return a new {@code JsonObject} with merged properties
189+
*/
190+
public JsonObject merge(JsonObject other) {
191+
JsonObject result = new JsonObject();
192+
193+
forEach((key, value) -> {
194+
var otherValue = other.get(key);
195+
196+
// add in any unique keys from this object
197+
if (otherValue == null) {
198+
result.put(key, value);
199+
} else {
200+
// nested merge if value in both is an object
201+
if (value.getType() == JsonElementType.JSON_OBJECT && otherValue.getType() == JsonElementType.JSON_OBJECT) {
202+
result.put(key, value.asObject().merge(otherValue.asObject()));
203+
} else {
204+
result.put(key, otherValue);
205+
}
206+
}
207+
});
208+
209+
// add in any unique keys from the other object
210+
other.forEach((key, value) -> {
211+
var thisValue = this.get(key);
212+
213+
if (thisValue == null) {
214+
result.put(key, value);
215+
}
216+
});
217+
218+
return result;
219+
}
220+
171221
@Override
172222
public String toString() {
173223
return toString(0);
@@ -184,5 +234,6 @@ public String toString(int spaces) {
184234

185235
solaJsonSerializer.getConfig().setSpaces(spaces);
186236

187-
return solaJsonSerializer.serialize(this); }
237+
return solaJsonSerializer.serialize(this);
238+
}
188239
}

src/test/java/technology/sola/json/JsonObjectTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,48 @@ void nullMethods() {
135135
assertTrue(root.isNull(TEST_KEY));
136136
}
137137
}
138+
139+
@Nested
140+
class merge {
141+
@Test
142+
void shouldHaveUniqueKeysFromBothObjects() {
143+
JsonObject objectOne = new JsonObject();
144+
145+
objectOne.put("test", 1);
146+
JsonObject objectTwo = new JsonObject();
147+
objectTwo.put("test2", 2);
148+
149+
JsonObject result = objectOne.merge(objectTwo);
150+
151+
assertEquals(1, result.getInt("test"));
152+
assertEquals(2, result.getInt("test2"));
153+
}
154+
155+
@Test
156+
void shouldReplaceDuplicateKeysIfNotObjects() {
157+
JsonObject objectOne = new JsonObject();
158+
objectOne.put("test", 1);
159+
JsonObject objectTwo = new JsonObject();
160+
objectTwo.put("test", 2);
161+
162+
assertEquals(2, objectOne.merge(objectTwo).getInt("test"));
163+
assertEquals(1, objectTwo.merge(objectOne).getInt("test"));
164+
}
165+
166+
@Test
167+
void shouldMergeNestedObjects() {
168+
JsonObject objectOne = new JsonObject();
169+
JsonObject objectOneNested = new JsonObject();
170+
objectOne.put("test", objectOneNested);
171+
objectOneNested.put("test", 1);
172+
173+
JsonObject objectTwo = new JsonObject();
174+
JsonObject objectTwoNested = new JsonObject();
175+
objectTwo.put("test", objectTwoNested);
176+
objectTwoNested.put("test", 2);
177+
178+
assertEquals(2, objectOne.merge(objectTwo).getObject("test").getInt("test"));
179+
assertEquals(1, objectTwo.merge(objectOne).getObject("test").getInt("test"));
180+
}
181+
}
138182
}

0 commit comments

Comments
 (0)