001package org.hl7.fhir.utilities.json.model; 002 003import java.util.ArrayList; 004import java.util.Iterator; 005import java.util.List; 006 007import org.hl7.fhir.utilities.json.JsonException; 008 009 010public class JsonArray extends JsonElement implements Iterable<JsonElement> { 011 private List<JsonElement> items = new ArrayList<>(); 012 private List<Boolean> noCommas; // validator use 013 private List<Boolean> unQuoted; // validator use 014 private boolean extraComma; // json5 support 015 016 public List<String> asStrings() { 017 List<String> list = new ArrayList<>(); 018 for (JsonElement n : items) { 019 if (n instanceof JsonPrimitive) { 020 list.add(n.asJsonPrimitive().getValue()); 021 } 022 } 023 return list; 024 } 025 026 public List<JsonElement> getItems() { 027 return items; 028 } 029 030 public List<JsonObject> asJsonObjects() { 031 List<JsonObject> list = new ArrayList<>(); 032 for (JsonElement n : items) { 033 if (n instanceof JsonObject) { 034 list.add((JsonObject) n); 035 } 036 } 037 return list; 038 } 039 040 public JsonElementType type() { 041 return JsonElementType.ARRAY; 042 } 043 044 public JsonArray add(JsonElement node) throws JsonException { 045 check(node != null, "null object in JsonArray.add()"); 046 items.add(node); 047 return this; 048 } 049 050 public JsonArray add(int i, JsonElement node) throws JsonException { 051 check(node != null, "null object in JsonArray.add()"); 052 items.add(i, node); 053 return this; 054 } 055 056 public JsonArray add(String value) throws JsonException { 057 check(value != null, "null value in JsonArray.add()"); 058 items.add(new JsonString(value)); 059 return this; 060 } 061 062 public JsonObject addObject() { 063 JsonObject res = new JsonObject(); 064 add(res); 065 return res; 066 } 067 068 public Integer size() { 069 return items.size(); 070 } 071 072 public boolean isNoComma(int i) { 073 return noCommas == null ? false : noCommas.get(i); 074 } 075 076 public boolean isUnquoted(int i) { 077 return unQuoted == null ? false : unQuoted.get(i); 078 } 079 080 // for the parser only 081 public void addForParser(JsonElement e, boolean itemNoComma, boolean unquoted) throws JsonException { 082 check(e != null, "null object in JsonArray.add()"); 083 items.add(e); 084 if (noCommas == null) { 085 noCommas = new ArrayList<>(); 086 } 087 noCommas.add(itemNoComma); 088 if (unQuoted == null) { 089 unQuoted = new ArrayList<>(); 090 } 091 unQuoted.add(unquoted); 092 } 093 094 095 public JsonObject findByStringProp(String prop, String value) { 096 for (JsonObject obj : asJsonObjects()) { 097 if (obj.has(prop) && value.equals(obj.asString(prop))) 098 return obj; 099 } 100 return null; 101 } 102 103 public Iterator<JsonElement> iterator() { 104 return items.iterator(); 105 } 106 107 public JsonElement get(int i) { 108 return items.get(i); 109 } 110 111 public JsonArray deepCopy() { 112 return (JsonArray) make().copy(this); 113 } 114 115 @Override 116 protected JsonElement copy(JsonElement other) { 117 JsonArray o = (JsonArray) other; 118 for (JsonElement p : o.getItems()) { 119 add(p.deepCopy()); 120 } 121 return this; 122 } 123 124 @Override 125 protected JsonElement make() { 126 return new JsonArray(); 127 } 128 129 @Override 130 public String toString() { 131 StringBuilder b = new StringBuilder(); 132 b.append("[ "); 133 boolean first = true; 134 for (JsonElement p : items) { 135 if (first) first = false; else b.append(", "); 136 b.append(p.toString()); 137 } 138 b.append(" ]"); 139 return b.toString(); 140 } 141 142 public boolean isExtraComma() { 143 return extraComma; 144 } 145 146 public void setExtraComma(boolean extraComma) { 147 this.extraComma = extraComma; 148 } 149 150 public void remove(JsonElement e) { 151 items.remove(e); 152 153 } 154 155 public boolean has(String key) { 156 for (JsonElement e : items) { 157 if (e.isJsonString() && key.equals(e.asString())) { 158 return true; 159 } 160 } 161 return false; 162 } 163 164 public void addAll(JsonArray src) { 165 for (JsonElement e : src) { 166 add(e); 167 } 168 169 } 170 171}