Debian Codegraph @

src/main/org/h2/util/json/JSONTextSource.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.util.json;
7
8 /**
9 * JSON text source.
10 */
11 public abstract class JSONTextSource {
12
13 /**
14 * The output.
15 */
16 final JSONTarget<?> target;
17
18 private final StringBuilder builder;
19
20 JSONTextSource(JSONTarget<?> target) {
21 this.target = target;
22 builder = new StringBuilder();
23 }
24
25 /**
26 * Parse the text and write it to the output.
27 */
28 final void parse() {
29 boolean comma = false;
30 for (int ch; (ch = nextCharAfterWhitespace()) >= 0;) {
31 if (ch == '}' || ch == ']') {
32 if (comma) {
33 throw new IllegalArgumentException();
34 }
35 if (ch == '}') {
36 target.endObject();
37 } else {
38 target.endArray();
39 }
40 continue;
41 }
42 if (ch == ',') {
43 if (comma || !target.isValueSeparatorExpected()) {
44 throw new IllegalArgumentException();
45 }
46 comma = true;
47 continue;
48 }
49 if (comma != target.isValueSeparatorExpected()) {
50 throw new IllegalArgumentException();
51 }
52 comma = false;
53 switch (ch) {
54 case 'f':
55 readKeyword1("false");
56 target.valueFalse();
57 break;
58 case 'n':
59 readKeyword1("null");
60 target.valueNull();
61 break;
62 case 't':
63 readKeyword1("true");
64 target.valueTrue();
65 break;
66 case '{':
67 target.startObject();
68 break;
69 case '[':
70 target.startArray();
71 break;
72 case '"': {
73 String s = readString();
74 if (target.isPropertyExpected()) {
75 if (nextCharAfterWhitespace() != ':') {
76 throw new IllegalArgumentException();
77 }
78 target.member(s);
79 } else {
80 target.valueString(s);
81 }
82 break;
83 }
84 case '-':
85 parseNumber(false);
86 break;
87 case '0':
88 case '1':
89 case '2':
90 case '3':
91 case '4':
92 case '5':
93 case '6':
94 case '7':
95 case '8':
96 case '9':
97 parseNumber(true);
98 break;
99 default:
100 throw new IllegalArgumentException();
101 }
102 }
103 }
104
105 /**
106 * Skip all whitespace characters, and get the next character.
107 *
108 * @return the character code
109 */
110 abstract int nextCharAfterWhitespace();
111
112 /**
113 * Read the specified keyword, or (it there is no match), throw an
114 * IllegalArgumentException.
115 *
116 * @param keyword the expected keyword
117 */
118 abstract void readKeyword1(String keyword);
119
120 /**
121 * Parse a number.
122 *
123 * @param positive whether it needs to be positive
124 */
125 abstract void parseNumber(boolean positive);
126
127 /**
128 * Read the next character.
129 *
130 * @return the character code
131 */
132 abstract int nextChar();
133
134 /**
135 * Read 4 hex characters (0-9, a-f, A-F), and return the Unicode character.
136 *
137 * @return the character
138 */
139 abstract char readHex();
140
141 private String readString() {
142 builder.setLength(0);
143 boolean inSurrogate = false;
144 for (;;) {
145 int ch = nextChar();
146 switch (ch) {
147 case '"':
148 if (inSurrogate) {
149 throw new IllegalArgumentException();
150 }
151 return builder.toString();
152 case '\\':
153 ch = nextChar();
154 switch (ch) {
155 case '"':
156 case '/':
157 case '\\':
158 appendNonSurrogate((char) ch, inSurrogate);
159 break;
160 case 'b':
161 appendNonSurrogate('\b', inSurrogate);
162 break;
163 case 'f':
164 appendNonSurrogate('\f', inSurrogate);
165 break;
166 case 'n':
167 appendNonSurrogate('\n', inSurrogate);
168 break;
169 case 'r':
170 appendNonSurrogate('\r', inSurrogate);
171 break;
172 case 't':
173 appendNonSurrogate('\t', inSurrogate);
174 break;
175 case 'u':
176 inSurrogate = appendChar(readHex(), inSurrogate);
177 break;
178 default:
179 throw new IllegalArgumentException();
180 }
181 break;
182 default:
183 if (Character.isBmpCodePoint(ch)) {
184 inSurrogate = appendChar((char) ch, inSurrogate);
185 } else {
186 if (inSurrogate) {
187 throw new IllegalArgumentException();
188 }
189 builder.appendCodePoint(ch);
190 inSurrogate = false;
191 }
192 }
193 }
194 }
195
196 private void appendNonSurrogate(char ch, boolean inSurrogate) {
197 if (inSurrogate) {
198 throw new IllegalArgumentException();
199 }
200 builder.append(ch);
201 }
202
203 private boolean appendChar(char ch, boolean inSurrogate) {
204 if (inSurrogate != Character.isLowSurrogate(ch)) {
205 throw new IllegalArgumentException();
206 }
207 if (inSurrogate) {
208 inSurrogate = false;
209 } else if (Character.isHighSurrogate(ch)) {
210 inSurrogate = true;
211 }
212 builder.append(ch);
213 return inSurrogate;
214 }
215
216 }