001package org.hl7.fhir.utilities.json;
002
003import java.io.IOException;
004import java.net.URL;
005import java.util.ArrayList;
006import java.util.List;
007import java.util.Map.Entry;
008
009import org.hl7.fhir.utilities.TextFile;
010
011/*
012  Copyright (c) 2011+, HL7, Inc.
013  All rights reserved.
014  
015  Redistribution and use in source and binary forms, with or without modification, 
016  are permitted provided that the following conditions are met:
017    
018   * Redistributions of source code must retain the above copyright notice, this 
019     list of conditions and the following disclaimer.
020   * Redistributions in binary form must reproduce the above copyright notice, 
021     this list of conditions and the following disclaimer in the documentation 
022     and/or other materials provided with the distribution.
023   * Neither the name of HL7 nor the names of its contributors may be used to 
024     endorse or promote products derived from this software without specific 
025     prior written permission.
026  
027  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
028  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
029  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
030  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
031  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
032  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
033  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
034  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
035  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
036  POSSIBILITY OF SUCH DAMAGE.
037  
038 */
039
040
041
042import com.google.gson.JsonArray;
043import com.google.gson.JsonElement;
044import com.google.gson.JsonNull;
045import com.google.gson.JsonObject;
046import com.google.gson.JsonPrimitive;
047
048public class JSONUtil {
049
050  public static JsonObject parse(String json) throws IOException {
051    return JsonTrackingParser.parseJson(json);    
052    
053  }
054
055  public static JsonObject forceObject(JsonObject obj, String name) {
056    if (obj.has(name) && obj.get(name).isJsonObject())
057      return obj.getAsJsonObject(name);
058    if (obj.has(name))
059      obj.remove(name);
060    JsonObject res = new JsonObject();
061    obj.add(name, res);
062    return res;
063  }
064
065  public static JsonArray forceArray(JsonObject obj, String name) {
066    if (obj.has(name) && obj.get(name).isJsonArray())
067      return obj.getAsJsonArray(name);
068    if (obj.has(name))
069      obj.remove(name);
070    JsonArray res = new JsonArray();
071    obj.add(name, res);
072    return res;  }
073
074  public static JsonObject addObj(JsonArray arr) {
075    JsonObject res = new JsonObject();
076    arr.add(res);
077    return res;
078  }
079
080  public static JsonObject findByStringProp(JsonArray arr, String prop, String value) {
081    for (JsonElement e : arr) {
082      JsonObject obj = (JsonObject) e;
083      if (obj.has(prop) && obj.get(prop).getAsString().equals(value)) 
084        return obj;
085    }
086    return null;
087  }
088
089  public static String str(JsonObject json, String name) {
090    JsonElement e = json.get(name);
091    return e == null || e instanceof JsonNull ? null : e.getAsString();
092  }
093
094  public static boolean bool(JsonObject json, String name) {
095    JsonElement e = json.get(name);
096    return e == null || e instanceof JsonNull ? false : e.getAsBoolean();
097  }
098
099  public static String str(JsonObject json, String name1, String name2) {
100    JsonElement e = json.get(name1);
101    if (e == null)
102      e = json.get(name2);
103    return e == null ? null : e instanceof JsonNull ? null :  e.getAsString();
104  }
105
106  public static boolean has(JsonObject json, String name1, String name2) {
107    return json.has(name1) || json.has(name2);
108  }
109
110  public static List<JsonObject> objects(JsonObject json, String name) {
111    List<JsonObject> res = new ArrayList<>();
112    if (json.has(name))
113      for (JsonElement e : json.getAsJsonArray(name))
114        if (e instanceof JsonObject)
115          res.add((JsonObject) e);
116    return res;
117  }
118
119  public static void merge(JsonObject source, JsonObject target) {
120    for (Entry<String, JsonElement> pp : source.entrySet()) {
121      if (target.has(pp.getKey())) {
122        JsonElement te = target.get(pp.getKey());
123        if (te.isJsonObject() && pp.getValue().isJsonObject()) {
124          merge(te.getAsJsonObject(), pp.getValue().getAsJsonObject());
125        } else {
126          target.remove(pp.getKey());
127          target.add(pp.getKey(), pp.getValue());
128        }
129      } else {
130        target.add(pp.getKey(), pp.getValue());
131      }
132    }
133  }
134
135  public static String type(JsonElement e) {
136    if (e == null) {
137      return "(null)";
138    }
139    if (e.isJsonObject()) {
140      return "Object";
141    }
142    if (e.isJsonArray()) {
143      return "Array";
144    }
145    if (e.isJsonNull()) {
146      return "Null";
147    }
148    JsonPrimitive p = (JsonPrimitive) e;
149    if (p.isBoolean()) {
150      return "Boolean";
151    }
152    if (p.isNumber()) {
153      return "Number";
154    }
155    return "String";
156  }
157
158}