1 package com.exsoinn.util.epf;
2
3 import com.google.gson.JsonArray;
4 import com.google.gson.JsonElement;
5 import com.google.gson.JsonObject;
6 import com.google.gson.JsonParser;
7 import net.jcip.annotations.Immutable;
8
9 import java.util.*;
10
11
12
13
14
15
16
17 @Immutable
18 class JsonContext extends AbstractContext {
19 private final JsonElement je;
20
21
22
23
24
25
26
27 JsonContext(JsonElement pJsonElement) {
28 this(pJsonElement, false);
29 }
30
31
32 JsonContext(JsonElement pJsonElement, boolean pCreateCopy) {
33 if (pCreateCopy) {
34 je = generateBrandNewJsonElementObject(pJsonElement);
35 } else {
36 je = pJsonElement;
37 }
38 }
39
40
41
42
43
44
45
46
47
48
49
50
51
52 static JsonElement generateBrandNewJsonElementObject(JsonElement pJsonElem) {
53 if (pJsonElem.isJsonPrimitive()) {
54 return pJsonElem;
55 } else {
56 String jsonAsString;
57 try {
58
59
60
61
62
63
64
65
66 if (!pJsonElem.isJsonArray() || pJsonElem.getAsJsonArray().size() > 1) {
67 jsonAsString = pJsonElem.getAsString();
68 } else {
69 jsonAsString = pJsonElem.toString();
70 }
71 } catch (Exception e) {
72 jsonAsString = pJsonElem.toString();
73 }
74 JsonParser jp = new JsonParser();
75 return jp.parse(jsonAsString);
76 }
77 }
78
79 @Override
80 public boolean isPrimitive() {
81 return je.isJsonPrimitive();
82 }
83
84 @Override
85 public boolean isRecursible() {
86 return je.isJsonObject();
87 }
88
89 @Override
90 public boolean isArray() {
91 return je.isJsonArray();
92 }
93
94 @Override
95 public Context entryFromArray(int pIdx)
96 throws IllegalStateException {
97 if (!isArray()) {
98 throw new IllegalStateException("This is not an array element, " + je);
99 }
100
101 return new JsonContext(je.getAsJsonArray().get(pIdx));
102 }
103
104 @Override
105 public String stringRepresentation() {
106
107
108
109
110
111 try {
112 return je.getAsString();
113 } catch (Exception e) {
114 return je.toString();
115 }
116 }
117
118
119 @Override
120 public String toString() {
121 return je.toString();
122 }
123
124
125
126
127
128
129
130
131 @Override
132 public List<Context> asArray() throws IllegalStateException {
133 if (!je.isJsonArray()) {
134 throw new IllegalStateException("Object is not a JSON array, therefore asArray() call is invalid: " + je);
135 }
136 List<Context> list = new ArrayList<>();
137 JsonArray ja = je.getAsJsonArray();
138
139
140 ja.iterator().forEachRemaining(e -> list.add(new JsonContext(e)));
141
142 return Collections.unmodifiableList(list);
143 }
144
145
146
147
148
149
150
151 @Override
152 public boolean containsElement(String pElemName) {
153 if (!je.isJsonObject()) {
154 throw new IllegalStateException("Expected a JSON object, but found '" + je.getClass().getName()
155 + "', therefore containsElement() call is invalid. Element was: " + je);
156 }
157
158 return ((JsonObject)je).has(pElemName);
159 }
160
161
162
163
164
165
166
167
168 @Override
169 public Set<Map.Entry<String, Context>> entrySet() throws IllegalStateException {
170 if (!je.isJsonObject()) {
171 throw new IllegalStateException("Object is not an JSON object, therefore entrySet() call is invalid: " + je);
172 }
173
174 JsonObject jo = je.getAsJsonObject();
175 Set<Map.Entry<String, JsonElement>> ents = jo.entrySet();
176 Map<String, Context> newMap = new LinkedHashMap<>();
177
178
179
180
181 ents.forEach(e -> newMap.put(e.getKey(), new JsonContext(e.getValue())));
182 return Collections.unmodifiableSet(newMap.entrySet());
183 }
184
185 @Override
186 public Context memberValue(String pMemberName) throws IllegalStateException {
187 if (!je.isJsonObject()) {
188 throw new IllegalStateException("Object is not an JSON object, therefore memberValue() call is invalid: " + je);
189 }
190
191 return new JsonContext(je.getAsJsonObject().get(pMemberName));
192 }
193
194 @Override
195 public boolean arrayContains(String pVal) throws IllegalStateException {
196 if (!je.isJsonArray()) {
197 throw new IllegalStateException("Object is not an JSON array, therefore arrayContains call is invalid: " + je);
198 }
199
200 for (Context c : asArray()) {
201 if (pVal.equals(c.stringRepresentation())) {
202 return true;
203 }
204 }
205 return false;
206 }
207
208
209
210 JsonElement unwrap() {
211 return je;
212 }
213 }