001package org.hl7.fhir.utilities;
002
003import java.util.Map.Entry;
004
005import com.google.gson.JsonElement;
006import com.google.gson.JsonObject;
007import com.google.gson.JsonPrimitive;
008
009public class JsonMerger {
010
011
012  public void merge(JsonObject dest, JsonObject source) {
013    for (Entry<String, JsonElement> e : source.entrySet()) {
014      if (dest.has(e.getKey())) {
015        if (e.getValue() instanceof JsonObject && dest.get(e.getKey()) instanceof JsonObject) 
016          merge((JsonObject) dest.get(e.getKey()), (JsonObject) e.getValue());
017        else if (e.getValue() instanceof JsonPrimitive && dest.get(e.getKey()) instanceof JsonPrimitive) {
018          dest.remove(e.getKey());
019          dest.add(e.getKey(), e.getValue());
020        } else
021          throw new Error("Not supported yet?");
022      } else 
023        dest.add(e.getKey(), e.getValue());
024    }
025    
026  }
027
028}