Skip to content

Commit cd08a43

Browse files
author
QIURC
committed
Support reading multiple JSON syntax blocks in a single input (#269)
1 parent d2e3b3c commit cd08a43

File tree

9 files changed

+391
-48
lines changed

9 files changed

+391
-48
lines changed

json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,31 @@ private boolean compareDoublePrecision(String convert, String origin) {
195195
return j == originArray.length;
196196
}
197197

198+
protected boolean hasNext() {
199+
return c != EOI;
200+
}
201+
198202
/**
203+
* parse from the first position <br>
199204
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
200205
* ContainerFactory
201206
*/
202207
protected <T> T parse(JsonReaderI<T> mapper) throws ParseException {
203208
this.pos = -1;
209+
return parseInner(mapper);
210+
}
211+
212+
/**
213+
* parse from the last position <br>
214+
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
215+
* ContainerFactory
216+
*/
217+
protected <T> T parseNext(JsonReaderI<T> mapper) throws ParseException {
218+
this.pos = this.pos > 0 ? this.pos : -1;
219+
return parseInner(mapper);
220+
}
221+
222+
private <T> T parseInner(JsonReaderI<T> mapper) throws ParseException {
204223
T result;
205224
try {
206225
read();

json-smart/src/main/java/net/minidev/json/parser/JSONParserByteArray.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public JSONParserByteArray(int permissiveMode) {
3333
super(permissiveMode);
3434
}
3535

36+
public JSONParserByteArray(byte[] in, int permissiveMode) {
37+
super(permissiveMode);
38+
this.in = in;
39+
this.len = in.length;
40+
}
41+
3642
/**
3743
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
3844
* ContainerFactory
@@ -41,14 +47,6 @@ public Object parse(byte[] in) throws ParseException {
4147
return parse(in, JSONValue.defaultReader.DEFAULT);
4248
}
4349

44-
//
45-
//
46-
//
47-
//
48-
//
49-
//
50-
//
51-
5250
/**
5351
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
5452
* ContainerFactory

json-smart/src/main/java/net/minidev/json/parser/JSONParserInputStream.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public JSONParserInputStream(int permissiveMode) {
3333
super(permissiveMode);
3434
}
3535

36+
public JSONParserInputStream(InputStream in, int permissiveMode) {
37+
super(new InputStreamReader(in, StandardCharsets.UTF_8), permissiveMode);
38+
}
39+
3640
/**
3741
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
3842
* ContainerFactory
@@ -54,13 +58,4 @@ public <T> T parse(InputStream in, JsonReaderI<T> mapper)
5458
//
5559
return super.parse(i2, mapper);
5660
}
57-
58-
//
59-
//
60-
//
61-
//
62-
//
63-
//
64-
//
65-
//
6661
}

json-smart/src/main/java/net/minidev/json/parser/JSONParserReader.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public JSONParserReader(int permissiveMode) {
3535
super(permissiveMode);
3636
}
3737

38+
public JSONParserReader(Reader in, int permissiveMode) {
39+
super(permissiveMode);
40+
this.in = in;
41+
}
42+
3843
/**
3944
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
4045
* ContainerFactory
@@ -54,19 +59,10 @@ public <T> T parse(Reader in, JsonReaderI<T> mapper) throws ParseException {
5459
return super.parse(mapper);
5560
}
5661

57-
//
58-
//
59-
//
60-
//
61-
//
62-
//
63-
//
64-
6562
protected void read() throws IOException {
6663
int i = in.read();
6764
c = (i == -1) ? (char) EOI : (char) i;
6865
pos++;
69-
//
7066
}
7167

7268
protected void readS() throws IOException {
@@ -84,6 +80,5 @@ protected void readNoEnd() throws ParseException, IOException {
8480
int i = in.read();
8581
if (i == -1) throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, "EOF");
8682
c = (char) i;
87-
//
8883
}
8984
}

json-smart/src/main/java/net/minidev/json/parser/JSONParserStream.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
* @see JSONParserReader
2929
*/
3030
abstract class JSONParserStream extends JSONParserBase {
31-
// len
32-
//
3331
public JSONParserStream(int permissiveMode) {
3432
super(permissiveMode);
3533
}
@@ -112,26 +110,8 @@ protected void readString() throws ParseException, IOException {
112110
throw new ParseException(pos, ERROR_UNEXPECTED_CHAR, c);
113111
}
114112
sb.clear();
115-
//
116-
//
117-
//
118-
//
119-
//
120-
//
121-
//
122-
//
123-
//
124-
//
113+
125114
/* assert (c == '\"' || c == '\'') */
126115
readString2();
127116
}
128-
129-
//
130-
//
131-
//
132-
//
133-
//
134-
//
135-
//
136-
//
137117
}

json-smart/src/main/java/net/minidev/json/parser/JSONParserString.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public JSONParserString(int permissiveMode) {
3232
super(permissiveMode);
3333
}
3434

35+
public JSONParserString(String in, int permissiveMode) {
36+
super(permissiveMode);
37+
this.in = in;
38+
this.len = in.length();
39+
}
40+
3541
/**
3642
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
3743
* ContainerFactory
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package net.minidev.json.parser;
2+
3+
import java.io.InputStream;
4+
import java.io.Reader;
5+
import net.minidev.json.JSONValue;
6+
import net.minidev.json.writer.JsonReaderI;
7+
8+
/**
9+
* json-smart will parse multiple json separated by blank or line break character.
10+
*
11+
* <p>multiple json example: <br>
12+
* {"json1": "value1"} {"json2": "value2"} <br>
13+
* or <br>
14+
* [{"json1-key1": "value1"}] [{"json2": "value2"}]
15+
*/
16+
public class MultipleJsonParser {
17+
18+
private final JSONParserBase jsonParser;
19+
20+
public MultipleJsonParser(byte[] in, int permissiveMode) {
21+
this.jsonParser = new JSONParserByteArray(in, permissiveMode);
22+
}
23+
24+
public MultipleJsonParser(InputStream in, int permissiveMode) {
25+
this.jsonParser = new JSONParserInputStream(in, permissiveMode);
26+
}
27+
28+
public MultipleJsonParser(Reader in, int permissiveMode) {
29+
this.jsonParser = new JSONParserReader(in, permissiveMode);
30+
}
31+
32+
public MultipleJsonParser(String in, int permissiveMode) {
33+
this.jsonParser = new JSONParserString(in, permissiveMode);
34+
}
35+
36+
/**
37+
* Parse next json with defaultReader <br>
38+
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
39+
* ContainerFactory
40+
*/
41+
public Object parseNext() throws ParseException {
42+
return jsonParser.parseNext(JSONValue.defaultReader.DEFAULT);
43+
}
44+
45+
/**
46+
* Parse next json with target JsonReaderI <br>
47+
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
48+
* ContainerFactory
49+
*/
50+
public <T> T parseNext(JsonReaderI<T> mapper) throws ParseException {
51+
return this.jsonParser.parseNext(mapper);
52+
}
53+
54+
/** Parse next json with target Class */
55+
public <T> T parseNext(Class<T> mapTo) throws ParseException {
56+
return this.jsonParser.parseNext(JSONValue.defaultReader.getMapper(mapTo));
57+
}
58+
59+
/**
60+
* Checks if there is another JSON value available in the input.
61+
*
62+
* @return true if another JSON value exists, false otherwise
63+
*/
64+
public boolean hasNext() {
65+
return this.jsonParser.hasNext();
66+
}
67+
}

0 commit comments

Comments
 (0)