Debian Codegraph @

src/main/org/h2/util/json/JSONStringTarget.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 import java.math.BigDecimal;
9
10 import org.h2.util.ByteStack;
11
12 /**
13 * JSON String target.
14 */
15 public final class JSONStringTarget extends JSONTarget<String> {
16
17 /**
18 * The hex characters.
19 */
20 static final char[] HEX = "0123456789abcdef".toCharArray();
21
22 /**
23 * A JSON object.
24 */
25 static final byte OBJECT = 1;
26
27 /**
28 * A JSON array.
29 */
30 static final byte ARRAY = 2;
31
32 /**
33 * Encodes a JSON string and appends it to the specified string builder.
34 *
35 * @param builder
36 * the string builder to append to
37 * @param s
38 * the string to encode
39 * @param asciiPrintableOnly
40 * whether all non-printable, non-ASCII characters, and {@code '}
41 * (single quote) characters should be escaped
42 * @return the specified string builder
43 */
44 public static StringBuilder encodeString(StringBuilder builder, String s, boolean asciiPrintableOnly) {
45 builder.append('"');
46 for (int i = 0, length = s.length(); i < length; i++) {
47 char c = s.charAt(i);
48 switch (c) {
49 case '\b':
50 builder.append("\\b");
51 break;
52 case '\t':
53 builder.append("\\t");
54 break;
55 case '\f':
56 builder.append("\\f");
57 break;
58 case '\n':
59 builder.append("\\n");
60 break;
61 case '\r':
62 builder.append("\\r");
63 break;
64 case '"':
65 builder.append("\\\"");
66 break;
67 case '\'':
68 if (asciiPrintableOnly) {
69 builder.append("\\u0027");
70 } else {
71 builder.append('\'');
72 }
73 break;
74 case '\\':
75 builder.append("\\\\");
76 break;
77 default:
78 if (c < ' ') {
79 builder.append("\\u00") //
80 .append(HEX[c >>> 4 & 0xf]) //
81 .append(HEX[c & 0xf]);
82 } else if (!asciiPrintableOnly || c <= 0x7f) {
83 builder.append(c);
84 } else {
85 builder.append("\\u") //
86 .append(HEX[c >>> 12 & 0xf]) //
87 .append(HEX[c >>> 8 & 0xf]) //
88 .append(HEX[c >>> 4 & 0xf]) //
89 .append(HEX[c & 0xf]);
90 }
91 }
92 }
93 return builder.append('"');
94 }
95
96 private final StringBuilder builder;
97
98 private final ByteStack stack;
99
100 private final boolean asciiPrintableOnly;
101
102 private boolean needSeparator;
103
104 private boolean afterName;
105
106 /**
107 * Creates new instance of JSON String target.
108 */
109 public JSONStringTarget() {
110 this(false);
111 }
112
113 /**
114 * Creates new instance of JSON String target.
115 *
116 * @param asciiPrintableOnly
117 * whether all non-printable, non-ASCII characters, and {@code '}
118 * (single quote) characters should be escaped
119 */
120 public JSONStringTarget(boolean asciiPrintableOnly) {
121 builder = new StringBuilder();
122 stack = new ByteStack();
123 this.asciiPrintableOnly = asciiPrintableOnly;
124 }
125
126 @Override
127 public void startObject() {
128 beforeValue();
129 afterName = false;
130 stack.push(OBJECT);
131 builder.append('{');
132 }
133
134 @Override
135 public void endObject() {
136 if (afterName || stack.poll(-1) != OBJECT) {
137 throw new IllegalStateException();
138 }
139 builder.append('}');
140 afterValue();
141 }
142
143 @Override
144 public void startArray() {
145 beforeValue();
146 afterName = false;
147 stack.push(ARRAY);
148 builder.append('[');
149 }
150
151 @Override
152 public void endArray() {
153 if (stack.poll(-1) != ARRAY) {
154 throw new IllegalStateException();
155 }
156 builder.append(']');
157 afterValue();
158 }
159
160 @Override
161 public void member(String name) {
162 if (afterName || stack.peek(-1) != OBJECT) {
163 throw new IllegalStateException();
164 }
165 afterName = true;
166 beforeValue();
167 encodeString(builder, name, asciiPrintableOnly).append(':');
168 }
169
170 @Override
171 public void valueNull() {
172 beforeValue();
173 builder.append("null");
174 afterValue();
175 }
176
177 @Override
178 public void valueFalse() {
179 beforeValue();
180 builder.append("false");
181 afterValue();
182 }
183
184 @Override
185 public void valueTrue() {
186 beforeValue();
187 builder.append("true");
188 afterValue();
189 }
190
191 @Override
192 public void valueNumber(BigDecimal number) {
193 beforeValue();
194 String s = number.toString();
195 int index = s.indexOf('E');
196 if (index >= 0 && s.charAt(++index) == '+') {
197 builder.append(s, 0, index).append(s, index + 1, s.length());
198 } else {
199 builder.append(s);
200 }
201 afterValue();
202 }
203
204 @Override
205 public void valueString(String string) {
206 beforeValue();
207 encodeString(builder, string, asciiPrintableOnly);
208 afterValue();
209 }
210
211 private void beforeValue() {
212 if (!afterName && stack.peek(-1) == OBJECT) {
213 throw new IllegalStateException();
214 }
215 if (needSeparator) {
216 if (stack.isEmpty()) {
217 throw new IllegalStateException();
218 }
219 needSeparator = false;
220 builder.append(',');
221 }
222 }
223
224 private void afterValue() {
225 needSeparator = true;
226 afterName = false;
227 }
228
229 @Override
230 public boolean isPropertyExpected() {
231 return !afterName && stack.peek(-1) == OBJECT;
232 }
233
234 @Override
235 public boolean isValueSeparatorExpected() {
236 return needSeparator;
237 }
238
239 @Override
240 public String getResult() {
241 if (!stack.isEmpty() || builder.length() == 0) {
242 throw new IllegalStateException();
243 }
244 return builder.toString();
245 }
246
247 }