Skip to content

Commit

Permalink
fix JSONObject.merge blocks overwriting, replace with overwrite (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
QIURC committed Feb 11, 2025
1 parent d17008b commit 77b1f08
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 17 deletions.
20 changes: 10 additions & 10 deletions json-smart/src/main/java/net/minidev/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,23 @@ public void merge(Object o2) {
}

/**
* merge two JSONObject with override or not
* override = false will not override existing key
* override = true will override the value with o2 of existing key
* merge two JSONObject with overwrite or not
* overwrite = false will not overwrite existing key
* overwrite = true will overwrite the value with o2 of existing key
*/
public void merge(Object o2, boolean override) {
merge(this, o2, override);
public void merge(Object o2, boolean overwrite) {
merge(this, o2, overwrite);
}

protected static JSONObject merge(JSONObject o1, Object o2, boolean override) {
protected static JSONObject merge(JSONObject o1, Object o2, boolean overwrite) {
if (o2 == null)
return o1;
if (o2 instanceof JSONObject)
return merge(o1, (JSONObject) o2, override);
return merge(o1, (JSONObject) o2, overwrite);
throw new RuntimeException("JSON merge can not merge JSONObject with " + o2.getClass());
}

private static JSONObject merge(JSONObject o1, JSONObject o2, boolean override) {
private static JSONObject merge(JSONObject o1, JSONObject o2, boolean overwrite) {
if (o2 == null)
return o1;
for (String key : o1.keySet()) {
Expand All @@ -230,13 +230,13 @@ private static JSONObject merge(JSONObject o1, JSONObject o2, boolean override)
continue;
}
if (value1 instanceof JSONObject) {
o1.put(key, merge((JSONObject) value1, value2, false));
o1.put(key, merge((JSONObject) value1, value2, overwrite));
continue;
}
if (value1.equals(value2))
continue;
if (value1.getClass().equals(value2.getClass())) {
if (override) {
if (overwrite) {
o1.put(key, value2);
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package net.minidev.json;
package net.minidev.json.test;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class JSONObjectTest {

@Test
void mergeInteger() {
void mergeIntegerFailed() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", 1);
Assertions.assertEquals("{\"k1\":1}", jsonObject1.toJSONString());
Expand All @@ -20,7 +22,7 @@ void mergeInteger() {
jsonObject1.merge(jsonObject3);
Assertions.assertEquals("{\"k1\":11,\"k2\":2}", jsonObject1.toJSONString());

// replace with new value by JSONObject merge will failed
// replace with new value by JSONObject merge will fail
Exception exception = Assertions.assertThrows(RuntimeException.class, () -> {
JSONObject jsonObject2 = new JSONObject();
jsonObject2.appendField("k1", 101);
Expand All @@ -31,7 +33,7 @@ void mergeInteger() {
}

@Test
void mergeString() {
void mergeStringFailed() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", "v1");
Assertions.assertEquals("{\"k1\":\"v1\"}", jsonObject1.toJSONString());
Expand All @@ -45,7 +47,7 @@ void mergeString() {
jsonObject1.merge(jsonObject3);
Assertions.assertEquals("{\"k1\":\"vNew1\",\"k2\":\"v2\"}", jsonObject1.toJSONString());

// replace with new value by JSONObject merge will failed
// replace with new value by JSONObject merge will fail
Exception exception = Assertions.assertThrows(RuntimeException.class, () -> {
JSONObject jsonObject2 = new JSONObject();
jsonObject2.appendField("k1", "vNew2");
Expand All @@ -57,7 +59,59 @@ void mergeString() {
}

@Test
void mergeIntegerWithOverride() {
void mergeJsonObjectFailed() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", "v1");
Assertions.assertEquals("{\"k1\":\"v1\"}", jsonObject1.toJSONString());

JSONObject jsonObject2 = new JSONObject();
jsonObject2.appendField("k2", jsonObject1);
Assertions.assertEquals("{\"k2\":{\"k1\":\"v1\"}}", jsonObject2.toJSONString());

// replace with new value by JSONObject merge will fail
JSONObject jsonObject3 = new JSONObject();
jsonObject3.appendField("k1", "vNew1");

JSONObject jsonObject4 = new JSONObject();
jsonObject4.appendField("k2", jsonObject3);
Assertions.assertEquals("{\"k2\":{\"k1\":\"vNew1\"}}", jsonObject4.toJSONString());

Exception exception = Assertions.assertThrows(RuntimeException.class, () -> {
jsonObject4.merge(jsonObject2);
});

Assertions.assertEquals(exception.getMessage(), "JSON merge can not merge two java.lang.String Object together");
}

@Test
void mergeJsonArraySuccess() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", "v1");
JSONObject jsonObject2 = new JSONObject();
jsonObject2.appendField("k2", "v2");

JSONArray jsonArray1 = new JSONArray();
jsonArray1.add(jsonObject1);
jsonArray1.add(jsonObject2);
Assertions.assertEquals("[{\"k1\":\"v1\"},{\"k2\":\"v2\"}]", jsonArray1.toJSONString());

// replace with new value by JSONObject merge will fail
JSONObject jsonObject3 = new JSONObject();
jsonObject3.appendField("k1", "vNew1");
JSONObject jsonObject4 = new JSONObject();
jsonObject4.appendField("k2", "vNew2");

JSONArray jsonArray2 = new JSONArray();
jsonArray2.add(jsonObject3);
jsonArray2.add(jsonObject4);
Assertions.assertEquals("[{\"k1\":\"vNew1\"},{\"k2\":\"vNew2\"}]", jsonArray2.toJSONString());

jsonArray2.merge(jsonArray1);
Assertions.assertEquals("[{\"k1\":\"vNew1\"},{\"k2\":\"vNew2\"},{\"k1\":\"v1\"},{\"k2\":\"v2\"}]", jsonArray2.toJSONString());
}

@Test
void mergeIntegerWithOverwriteSuccess() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", 1);
Assertions.assertEquals("{\"k1\":1}", jsonObject1.toJSONString());
Expand All @@ -79,7 +133,7 @@ void mergeIntegerWithOverride() {
}

@Test
void mergeStringWithOverride() {
void mergeStringWithOverwriteSuccess() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", "v1");
Assertions.assertEquals("{\"k1\":\"v1\"}", jsonObject1.toJSONString());
Expand All @@ -99,4 +153,26 @@ void mergeStringWithOverride() {
jsonObject1.merge(jsonObject2, true);
Assertions.assertEquals("{\"k1\":\"vNew2\",\"k2\":\"v2\"}", jsonObject1.toJSONString());
}

@Test
void mergeJsonObjectWithOverwriteSuccess() {
JSONObject jsonObject1 = new JSONObject();
jsonObject1.appendField("k1", "v1");
Assertions.assertEquals("{\"k1\":\"v1\"}", jsonObject1.toJSONString());

JSONObject jsonObject2 = new JSONObject();
jsonObject2.appendField("k2", jsonObject1);
Assertions.assertEquals("{\"k2\":{\"k1\":\"v1\"}}", jsonObject2.toJSONString());

// JSONObject merge will overwrite jsonObject3 by jsonObject2
JSONObject jsonObject3 = new JSONObject();
jsonObject3.appendField("k1", "vNew1");

JSONObject jsonObject4 = new JSONObject();
jsonObject4.appendField("k2", jsonObject3);
Assertions.assertEquals("{\"k2\":{\"k1\":\"vNew1\"}}", jsonObject4.toJSONString());

jsonObject4.merge(jsonObject2, true);
Assertions.assertEquals("{\"k2\":{\"k1\":\"v1\"}}", jsonObject4.toJSONString());
}
}

0 comments on commit 77b1f08

Please sign in to comment.