-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathJSONParser.java
303 lines (264 loc) · 8.11 KB
/
JSONParser.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
package net.minidev.json.parser;
/*
* Copyright 2011-2024 JSON-SMART authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.InputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import net.minidev.json.JSONValue;
import net.minidev.json.writer.JsonReaderI;
public class JSONParser {
/** allow simple quote as String quoting char */
public static final int ACCEPT_SIMPLE_QUOTE = 1;
/** allow non quoted test */
public static final int ACCEPT_NON_QUOTE = 2;
/** Parse NaN as Float.NaN */
public static final int ACCEPT_NAN = 4;
/** Ignore control char in input text. */
public static final int IGNORE_CONTROL_CHAR = 8;
/**
* Use int datatype to store number when it's possible.
*
* @since 1.0.7
*/
public static final int USE_INTEGER_STORAGE = 16;
/**
* Throws exception on excessive 0 leading in digits
*
* @since 1.0.7
*/
public static final int ACCEPT_LEADING_ZERO = 32;
/**
* Throws exception on useless comma in object and array
*
* @since 1.0.8
*/
public static final int ACCEPT_USELESS_COMMA = 64;
/**
* Allow Json-smart to use Double or BigDecimal to store floating point value
*
* <p>You may need to disable HI_PRECISION_FLOAT feature on 32bit to improve parsing performances.
*
* @since 1.0.9
*/
public static final int USE_HI_PRECISION_FLOAT = 128;
/**
* If enabled json-smart will throws exception if datas are present after the end of the Json
* data.
*
* @since 1.0.9-2
*/
public static final int ACCEPT_TAILLING_DATA = 256;
/**
* smart mode, fastest parsing mode. accept lots of non standard json syntax
*
* @since 2.0.1
*/
public static final int ACCEPT_TAILLING_SPACE = 512;
/**
* smart mode, fastest parsing mode. accept lots of non standard json syntax
*
* @since 2.2.2
*/
public static final int REJECT_127_CHAR = 1024;
/**
* Use double if possible for big digits, if no precision lost is observed
*
* @since 2.4
*/
public static final int BIG_DIGIT_UNRESTRICTED = 2048;
/**
* If limit the max depth of json size
*
* @since 2.5
*/
public static final int LIMIT_JSON_DEPTH = 4096;
/**
* smart mode, fastest parsing mode. accept lots of non standard json syntax
*
* @since 1.0.6
*/
public static final int MODE_PERMISSIVE = -1;
/**
* strict RFC4627 mode.
*
* <p>slower than PERMISSIVE MODE.
*
* @since 1.0.6
*/
public static final int MODE_RFC4627 =
USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | ACCEPT_TAILLING_SPACE | LIMIT_JSON_DEPTH;
/**
* Parse Object like json-simple
*
* <p>Best for an iso-bug json-simple API port.
*
* @since 1.0.7
*/
public static final int MODE_JSON_SIMPLE =
ACCEPT_USELESS_COMMA
| USE_HI_PRECISION_FLOAT
| ACCEPT_TAILLING_DATA
| ACCEPT_TAILLING_SPACE
| REJECT_127_CHAR
| BIG_DIGIT_UNRESTRICTED
| LIMIT_JSON_DEPTH;
/**
* Strictest parsing mode
*
* @since 2.0.1
*/
public static final int MODE_STRICTEST =
USE_INTEGER_STORAGE | USE_HI_PRECISION_FLOAT | REJECT_127_CHAR | LIMIT_JSON_DEPTH;
/** Default json-smart processing mode */
public static int DEFAULT_PERMISSIVE_MODE =
(System.getProperty("JSON_SMART_SIMPLE") != null) ? MODE_JSON_SIMPLE : MODE_PERMISSIVE;
/*
* internal fields
*/
private final int mode;
private JSONParserInputStream pBinStream;
private JSONParserByteArray pBytes;
private JSONParserReader pStream;
private JSONParserString pString;
private JSONParserReader getPStream() {
if (pStream == null) pStream = new JSONParserReader(mode);
return pStream;
}
/**
* cached constructor
*
* @return instance of JSONParserInputStream
*/
private JSONParserInputStream getPBinStream() {
if (pBinStream == null) pBinStream = new JSONParserInputStream(mode);
return pBinStream;
}
/**
* cached constructor
*
* @return instance of JSONParserString
*/
private JSONParserString getPString() {
if (pString == null) pString = new JSONParserString(mode);
return pString;
}
/**
* cached constructor
*
* @return instance of JSONParserByteArray
*/
private JSONParserByteArray getPBytes() {
if (pBytes == null) pBytes = new JSONParserByteArray(mode);
return pBytes;
}
/**
* @deprecated prefer usage of new JSONParser(JSONParser.MODE_*)
*/
public JSONParser() {
this.mode = DEFAULT_PERMISSIVE_MODE;
}
public JSONParser(int permissifMode) {
this.mode = permissifMode;
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public Object parse(byte[] in) throws ParseException {
return getPBytes().parse(in);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(byte[] in, JsonReaderI<T> mapper) throws ParseException {
return getPBytes().parse(in, mapper);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(byte[] in, Class<T> mapTo) throws ParseException {
return getPBytes().parse(in, JSONValue.defaultReader.getMapper(mapTo));
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*
* @throws UnsupportedEncodingException
*/
public Object parse(InputStream in) throws ParseException, UnsupportedEncodingException {
return getPBinStream().parse(in);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(InputStream in, JsonReaderI<T> mapper)
throws ParseException, UnsupportedEncodingException {
return getPBinStream().parse(in, mapper);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(InputStream in, Class<T> mapTo)
throws ParseException, UnsupportedEncodingException {
return getPBinStream().parse(in, JSONValue.defaultReader.getMapper(mapTo));
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public Object parse(Reader in) throws ParseException {
return getPStream().parse(in);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(Reader in, JsonReaderI<T> mapper) throws ParseException {
return getPStream().parse(in, mapper);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(Reader in, Class<T> mapTo) throws ParseException {
return getPStream().parse(in, JSONValue.defaultReader.getMapper(mapTo));
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public Object parse(String in) throws ParseException {
return getPString().parse(in);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(String in, JsonReaderI<T> mapper) throws ParseException {
return getPString().parse(in, mapper);
}
/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parse(String in, Class<T> mapTo) throws ParseException {
return getPString().parse(in, JSONValue.defaultReader.getMapper(mapTo));
}
}