minor improvements to json library

This commit is contained in:
Grahame Grieve 2022-11-25 22:37:18 -03:00
parent 21bd9bf7e9
commit 450fa1d02c
3 changed files with 26 additions and 3 deletions

View File

@ -5,7 +5,7 @@ import org.hl7.fhir.exceptions.FHIRException;
public class JsonException extends FHIRException {
public JsonException(String msg) {
// TODO Auto-generated constructor stub
super(msg);
}
}

View File

@ -79,7 +79,7 @@ public class JsonArray extends JsonElement implements Iterable<JsonElement> {
}
public JsonObject findByStringProp(JsonArray arr, String prop, String value) {
public JsonObject findByStringProp(String prop, String value) {
for (JsonObject obj : asJsonObjects()) {
if (obj.has(prop) && value.equals(obj.asString(prop)))
return obj;

View File

@ -24,7 +24,9 @@ public class JsonObject extends JsonElement {
public JsonObject add(String name, JsonElement value) throws JsonException {
check(name != null, "Name is null");
check(value != null, "Value is null");
check(get(name) == null, "Name '"+name+"' already exists");
if (get(name) != null) {
check(false, "Name '"+name+"' already exists (value = "+get(name).toString()+")");
}
JsonProperty p = new JsonProperty(name, value);
properties.add(p);
propMap.put(name, p);
@ -116,6 +118,15 @@ public class JsonObject extends JsonElement {
return propMap.containsKey(name);
}
public boolean has(String... names) {
for (String n : names) {
if (propMap.containsKey(n)) {
return true;
}
}
return false;
}
public void remove(String name) {
if (propMap.containsKey(name)) {
propMap.remove(name);
@ -127,6 +138,10 @@ public class JsonObject extends JsonElement {
return properties;
}
public List<String> getNames() {
return Utilities.sorted(propMap.keySet());
}
public String str(String name) {
if (hasPrimitive(name)) {
return get(name).asJsonPrimitive().getValue();
@ -298,6 +313,14 @@ public class JsonObject extends JsonElement {
return new JsonObject();
}
public JsonObject findByStringProp(String arrName, String prop, String value) {
for (JsonObject obj : getJsonObjects(arrName)) {
if (obj.has(prop) && value.equals(obj.asString(prop)))
return obj;
}
return null;
}
public void merge(JsonObject source) {
for (JsonProperty pp : source.getProperties()) {
if (has(pp.getName())) {