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