fix bug in json tiny stream parser
This commit is contained in:
parent
b59a36719b
commit
bfd3b0eded
7 changed files with 23 additions and 12 deletions
|
@ -166,8 +166,8 @@ public class StreamParser implements Parser {
|
||||||
escaped = true;
|
escaped = true;
|
||||||
ch = reader.read();
|
ch = reader.read();
|
||||||
if (ch == '"' || ch == '/' || ch == '\\' || ch == 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't') {
|
if (ch == '"' || ch == '/' || ch == '\\' || ch == 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't') {
|
||||||
count++;
|
|
||||||
ch = reader.read();
|
ch = reader.read();
|
||||||
|
count += 2;
|
||||||
} else if (ch == 'u') {
|
} else if (ch == 'u') {
|
||||||
expectHex();
|
expectHex();
|
||||||
expectHex();
|
expectHex();
|
||||||
|
|
|
@ -32,11 +32,15 @@ public class StringParser {
|
||||||
|
|
||||||
private char ch;
|
private char ch;
|
||||||
|
|
||||||
|
public StringParser() {
|
||||||
|
this(new TinyJsonListener());
|
||||||
|
}
|
||||||
|
|
||||||
public StringParser(JsonResult listener) {
|
public StringParser(JsonResult listener) {
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void parse(String input) throws JsonException {
|
public Node<?> parse(String input) throws JsonException {
|
||||||
Objects.requireNonNull(input);
|
Objects.requireNonNull(input);
|
||||||
Objects.requireNonNull(listener);
|
Objects.requireNonNull(listener);
|
||||||
this.input = input;
|
this.input = input;
|
||||||
|
@ -50,6 +54,7 @@ public class StringParser {
|
||||||
throw new JsonException("malformed json: " + ch);
|
throw new JsonException("malformed json: " + ch);
|
||||||
}
|
}
|
||||||
listener.end();
|
listener.end();
|
||||||
|
return listener.getResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node<?> getNode() {
|
public Node<?> getNode() {
|
||||||
|
|
|
@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.datastructures.json.tiny.Json;
|
import org.xbib.datastructures.json.tiny.Json;
|
||||||
import org.xbib.datastructures.json.tiny.JsonBuilder;
|
import org.xbib.datastructures.json.tiny.JsonBuilder;
|
||||||
import org.xbib.datastructures.json.tiny.StreamParser;
|
import org.xbib.datastructures.json.tiny.StreamParser;
|
||||||
|
import org.xbib.datastructures.json.tiny.StringParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
@ -57,6 +58,8 @@ public class JsonBuilderTest {
|
||||||
assertEquals("[\"a\",\"b\",\"c\"]", s);
|
assertEquals("[\"a\",\"b\",\"c\"]", s);
|
||||||
StreamParser streamParser = new StreamParser();
|
StreamParser streamParser = new StreamParser();
|
||||||
assertEquals("[a, b, c]", streamParser.parse(new StringReader(s)).get().toString());
|
assertEquals("[a, b, c]", streamParser.parse(new StringReader(s)).get().toString());
|
||||||
|
StringParser stringParser = new StringParser();
|
||||||
|
assertEquals("[a, b, c]", stringParser.parse(s).get().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.xbib.datastructures.json.tiny.test;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled;
|
import org.junit.jupiter.api.Disabled;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.datastructures.json.tiny.TinyJsonListener;
|
|
||||||
import org.xbib.datastructures.json.tiny.StreamParser;
|
import org.xbib.datastructures.json.tiny.StreamParser;
|
||||||
import org.xbib.datastructures.json.tiny.StringParser;
|
import org.xbib.datastructures.json.tiny.StringParser;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -27,7 +26,7 @@ public class LargeFileTest {
|
||||||
try (inputStream) {
|
try (inputStream) {
|
||||||
byte[] b = inputStream.readAllBytes();
|
byte[] b = inputStream.readAllBytes();
|
||||||
String string = new String(b, StandardCharsets.UTF_8);
|
String string = new String(b, StandardCharsets.UTF_8);
|
||||||
StringParser stringParser = new StringParser(new TinyJsonListener());
|
StringParser stringParser = new StringParser();
|
||||||
stringParser.parse(string);
|
stringParser.parse(string);
|
||||||
stringParser.getNode().get();
|
stringParser.getNode().get();
|
||||||
stringParser.parse(string);
|
stringParser.parse(string);
|
||||||
|
@ -41,7 +40,7 @@ public class LargeFileTest {
|
||||||
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
|
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||||
StreamParser streamParser = new StreamParser(new TinyJsonListener());
|
StreamParser streamParser = new StreamParser();
|
||||||
Logger.getLogger("").log(Level.INFO, streamParser.parse(reader).get().toString());
|
Logger.getLogger("").log(Level.INFO, streamParser.parse(reader).get().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package org.xbib.datastructures.json.tiny.test;
|
package org.xbib.datastructures.json.tiny.test;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.datastructures.json.tiny.TinyJsonListener;
|
|
||||||
import org.xbib.datastructures.json.tiny.StreamParser;
|
import org.xbib.datastructures.json.tiny.StreamParser;
|
||||||
import org.xbib.datastructures.json.tiny.StringParser;
|
import org.xbib.datastructures.json.tiny.StringParser;
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
@ -20,9 +19,10 @@ public class ParserTest {
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
byte[] b = inputStream.readAllBytes();
|
byte[] b = inputStream.readAllBytes();
|
||||||
String string = new String(b, StandardCharsets.UTF_8);
|
String string = new String(b, StandardCharsets.UTF_8);
|
||||||
StringParser stringParser = new StringParser(new TinyJsonListener());
|
StringParser stringParser = new StringParser();
|
||||||
stringParser.parse(string);
|
stringParser.parse(string);
|
||||||
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg}",
|
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg, z=This is \"quoted\" text\n" +
|
||||||
|
"after line break}",
|
||||||
stringParser.getNode().get().toString());
|
stringParser.getNode().get().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,8 +33,10 @@ public class ParserTest {
|
||||||
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
|
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
|
||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
|
||||||
StreamParser streamParser = new StreamParser(new TinyJsonListener());
|
StreamParser streamParser = new StreamParser();
|
||||||
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg}", streamParser.parse(reader).get().toString());
|
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg, z=This is \"quoted\" text\n" +
|
||||||
|
"after line break}",
|
||||||
|
streamParser.parse(reader).get().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,8 @@ public class PlainParserTest {
|
||||||
String string = new String(b, StandardCharsets.UTF_8);
|
String string = new String(b, StandardCharsets.UTF_8);
|
||||||
PlainParser parser = new PlainParser();
|
PlainParser parser = new PlainParser();
|
||||||
parser.parse(string);
|
parser.parse(string);
|
||||||
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg}",
|
assertEquals("{a=b, c=d, e=[f, g], h={i={j=k}}, l=null, m=true, n=false, o=0, p=1, q=-1, r=0.0, s=1.0, t=2.1, u=-1.0, v=-2.1, w=, x=₫, y=Jörg, z=This is \"quoted\" text\n" +
|
||||||
|
"after line break}",
|
||||||
parser.getResult().toString());
|
parser.getResult().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,5 +20,6 @@
|
||||||
"v": -2.1,
|
"v": -2.1,
|
||||||
"w": "",
|
"w": "",
|
||||||
"x": "\u20AB",
|
"x": "\u20AB",
|
||||||
"y": "Jörg"
|
"y": "Jörg",
|
||||||
|
"z": "This is \"quoted\" text\nafter line break"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue