-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonObject.java
337 lines (275 loc) · 10.8 KB
/
JsonObject.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.lupcode.JSON;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.lupcode.JSON.exceptions.JsonParseException;
import com.lupcode.JSON.exceptions.JsonParseUnfinishedException;
import com.lupcode.JSON.utils.LineColumnTracker;
import com.lupcode.Utilities.streams.UTF8CharInputStream;
import java.util.Set;
/** Represents a JSON object
* @author LupCode.com (Luca Vogels)
* @since 2020-12-23
*/
public class JsonObject extends JSON<JsonObject> {
private Map<String, JSON<?>> values = new LinkedHashMap<>();
public JsonObject(){
}
public JsonObject(Collection<JSON<?>> values){
if(values!=null){
int index = 0;
for(JSON<?> value : values){
this.values.put(index+"", value);
index++;
}
}
}
public int size(){
return values.size();
}
public JsonObject clear(){
values.clear();
return this;
}
public boolean has(String key){
return key!=null && values.containsKey(key);
}
public boolean containsKey(String key){
return key!=null && values.containsKey(key);
}
public boolean containsValue(JSON<?> object){
return values.containsValue(object);
}
public JSON<?> get(String key){
return key!=null ? values.get(key) : null;
}
public JsonArray getAsArray(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonArray)obj : null;
}
public JsonBoolean getAsJBoolean(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonBoolean)obj : null;
}
public Boolean getAsBoolean(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonBoolean)obj).getValue() : null;
}
public JsonNumber getAsNumber(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonNumber)obj : null;
}
public Double getAsDouble(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsDouble() : null;
}
public Float getAsFloat(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsFloat() : null;
}
public Integer getAsInt(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsInt() : null;
}
public Long getAsLong(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsLong() : null;
}
public Short getAsShort(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsShort() : null;
}
public Byte getAsByte(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonNumber)obj).getAsByte() : null;
}
public JsonObject getAsObject(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonObject)obj : null;
}
public JsonString getAsJString(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? (JsonString)obj : null;
}
public String getAsString(String key) throws ClassCastException {
JSON<?> obj = get(key);
return (obj!=null && !(obj instanceof JsonNull)) ? ((JsonString)obj).getValue() : null;
}
public JsonObject put(String key, JSON<?> object){
if(key==null){ throw new NullPointerException("Key cannot be null"); }
values.put(key, object!=null ? object : new JsonNull());
return this;
}
public JsonObject put(String key, String value){
return put(key, new JsonString(value));
}
public JsonObject put(String key, boolean value){
return put(key, new JsonBoolean(value));
}
public JsonObject put(String key, double value){
return put(key, new JsonNumber(value));
}
public JsonObject put(String key, float value){
return put(key, new JsonNumber(value));
}
public JsonObject put(String key, int value){
return put(key, new JsonNumber(value));
}
public JsonObject put(String key, long value){
return put(key, new JsonNumber(value));
}
public JsonObject put(String key, short value){
return put(key, new JsonNumber(value));
}
public JsonObject put(String key, byte value){
return put(key, new JsonNumber(value));
}
public JsonObject putArray(String key, JSON<?>... values){
return put(key, new JsonArray(values));
}
public JsonObject putArray(String key, Collection<JSON<?>> values){
return put(key, new JsonArray(values));
}
public JSON<?> remove(String key){
return key!=null ? values.remove(key) : null;
}
public Set<String> listKeys(){
return values.keySet();
}
public Collection<JSON<?>> listValues(){
return values.values();
}
public Set<Entry<String, JSON<?>>> entrySet(){
return values.entrySet();
}
public JsonArray keysAsJsonArray(){
return new JsonArray(values.keySet());
}
public JsonArray toJsonArray(){
return new JsonArray(values.values());
}
@Override
protected void toJSON(OutputStream output, boolean prettyPrint, String whitespace) throws IOException {
output.write("{".getBytes(StandardCharsets.UTF_8));
if(!values.isEmpty()){
if(prettyPrint){
boolean notFirst = false;
String ws = whitespace + SPACER;
byte[] wsArr = ws.getBytes(StandardCharsets.UTF_8);
for(Entry<String, JSON<?>> entry : values.entrySet()){
if(notFirst) output.write(",".getBytes(StandardCharsets.UTF_8)); else notFirst = true;
output.write(LINE_BREAKER.getBytes());
output.write(wsArr);
output.write("\"".getBytes(StandardCharsets.UTF_8));
output.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
output.write("\": ".getBytes(StandardCharsets.UTF_8));
(entry.getValue()!=null ? entry.getValue() : new JsonNull()).toJSON(output, prettyPrint, ws);
}
output.write(LINE_BREAKER.getBytes());
output.write(whitespace.getBytes(StandardCharsets.UTF_8));
} else {
boolean notFirst = false;
for(Entry<String, JSON<?>> entry : values.entrySet()){
if(notFirst) output.write(",".getBytes(StandardCharsets.UTF_8)); else notFirst = true;
output.write("\"".getBytes(StandardCharsets.UTF_8));
output.write(entry.getKey().getBytes(StandardCharsets.UTF_8));
output.write("\": ".getBytes(StandardCharsets.UTF_8));
(entry.getValue()!=null ? entry.getValue() : new JsonNull()).toJSON(output, prettyPrint, whitespace);
}
}
}
output.write("}".getBytes(StandardCharsets.UTF_8));
}
@Override
protected JsonObject parseJSON(UTF8CharInputStream input, LineColumnTracker lct) throws JsonParseException, IOException {
String c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException('{', lct); }
if(!c.equals("{")){ throw new JsonParseException('{', c, lct); }
lct.increaseColumn();
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(lct); }
if(c.equals("}")){ lct.increaseColumn(); this.values.clear(); return this; }
input.insertReadAgainAtBeginning(c); // to parse JsonString next
boolean nextEntry = false;
Map<String, JSON<?>> map = new LinkedHashMap<>();
do {
if(nextEntry) lct.increaseColumn(); // increment for ','
// check if empty ','
c = skipIgnorers(input, lct);
if(c!=null && c.equals("}")) break;
input.insertReadAgainAtBeginning(c);
// read key as JsonString
JsonString str = new JsonString().parseJSON(input, lct);
c = skipIgnorers(input, lct);
if(c==null){ throw new JsonParseUnfinishedException(':', lct); }
if(!c.equals(":")){ throw new JsonParseException(':', c, lct); }
lct.increaseColumn();
// read value as JSON<?>
map.put(str.getValue(), parseAutoJSON(input, lct));
c = skipIgnorers(input, lct);
} while((nextEntry = (c!=null && c.equals(","))));
if(c==null){ throw new JsonParseUnfinishedException('}', lct); }
if(!c.equals("}")){ throw new JsonParseException('}', c, lct); }
lct.increaseColumn();
this.values = map;
return this;
}
/** Tries to parse a given JSON string as a {@link JsonObject}
* @param json String that should be parsed
* @return Parsed JSON object
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonObject parse(String json) throws JsonParseException, NullPointerException {
return new JsonObject().parseJSON(json);
}
/** Tries to parse a given JSON string as a {@link JsonObject}
* @param json String that should be parsed
* @param offset Offset where to start reading the JSON string
* @return Parse JSON object
* @throws JsonParseException if JSON string could not be parsed correctly
* @throws NullPointerException if JSON string is null
*/
public static JsonObject parse(String json, int offset) throws JsonParseException, NullPointerException {
return new JsonObject().parseJSON(json, offset);
}
/** Tries to parse a {@link JsonObject} from a given {@link InputStream} in UTF-8
* @param json Stream in UTF-8 that should be parsed
* @return Parse JSON object
* @throws JsonParseException if input could not be parsed correctly
* @throws IOException if an error while reading {@link InputStream} occurred
* @throws NullPointerException if input is null
*/
public static JsonObject parse(UTF8CharInputStream input) throws JsonParseException, IOException, NullPointerException {
return new JsonObject().parseJSON(input);
}
/**
* Tries to parse a {@link JsonObject} from a given {@link File}
* @param file File the JSON data should be read from
* @return Parsed JSON object
* @throws JsonParseException if file could not be parsed correctly
* @throws NullPointerException if file is null
* @throws IOException if an error occurs while reading the {@link File}
*/
public static JsonObject parse(File file) throws JsonParseException, NullPointerException, IOException{
return new JsonObject().parseJSON(file);
}
/**
* Tries to parse a {@link JsonObject} from a given {@link URL}
* @param url URL the JSON data should be read from
* @return Parsed JSON object
* @throws JsonParseException if {@link URL} could not be parsed correctly
* @throws NullPointerException if {@link URL} is null
* @throws IOException if an error occurs while reading the from the {@link URL}
*/
public static JsonObject parse(URL url) throws JsonParseException, NullPointerException, IOException{
return new JsonObject().parseJSON(url);
}
}