Skip to content

Commit 5ad0031

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

File tree

9 files changed

+391
-51
lines changed

9 files changed

+391
-51
lines changed

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

Lines changed: 23 additions & 2 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
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
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();
@@ -594,7 +613,7 @@ protected <T> T readObject(JsonReaderI<T> mapper) throws ParseException, IOExcep
594613
return mapper.convert(current);
595614
}
596615
if (c == EOI) // Fixed on 18/10/2011 reported by vladimir
597-
throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, null);
616+
throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, null);
598617
// if c==, continue
599618
if (c == ',') acceptData = needData = true;
600619
else throw new ParseException(pos - 1, ERROR_UNEXPECTED_TOKEN, c);
@@ -603,7 +622,9 @@ protected <T> T readObject(JsonReaderI<T> mapper) throws ParseException, IOExcep
603622
}
604623
}
605624

606-
/** store and read */
625+
/**
626+
* store and read
627+
*/
607628
abstract void readS() throws IOException;
608629

609630
protected abstract void readString() throws ParseException, IOException;

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

0 commit comments

Comments
 (0)