fix bug in json tiny stream parser

This commit is contained in:
Jörg Prante 2021-12-10 15:01:50 +01:00
parent b59a36719b
commit bfd3b0eded
7 changed files with 23 additions and 12 deletions

View file

@ -166,8 +166,8 @@ public class StreamParser implements Parser {
escaped = true;
ch = reader.read();
if (ch == '"' || ch == '/' || ch == '\\' || ch == 'b' || ch == 'f' || ch == 'n' || ch == 'r' || ch == 't') {
count++;
ch = reader.read();
count += 2;
} else if (ch == 'u') {
expectHex();
expectHex();

View file

@ -32,11 +32,15 @@ public class StringParser {
private char ch;
public StringParser() {
this(new TinyJsonListener());
}
public StringParser(JsonResult listener) {
this.listener = listener;
}
public void parse(String input) throws JsonException {
public Node<?> parse(String input) throws JsonException {
Objects.requireNonNull(input);
Objects.requireNonNull(listener);
this.input = input;
@ -50,6 +54,7 @@ public class StringParser {
throw new JsonException("malformed json: " + ch);
}
listener.end();
return listener.getResult();
}
public Node<?> getNode() {

View file

@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import org.xbib.datastructures.json.tiny.Json;
import org.xbib.datastructures.json.tiny.JsonBuilder;
import org.xbib.datastructures.json.tiny.StreamParser;
import org.xbib.datastructures.json.tiny.StringParser;
import java.io.IOException;
import java.io.StringReader;
@ -57,6 +58,8 @@ public class JsonBuilderTest {
assertEquals("[\"a\",\"b\",\"c\"]", s);
StreamParser streamParser = new StreamParser();
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

View file

@ -2,7 +2,6 @@ package org.xbib.datastructures.json.tiny.test;
import org.junit.jupiter.api.Disabled;
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.StringParser;
import java.io.BufferedReader;
@ -27,7 +26,7 @@ public class LargeFileTest {
try (inputStream) {
byte[] b = inputStream.readAllBytes();
String string = new String(b, StandardCharsets.UTF_8);
StringParser stringParser = new StringParser(new TinyJsonListener());
StringParser stringParser = new StringParser();
stringParser.parse(string);
stringParser.getNode().get();
stringParser.parse(string);
@ -41,7 +40,7 @@ public class LargeFileTest {
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
if (inputStream != null) {
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());
}
}

View file

@ -1,7 +1,6 @@
package org.xbib.datastructures.json.tiny.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.StringParser;
import java.io.BufferedReader;
@ -20,9 +19,10 @@ public class ParserTest {
if (inputStream != null) {
byte[] b = inputStream.readAllBytes();
String string = new String(b, StandardCharsets.UTF_8);
StringParser stringParser = new StringParser(new TinyJsonListener());
StringParser stringParser = new StringParser();
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());
}
}
@ -33,8 +33,10 @@ public class ParserTest {
InputStream inputStream = ParserTest.class.getResourceAsStream("/org/xbib/datastructures/json/tiny/test/test.json");
if (inputStream != null) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
StreamParser streamParser = new StreamParser(new TinyJsonListener());
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());
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, z=This is \"quoted\" text\n" +
"after line break}",
streamParser.parse(reader).get().toString());
}
}
}

View file

@ -19,7 +19,8 @@ public class PlainParserTest {
String string = new String(b, StandardCharsets.UTF_8);
PlainParser parser = new PlainParser();
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());
}
}

View file

@ -20,5 +20,6 @@
"v": -2.1,
"w": "",
"x": "\u20AB",
"y": "Jörg"
"y": "Jörg",
"z": "This is \"quoted\" text\nafter line break"
}