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