001package org.hl7.fhir.utilities.json;
002
003import java.io.IOException;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Map.Entry;
007
008/*-
009 * #%L
010 * org.hl7.fhir.utilities
011 * %%
012 * Copyright (C) 2014 - 2019 Health Level 7
013 * %%
014 * Licensed under the Apache License, Version 2.0 (the "License");
015 * you may not use this file except in compliance with the License.
016 * You may obtain a copy of the License at
017 * 
018 *      http://www.apache.org/licenses/LICENSE-2.0
019 * 
020 * Unless required by applicable law or agreed to in writing, software
021 * distributed under the License is distributed on an "AS IS" BASIS,
022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
023 * See the License for the specific language governing permissions and
024 * limitations under the License.
025 * #L%
026 */
027
028
029import com.google.gson.JsonArray;
030import com.google.gson.JsonElement;
031import com.google.gson.JsonObject;
032
033public class JSONUtil {
034
035  public static JsonObject parse(String json) throws IOException {
036    return JsonTrackingParser.parseJson(json);    
037    
038  }
039
040  public static JsonObject forceObject(JsonObject obj, String name) {
041    if (obj.has(name) && obj.get(name).isJsonObject())
042      return obj.getAsJsonObject(name);
043    if (obj.has(name))
044      obj.remove(name);
045    JsonObject res = new JsonObject();
046    obj.add(name, res);
047    return res;
048  }
049
050  public static JsonArray forceArray(JsonObject obj, String name) {
051    if (obj.has(name) && obj.get(name).isJsonArray())
052      return obj.getAsJsonArray(name);
053    if (obj.has(name))
054      obj.remove(name);
055    JsonArray res = new JsonArray();
056    obj.add(name, res);
057    return res;  }
058
059  public static JsonObject addObj(JsonArray arr) {
060    JsonObject res = new JsonObject();
061    arr.add(res);
062    return res;
063  }
064
065  public static JsonObject findByStringProp(JsonArray arr, String prop, String value) {
066    for (JsonElement e : arr) {
067      JsonObject obj = (JsonObject) e;
068      if (obj.has(prop) && obj.get(prop).getAsString().equals(value)) 
069        return obj;
070    }
071    return null;
072  }
073
074  public static String str(JsonObject json, String name) {
075    JsonElement e = json.get(name);
076    return e == null ? null : e.getAsString();
077  }
078
079  public static String str(JsonObject json, String name1, String name2) {
080    JsonElement e = json.get(name1);
081    if (e == null)
082      e = json.get(name2);
083    return e == null ? null : e.getAsString();
084  }
085
086  public static boolean has(JsonObject json, String name1, String name2) {
087    return json.has(name1) || json.has(name2);
088  }
089
090  public static List<JsonObject> objects(JsonObject json, String name) {
091    List<JsonObject> res = new ArrayList<>();
092    if (json.has(name))
093      for (JsonElement e : json.getAsJsonArray(name))
094        if (e instanceof JsonObject)
095          res.add((JsonObject) e);
096    return res;
097  }
098
099  public static void merge(JsonObject source, JsonObject target) {
100    for (Entry<String, JsonElement> pp : source.entrySet()) {
101      if (target.has(pp.getKey())) {
102        JsonElement te = target.get(pp.getKey());
103        if (te.isJsonObject() && pp.getValue().isJsonObject()) {
104          merge(te.getAsJsonObject(), pp.getValue().getAsJsonObject());
105        } else {
106          target.remove(pp.getKey());
107          target.add(pp.getKey(), pp.getValue());
108        }
109      } else {
110        target.add(pp.getKey(), pp.getValue());
111      }
112    }
113  }
114
115}