Skip to content

Support reading multiple JSON syntax blocks in a single input (#269) #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,31 @@ private boolean compareDoublePrecision(String convert, String origin) {
return j == originArray.length;
}

protected boolean hasNext() {
return c != EOI;
}

/**
* parse from the first position <br>
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
protected <T> T parse(JsonReaderI<T> mapper) throws ParseException {
this.pos = -1;
return parseInner(mapper);
}

/**
* parse from the last position <br>
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
protected <T> T parseNext(JsonReaderI<T> mapper) throws ParseException {
this.pos = this.pos > 0 ? this.pos : -1;
return parseInner(mapper);
}

private <T> T parseInner(JsonReaderI<T> mapper) throws ParseException {
T result;
try {
read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public JSONParserByteArray(int permissiveMode) {
super(permissiveMode);
}

public JSONParserByteArray(byte[] in, int permissiveMode) {
super(permissiveMode);
this.in = in;
this.len = in.length;
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
Expand All @@ -41,14 +47,6 @@ public Object parse(byte[] in) throws ParseException {
return parse(in, JSONValue.defaultReader.DEFAULT);
}

//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important that we maintain the blank lines in this file. They aren't 'empty' code; they're there to synchronize line numbers with similar files. In this pull request, you've added six lines and removed seven, which has broken this synchronization. Please adjust your changes to restore the correct line number offset.

//
//
//
//
//
//

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public JSONParserInputStream(int permissiveMode) {
super(permissiveMode);
}

public JSONParserInputStream(InputStream in, int permissiveMode) {
super(new InputStreamReader(in, StandardCharsets.UTF_8), permissiveMode);
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
Expand All @@ -54,13 +58,4 @@ public <T> T parse(InputStream in, JsonReaderI<T> mapper)
//
return super.parse(i2, mapper);
}

//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double check lines offset ans preserve some empty lines here probably should keep 5 out of 9

//
//
//
//
//
//
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public JSONParserReader(int permissiveMode) {
super(permissiveMode);
}

public JSONParserReader(Reader in, int permissiveMode) {
super(permissiveMode);
this.in = in;
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
Expand All @@ -54,19 +59,10 @@ public <T> T parse(Reader in, JsonReaderI<T> mapper) throws ParseException {
return super.parse(mapper);
}

//
//
//
//
//
//
//

protected void read() throws IOException {
int i = in.read();
c = (i == -1) ? (char) EOI : (char) i;
pos++;
//
}

protected void readS() throws IOException {
Expand All @@ -84,6 +80,5 @@ protected void readNoEnd() throws ParseException, IOException {
int i = in.read();
if (i == -1) throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, "EOF");
c = (char) i;
//
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
* @see JSONParserReader
*/
abstract class JSONParserStream extends JSONParserBase {
// len
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not drop those offset empty lines.

//
public JSONParserStream(int permissiveMode) {
super(permissiveMode);
}
Expand Down Expand Up @@ -112,26 +110,8 @@ protected void readString() throws ParseException, IOException {
throw new ParseException(pos, ERROR_UNEXPECTED_CHAR, c);
}
sb.clear();
//
//
//
//
//
//
//
//
//
//

/* assert (c == '\"' || c == '\'') */
readString2();
}

//
//
//
//
//
//
//
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public JSONParserString(int permissiveMode) {
super(permissiveMode);
}

public JSONParserString(String in, int permissiveMode) {
super(permissiveMode);
this.in = in;
this.len = in.length();
}

/**
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package net.minidev.json.parser;

import java.io.InputStream;
import java.io.Reader;
import net.minidev.json.JSONValue;
import net.minidev.json.writer.JsonReaderI;

/**
* json-smart will parse multiple json separated by blank or line break character.
*
* <p>multiple json example: <br>
* {"json1": "value1"} {"json2": "value2"} <br>
* or <br>
* [{"json1-key1": "value1"}] [{"json2": "value2"}]
*/
public class MultipleJsonParser {

private final JSONParserBase jsonParser;

public MultipleJsonParser(byte[] in, int permissiveMode) {
this.jsonParser = new JSONParserByteArray(in, permissiveMode);
}

public MultipleJsonParser(InputStream in, int permissiveMode) {
this.jsonParser = new JSONParserInputStream(in, permissiveMode);
}

public MultipleJsonParser(Reader in, int permissiveMode) {
this.jsonParser = new JSONParserReader(in, permissiveMode);
}

public MultipleJsonParser(String in, int permissiveMode) {
this.jsonParser = new JSONParserString(in, permissiveMode);
}

/**
* Parse next json with defaultReader <br>
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public Object parseNext() throws ParseException {
return jsonParser.parseNext(JSONValue.defaultReader.DEFAULT);
}

/**
* Parse next json with target JsonReaderI <br>
* use to return Primitive Type, or String, Or JsonObject or JsonArray generated by a
* ContainerFactory
*/
public <T> T parseNext(JsonReaderI<T> mapper) throws ParseException {
return this.jsonParser.parseNext(mapper);
}

/** Parse next json with target Class */
public <T> T parseNext(Class<T> mapTo) throws ParseException {
return this.jsonParser.parseNext(JSONValue.defaultReader.getMapper(mapTo));
}

/**
* Checks if there is another JSON value available in the input.
*
* @return true if another JSON value exists, false otherwise
*/
public boolean hasNext() {
return this.jsonParser.hasNext();
}
}
Loading