001package org.hl7.fhir.utilities.json.model; 002 003import java.text.ParseException; 004import java.text.SimpleDateFormat; 005import java.time.Instant; 006import java.time.OffsetDateTime; 007import java.util.ArrayList; 008import java.util.Date; 009import java.util.HashMap; 010import java.util.List; 011import java.util.Map; 012 013import org.hl7.fhir.utilities.Utilities; 014import org.hl7.fhir.utilities.json.JsonException; 015 016 017public class JsonObject extends JsonElement { 018 019 private List<JsonProperty> properties = new ArrayList<>(); 020 private Map<String, JsonProperty> propMap = new HashMap<>(); 021 private boolean extraComma; // json5 support 022 023 public JsonElementType type() { 024 return JsonElementType.OBJECT; 025 } 026 027 public JsonObject add(String name, JsonElement value) throws JsonException { 028 check(name != null, "Json Property Name is null"); 029 check(value != null, "Json Property Value is null"); 030 if (get(name) != null) { 031 check(false, "Name '"+name+"' already exists (value = "+get(name).toString()+")"); 032 } 033 JsonProperty p = new JsonProperty(name, value); 034 properties.add(p); 035 propMap.put(name, p); 036 return this; 037 } 038 039 public JsonObject addIfNotNull(String name, JsonElement value) throws JsonException { 040 if (value != null) { 041 check(name != null, "Name is null"); 042 if (get(name) != null) { 043 check(false, "Name '"+name+"' already exists (value = "+get(name).toString()+")"); 044 } 045 JsonProperty p = new JsonProperty(name, value); 046 properties.add(p); 047 propMap.put(name, p); 048 } 049 return this; 050 } 051 052 053 // this is used by the parser which can allow duplicates = true (for the validator). You should not otherwise use it 054 public JsonObject addForParser(String name, JsonElement value, boolean noComma, boolean nameUnquoted, boolean valueUnquoted) throws JsonException { 055 check(name != null, "Name is null"); 056 check(value != null, "Value is null"); 057 JsonProperty p = new JsonProperty(name, value); 058 p.setNoComma(noComma); 059 p.setUnquotedName(nameUnquoted); 060 p.setUnquotedValue(valueUnquoted); 061 properties.add(p); 062 propMap.put(name, p); // last duplicate wins 063 return this; 064 } 065 066 public JsonObject add(String name, String value) throws JsonException { 067 check(name != null, "Name is null"); 068 return add(name, value == null ? new JsonNull() : new JsonString(value)); 069 } 070 071 public JsonObject add(String name, List<String> values) throws JsonException { 072 check(name != null, "Name is null"); 073 JsonArray arr = new JsonArray(); 074 add(name, arr); 075 for (String v : values) { 076 arr.add(v); 077 } 078 return this; 079 } 080 081 public JsonObject addIfNotNull(String name, String value) throws JsonException { 082 check(name != null, "Name is null"); 083 if (value == null) { 084 return this; 085 } else { 086 return add(name, value == null ? new JsonNull() : new JsonString(value)); 087 } 088 } 089 090 public JsonObject add(String name, boolean value) throws JsonException { 091 check(name != null, "Name is null"); 092 return add(name, new JsonBoolean(value)); 093 } 094 095 public JsonObject add(String name, int value) throws JsonException { 096 check(name != null, "Name is null"); 097 return add(name, new JsonNumber(value)); 098 } 099 100 101 public JsonObject add(String name, long value) throws JsonException { 102 check(name != null, "Name is null"); 103 return add(name, new JsonNumber(value)); 104 } 105 106 107 public JsonObject set(String name, JsonElement value) throws JsonException { 108 check(name != null, "Name is null"); 109 check(value != null, "Value is null"); 110 JsonProperty p = propMap.get(name); 111 if (p != null) { 112 p.setValue(value); 113 return this; 114 } else { 115 return add(name, value); 116 } 117 } 118 119 public JsonObject set(String name, Instant value) throws JsonException { 120 String v = value == null ? null : value.toString(); 121 return set(name, v); 122 } 123 124 125 public JsonObject set(String name, String value) throws JsonException { 126 check(name != null, "Name is null"); 127 JsonProperty p = propMap.get(name); 128 if (p != null) { 129 p.setValue(value == null ? new JsonNull() : new JsonString(value)); 130 return this; 131 } else { 132 return add(name, value == null ? new JsonNull() : new JsonString(value)); 133 } 134 } 135 136 public JsonObject set(String name, boolean value) throws JsonException { 137 check(name != null, "Name is null"); 138 JsonProperty p = propMap.get(name); 139 if (p != null) { 140 p.setValue(new JsonBoolean(value)); 141 return this; 142 } else { 143 return add(name, new JsonBoolean(value)); 144 } 145 } 146 147 public JsonObject set(String name, int value) throws JsonException { 148 check(name != null, "Name is null"); 149 JsonProperty p = propMap.get(name); 150 if (p != null) { 151 p.setValue(new JsonNumber(value)); 152 return this; 153 } else { 154 return add(name, new JsonNumber(value)); 155 } 156 } 157 158 public JsonObject set(String name, long value) throws JsonException { 159 check(name != null, "Name is null"); 160 JsonProperty p = propMap.get(name); 161 if (p != null) { 162 p.setValue(new JsonNumber(value)); 163 return this; 164 } else { 165 return add(name, new JsonNumber(value)); 166 } 167 } 168 169 public JsonElement get(String name) { 170 if (propMap.containsKey(name)) { 171 return propMap.get(name).getValue(); 172 } else { 173 return null; 174 } 175 } 176 177 public boolean has(String name) { 178 return propMap.containsKey(name); 179 } 180 181 public boolean has(String... names) { 182 for (String n : names) { 183 if (propMap.containsKey(n)) { 184 return true; 185 } 186 } 187 return false; 188 } 189 190 public void remove(String name) { 191 if (propMap.containsKey(name)) { 192 propMap.remove(name); 193 properties.removeIf((JsonProperty item) -> name.equals(item.getName())); 194 } 195 } 196 197 public List<JsonProperty> getProperties() { 198 return properties; 199 } 200 201 public List<String> getNames() { 202 return Utilities.sorted(propMap.keySet()); 203 } 204 205 public String str(String name) { 206 if (hasPrimitive(name)) { 207 return get(name).asJsonPrimitive().getValue(); 208 } else { 209 return null; 210 } 211 } 212 213 public boolean hasObject(String name) { 214 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.OBJECT; 215 } 216 217 public boolean hasArray(String name) { 218 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.ARRAY; 219 } 220 221 public boolean hasPrimitive(String name) { 222 return propMap.containsKey(name) && propMap.get(name).getValue() instanceof JsonPrimitive; 223 } 224 225 public boolean hasString(String name) { 226 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.STRING; 227 } 228 229 public boolean hasNumber(String name) { 230 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.NUMBER; 231 } 232 233 public boolean hasBoolean(String name) { 234 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.BOOLEAN; 235 } 236 237 public boolean hasNull(String name) { 238 return propMap.containsKey(name) && propMap.get(name).getValue().type() == JsonElementType.NULL; 239 } 240 241 242 public JsonObject getJsonObject(String name) { 243 return hasObject(name) ? (JsonObject) get(name) : null; 244 } 245 246 public JsonString getJsonString(String name) { 247 return hasString(name) ? (JsonString) get(name) : null; 248 } 249 250 public JsonBoolean getJsonBoolean(String name) { 251 return hasBoolean(name) ? (JsonBoolean) get(name) : null; 252 } 253 254 public JsonNumber getJsonNumber(String name) { 255 return hasNumber(name) ? (JsonNumber) get(name) : null; 256 } 257 258 public JsonNull getJsonNull(String name) { 259 return hasNull(name) ?(JsonNull) get(name) : null; 260 } 261 262 public JsonArray getJsonArray(String name) { 263 return hasArray(name) ? (JsonArray) get(name) : null; 264 } 265 266 public Integer asInteger(String name) { 267 if (hasNumber(name)) { 268 return ((JsonNumber) get(name)).getInteger(); 269 } 270 if (hasPrimitive(name)) { 271 String s = asString(name); 272 if (Utilities.isInteger(s)) { 273 return Integer.parseInt(s); 274 } 275 } 276 return null; 277 } 278 279 public String asString(String name) { 280 return hasPrimitive(name) ? ((JsonPrimitive) get(name)).getValue() : null; 281 } 282 283 public String asString(String... names) { 284 for (String n : names) { 285 if (hasPrimitive(n)) { 286 return asString(n); 287 } 288 } 289 return null; 290 } 291 292 public boolean asBoolean(String name) { 293 if (hasBoolean(name)) { 294 return ((JsonBoolean) get(name)).isValue(); 295 } 296 if (hasPrimitive(name)) { 297 String s = asString(name); 298 if ("true".equals(s)) { 299 return true; 300 } 301 if ("false".equals(s)) { 302 return false; 303 } 304 } 305 return false; 306 } 307 308 public Instant asDate(String name) { 309 String source = asString(name); 310 if (Utilities.noString(source)) { 311 return null; 312 } else { 313 OffsetDateTime odt = OffsetDateTime.parse(source); 314 return odt.toInstant(); 315 } 316 } 317 318 public Instant asInstant(String name) throws ParseException { 319 String source = asString(name); 320 if (Utilities.noString(source) || "null".equals(source)) { 321 return null; 322 } else if (source.length() <= 10) { 323 Date d = new SimpleDateFormat("yyyy-mm-dd").parse(source); 324 return d.toInstant(); 325 } else { 326 OffsetDateTime odt = OffsetDateTime.parse(source); 327 return odt.toInstant(); 328 } 329 } 330 331 public JsonObject forceObject(String name) throws JsonException { 332 if (has(name) && !hasObject(name)) { 333 remove(name); 334 } 335 if (!has(name)) { 336 add(name, new JsonObject()); 337 } 338 return getJsonObject(name); 339 } 340 341 public JsonArray forceArray(String name) throws JsonException { 342 if (has(name) && !hasArray(name)) { 343 remove(name); 344 } 345 if (!has(name)) { 346 add(name, new JsonArray()); 347 } 348 return getJsonArray(name); 349 } 350 351 public List<JsonObject> getJsonObjects(String name) { 352 List<JsonObject> res = new ArrayList<>(); 353 if (hasArray(name)) { 354 res.addAll(getJsonArray(name).asJsonObjects()); 355 } else if (hasObject(name)) { 356 res.add(getJsonObject(name)); 357 } 358 return res; 359 } 360 361 public List<String> getStrings(String name) { 362 List<String> res = new ArrayList<>(); 363 if (hasArray(name)) { 364 res.addAll(getJsonArray(name).asStrings()); 365 } else if (hasPrimitive(name)) { 366 res.add(asString(name)); 367 } 368 return res; 369 } 370 371 public JsonObject deepCopy() { 372 return (JsonObject) make().copy(this); 373 } 374 375 @Override 376 protected JsonElement copy(JsonElement other) { 377 JsonObject o = (JsonObject) other; 378 for (JsonProperty p : o.getProperties()) { 379 add(p.getName(), p.getValue().deepCopy()); 380 } 381 return this; 382 } 383 384 @Override 385 protected JsonElement make() { 386 return new JsonObject(); 387 } 388 389 public JsonObject findByStringProp(String arrName, String prop, String value) { 390 for (JsonObject obj : getJsonObjects(arrName)) { 391 if (obj.has(prop) && value.equals(obj.asString(prop))) 392 return obj; 393 } 394 return null; 395 } 396 397 public void merge(JsonObject source) { 398 for (JsonProperty pp : source.getProperties()) { 399 if (has(pp.getName())) { 400 JsonElement te = get(pp.getName()); 401 if (te.isJsonObject() && pp.getValue().isJsonObject()) { 402 ((JsonObject) te).merge((JsonObject) pp.getValue()); 403 } else { 404 set(pp.getName(), pp.getValue()); 405 } 406 } else { 407 add(pp.getName(), pp.getValue()); 408 } 409 } 410 } 411 412 413 @Override 414 public String toString() { 415 StringBuilder b = new StringBuilder(); 416 b.append("{ "); 417 boolean first = true; 418 for (JsonProperty p : properties) { 419 if (first) first = false; else b.append(", "); 420 b.append(p.toString()); 421 } 422 b.append(" }"); 423 return b.toString(); 424 } 425 426 public boolean isExtraComma() { 427 return extraComma; 428 } 429 430 public void setExtraComma(boolean extraComma) { 431 this.extraComma = extraComma; 432 } 433 434 public void clear() { 435 properties.clear(); 436 propMap.clear(); 437 } 438 439}