001package org.hl7.fhir.utilities.json; 002 003import java.io.IOException; 004import java.net.MalformedURLException; 005import java.net.URL; 006import java.net.URLConnection; 007import java.util.ArrayList; 008import java.util.List; 009import java.util.Map.Entry; 010 011import org.hl7.fhir.utilities.TextFile; 012 013/* 014 Copyright (c) 2011+, HL7, Inc. 015 All rights reserved. 016 017 Redistribution and use in source and binary forms, with or without modification, 018 are permitted provided that the following conditions are met: 019 020 * Redistributions of source code must retain the above copyright notice, this 021 list of conditions and the following disclaimer. 022 * Redistributions in binary form must reproduce the above copyright notice, 023 this list of conditions and the following disclaimer in the documentation 024 and/or other materials provided with the distribution. 025 * Neither the name of HL7 nor the names of its contributors may be used to 026 endorse or promote products derived from this software without specific 027 prior written permission. 028 029 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 030 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 031 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 032 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 033 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 034 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 035 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 036 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 037 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 038 POSSIBILITY OF SUCH DAMAGE. 039 040 */ 041 042 043 044import com.google.gson.JsonArray; 045import com.google.gson.JsonElement; 046import com.google.gson.JsonNull; 047import com.google.gson.JsonObject; 048 049public class JSONUtil { 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 JsonObject fetchJson(String source) throws IOException { 137 URL url = new URL(source); 138 URLConnection c = url.openConnection(); 139 return (JsonObject) new com.google.gson.JsonParser().parse(TextFile.streamToString(c.getInputStream())); 140 } 141 142}