Debian Codegraph @

src/main/org/h2/util/json/JSONStringSource.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.StringUtils;
11
12 /**
13 * JSON string source.
14 */
15 public final class JSONStringSource extends JSONTextSource {
16
17 /**
18 * Parses source string to a specified target.
19 *
20 * @param string
21 * source
22 * @param target
23 * target
24 * @param <R>
25 * the type of the result
26 * @return the result of the target
27 */
28 public static <R> R parse(String string, JSONTarget<R> target) {
29 new JSONStringSource(string, target).parse();
30 return target.getResult();
31 }
32
33 /**
34 * Normalizes textual JSON representation.
35 *
36 * @param string
37 * source representation
38 * @return normalized representation
39 */
40 public static byte[] normalize(String string) {
41 return parse(string, new JSONByteArrayTarget());
42 }
43
44 private final String string;
45
46 private final int length;
47
48 private int index;
49
50 JSONStringSource(String string, JSONTarget<?> target) {
51 super(target);
52 this.string = string;
53 this.length = string.length();
54 if (length == 0) {
55 throw new IllegalArgumentException();
56 }
57 // Ignore BOM
58 if (string.charAt(index) == '\uFEFF') {
59 index++;
60 }
61 }
62
63 @Override
64 int nextCharAfterWhitespace() {
65 int index = this.index;
66 while (index < length) {
67 char ch = string.charAt(index++);
68 switch (ch) {
69 case '\t':
70 case '\n':
71 case '\r':
72 case ' ':
73 break;
74 default:
75 this.index = index;
76 return ch;
77 }
78 }
79 return -1;
80 }
81
82 @Override
83 void readKeyword1(String keyword) {
84 int l = keyword.length() - 1;
85 if (!string.regionMatches(index, keyword, 1, l)) {
86 throw new IllegalArgumentException();
87 }
88 index += l;
89 }
90
91 @Override
92 void parseNumber(boolean positive) {
93 int index = this.index;
94 int start = index - 1;
95 index = skipInt(index, positive);
96 l: if (index < length) {
97 char ch = string.charAt(index);
98 if (ch == '.') {
99 index = skipInt(index + 1, false);
100 if (index >= length) {
101 break l;
102 }
103 ch = string.charAt(index);
104 }
105 if (ch == 'E' || ch == 'e') {
106 if (++index >= length) {
107 throw new IllegalArgumentException();
108 }
109 ch = string.charAt(index);
110 if (ch == '+' || ch == '-') {
111 index++;
112 }
113 index = skipInt(index, false);
114 }
115 }
116 target.valueNumber(new BigDecimal(string.substring(start, index)));
117 this.index = index;
118 }
119
120 private int skipInt(int index, boolean hasInt) {
121 while (index < length) {
122 char ch = string.charAt(index);
123 if (ch >= '0' && ch <= '9') {
124 hasInt = true;
125 index++;
126 } else {
127 break;
128 }
129 }
130 if (!hasInt) {
131 throw new IllegalArgumentException();
132 }
133 return index;
134 }
135
136 @Override
137 int nextChar() {
138 if (index >= length) {
139 throw new IllegalArgumentException();
140 }
141 return string.charAt(index++);
142 }
143
144 @Override
145 char readHex() {
146 if (index + 3 >= length) {
147 throw new IllegalArgumentException();
148 }
149 try {
150 return (char) Integer.parseInt(string.substring(index, index += 4), 16);
151 } catch (NumberFormatException e) {
152 throw new IllegalArgumentException();
153 }
154 }
155
156 @Override
157 public String toString() {
158 return StringUtils.addAsterisk(string, index);
159 }
160
161 }