{{ message }}
Repository navigation
Expand file tree
/
Copy pathJson.java
More file actions
394 lines (357 loc) · 12.3 KB
/
Copy pathJson.java
File metadata and controls
394 lines (357 loc) · 12.3 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.hook0.client;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* JSON, read and written with nothing but the standard library.
*
* <p>Java ships no JSON parser, and this artefact takes no dependency to get one. That is a deliberate trade rather
* than an oversight: every other Hook0 SDK installs with nothing beside it, and a Java artefact that dragged Jackson or
* Gson onto the classpath would put an application that only wanted to send an event in the position of reconciling its
* own pinned version with this one. A webhook SDK is not worth a dependency conflict, and what the API speaks is a
* small, closed subset of JSON.
*
* <p>Everything is bounded, in both directions. A document longer than {@link #MAX_DOCUMENT_CHARS}, nested deeper than
* {@link #MAX_DEPTH}, carrying a string longer than {@link #MAX_STRING_CHARS} or a collection larger than
* {@link #MAX_ITEMS} is <em>refused</em> rather than trimmed: half a document read as a whole one is worse than no
* document at all, so the ceiling is a failure and never a truncation.
*
* <p>What a document reads as: an object is a {@link LinkedHashMap} in the order it arrived, an array is a
* {@link List}, a string is a {@link String}, {@code true} and {@code false} are {@link Boolean}, {@code null} is
* {@code null}, and a number is a {@link Long} when it is whole and fits and a {@link Double} otherwise.
*/
public final class Json {
/** Longest document read or written, in characters. */
public static final int MAX_DOCUMENT_CHARS = 8 * 1024 * 1024;
/** Deepest an object or an array may nest, which is what stops a document of nothing but brackets. */
public static final int MAX_DEPTH = 64;
/** Longest string a document may carry, in characters. */
public static final int MAX_STRING_CHARS = 1024 * 1024;
/** Most members one object or items one array may carry. */
public static final int MAX_ITEMS = 100_000;
private final String text;
private int at;
private Json(String text) {
this.text = text;
}
/**
* Reads a document, refusing anything it cannot read whole.
*
* @param document the text to read
* @return the value it carries
* @throws JsonException for every way a document can fail to be one
*/
public static Object parse(String document) {
if (document == null) {
throw new JsonException("there is no document to read");
}
if (document.length() > MAX_DOCUMENT_CHARS) {
throw new JsonException(
"the document is " + document.length() + " characters long, above the " + MAX_DOCUMENT_CHARS + " read");
}
Json reading = new Json(document);
reading.skipBlanks();
Object value = reading.value(0);
reading.skipBlanks();
if (reading.at != document.length()) {
throw new JsonException("the document carries something past its end at character " + reading.at);
}
return value;
}
/**
* Writes a value the way the API reads one.
*
* @param value what to write
* @return the document carrying it
* @throws JsonException when the value is one JSON has no way to carry, or crosses a ceiling
*/
public static String write(Object value) {
StringBuilder written = new StringBuilder();
writeInto(written, value, 0);
if (written.length() > MAX_DOCUMENT_CHARS) {
throw new JsonException(
"the document is " + written.length() + " characters long, above the " + MAX_DOCUMENT_CHARS + " written");
}
return written.toString();
}
private static void writeInto(StringBuilder out, Object value, int depth) {
if (depth > MAX_DEPTH) {
throw new JsonException("the value nests deeper than the " + MAX_DEPTH + " written");
}
switch (value) {
case null -> out.append("null");
case String text -> writeText(out, text);
case Boolean flag -> out.append(flag.booleanValue() ? "true" : "false");
case Integer number -> out.append(number.intValue());
case Long number -> out.append(number.longValue());
case Double number -> writeNumber(out, number.doubleValue());
case Float number -> writeNumber(out, number.doubleValue());
case Map<?, ?> members -> writeObject(out, members, depth);
case Collection<?> items -> writeArray(out, items, depth);
default ->
throw new JsonException("a " + value.getClass().getName() + " is not something JSON carries");
}
}
private static void writeNumber(StringBuilder out, double number) {
if (Double.isNaN(number) || Double.isInfinite(number)) {
throw new JsonException("`" + number + "` is not a number JSON carries");
}
if (number == Math.floor(number) && Math.abs(number) < (double) (1L << 53)) {
out.append((long) number);
return;
}
out.append(number);
}
private static void writeObject(StringBuilder out, Map<?, ?> members, int depth) {
if (members.size() > MAX_ITEMS) {
throw new JsonException("an object carries " + members.size() + " members, above the " + MAX_ITEMS + " written");
}
out.append('{');
boolean first = true;
for (Map.Entry<?, ?> member : members.entrySet()) {
if (!(member.getKey() instanceof String name)) {
throw new JsonException("an object is keyed by something that is not a string");
}
if (!first) {
out.append(',');
}
first = false;
writeText(out, name);
out.append(':');
writeInto(out, member.getValue(), depth + 1);
}
out.append('}');
}
private static void writeArray(StringBuilder out, Collection<?> items, int depth) {
if (items.size() > MAX_ITEMS) {
throw new JsonException("an array carries " + items.size() + " items, above the " + MAX_ITEMS + " written");
}
out.append('[');
boolean first = true;
for (Object item : items) {
if (!first) {
out.append(',');
}
first = false;
writeInto(out, item, depth + 1);
}
out.append(']');
}
private static void writeText(StringBuilder out, String text) {
if (text.length() > MAX_STRING_CHARS) {
throw new JsonException(
"a string is " + text.length() + " characters long, above the " + MAX_STRING_CHARS + " written");
}
out.append('"');
for (int index = 0; index < text.length(); index++) {
char character = text.charAt(index);
switch (character) {
case '"' -> out.append("\\\"");
case '\\' -> out.append("\\\\");
case '\n' -> out.append("\\n");
case '\r' -> out.append("\\r");
case '\t' -> out.append("\\t");
case '\b' -> out.append("\\b");
case '\f' -> out.append("\\f");
default -> {
if (character < 0x20) {
out.append(String.format(java.util.Locale.ROOT, "\\u%04x", (int) character));
} else {
out.append(character);
}
}
}
}
out.append('"');
}
private Object value(int depth) {
if (depth > MAX_DEPTH) {
throw new JsonException("the document nests deeper than the " + MAX_DEPTH + " read");
}
if (at >= text.length()) {
throw new JsonException("the document stops where a value was expected");
}
return switch (text.charAt(at)) {
case '{' -> object(depth);
case '[' -> array(depth);
case '"' -> string();
case 't' -> literal("true", Boolean.TRUE);
case 'f' -> literal("false", Boolean.FALSE);
case 'n' -> literal("null", null);
default -> number();
};
}
private Object object(int depth) {
at++;
Map<String, Object> members = new LinkedHashMap<>();
skipBlanks();
if (peek() == '}') {
at++;
return members;
}
while (true) {
skipBlanks();
if (peek() != '"') {
throw new JsonException("an object names a member with something that is not a string at character " + at);
}
String name = string();
skipBlanks();
if (peek() != ':') {
throw new JsonException("an object member carries no value at character " + at);
}
at++;
skipBlanks();
members.put(name, value(depth + 1));
if (members.size() > MAX_ITEMS) {
throw new JsonException("an object carries more than the " + MAX_ITEMS + " members read");
}
skipBlanks();
char next = peek();
at++;
if (next == '}') {
return members;
}
if (next != ',') {
throw new JsonException("an object is not closed at character " + (at - 1));
}
}
}
private Object array(int depth) {
at++;
List<Object> items = new ArrayList<>();
skipBlanks();
if (peek() == ']') {
at++;
return items;
}
while (true) {
skipBlanks();
items.add(value(depth + 1));
if (items.size() > MAX_ITEMS) {
throw new JsonException("an array carries more than the " + MAX_ITEMS + " items read");
}
skipBlanks();
char next = peek();
at++;
if (next == ']') {
return items;
}
if (next != ',') {
throw new JsonException("an array is not closed at character " + (at - 1));
}
}
}
private String string() {
at++;
StringBuilder read = new StringBuilder();
while (true) {
if (at >= text.length()) {
throw new JsonException("a string is not closed before the document ends");
}
if (read.length() > MAX_STRING_CHARS) {
throw new JsonException("a string is longer than the " + MAX_STRING_CHARS + " characters read");
}
char character = text.charAt(at);
at++;
if (character == '"') {
return read.toString();
}
if (character == '\\') {
read.append(escaped());
continue;
}
if (character < 0x20) {
throw new JsonException("a string carries a control character at " + (at - 1));
}
read.append(character);
}
}
private char escaped() {
if (at >= text.length()) {
throw new JsonException("a string stops on an escape");
}
char marker = text.charAt(at);
at++;
return switch (marker) {
case '"' -> '"';
case '\\' -> '\\';
case '/' -> '/';
case 'b' -> '\b';
case 'f' -> '\f';
case 'n' -> '\n';
case 'r' -> '\r';
case 't' -> '\t';
case 'u' -> codePoint();
default -> throw new JsonException("`\\" + marker + "` is not an escape JSON declares");
};
}
private char codePoint() {
if (at + 4 > text.length()) {
throw new JsonException("a string stops inside an escaped code point");
}
String digits = text.substring(at, at + 4);
for (int index = 0; index < digits.length(); index++) {
if (Character.digit(digits.charAt(index), 16) < 0) {
throw new JsonException("`" + digits + "` is not an escaped code point");
}
}
at += 4;
return (char) Integer.parseInt(digits, 16);
}
private Object literal(String written, Object value) {
if (!text.startsWith(written, at)) {
throw new JsonException("the document carries something that is not a value at character " + at);
}
at += written.length();
return value;
}
private Object number() {
int start = at;
if (peek() == '-') {
at++;
}
boolean fractional = false;
while (at < text.length()) {
char character = text.charAt(at);
if (character >= '0' && character <= '9') {
at++;
continue;
}
if (character == '.' || character == 'e' || character == 'E' || character == '+' || character == '-') {
fractional = true;
at++;
continue;
}
break;
}
String written = text.substring(start, at);
if (written.isEmpty()) {
throw new JsonException("the document carries something that is not a value at character " + start);
}
try {
if (!fractional) {
return Long.valueOf(written);
}
return Double.valueOf(written);
} catch (NumberFormatException malformed) {
throw new JsonException("`" + written + "` is not a number");
}
}
private char peek() {
if (at >= text.length()) {
throw new JsonException("the document stops where more was expected");
}
return text.charAt(at);
}
private void skipBlanks() {
while (at < text.length()) {
char character = text.charAt(at);
if (character != ' ' && character != '\t' && character != '\n' && character != '\r') {
return;
}
at++;
}
}
}
You can’t perform that action at this time.
