Commit merge method

This commit is contained in:
Grahame Grieve 2019-05-21 07:39:01 +10:00
parent 05c5069c4c
commit 50c88dc1c1
1 changed files with 17 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package org.hl7.fhir.utilities.json;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
/*-
* #%L
@ -84,4 +85,20 @@ public class JSONUtil {
return res;
}
public static void merge(JsonObject source, JsonObject target) {
for (Entry<String, JsonElement> pp : source.entrySet()) {
if (target.has(pp.getKey())) {
JsonElement te = target.get(pp.getKey());
if (te.isJsonObject() && pp.getValue().isJsonObject()) {
merge(te.getAsJsonObject(), pp.getValue().getAsJsonObject());
} else {
target.remove(pp.getKey());
target.add(pp.getKey(), pp.getValue());
}
} else {
target.add(pp.getKey(), pp.getValue());
}
}
}
}