diff --git a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StreamParser.java b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StreamParser.java index b1eff71..8e80960 100644 --- a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StreamParser.java +++ b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StreamParser.java @@ -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(); diff --git a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StringParser.java b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StringParser.java index 6f9284c..983338c 100644 --- a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StringParser.java +++ b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/StringParser.java @@ -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() { diff --git a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/JsonBuilderTest.java b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/JsonBuilderTest.java index 6b5cc3f..c32f2ab 100644 --- a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/JsonBuilderTest.java +++ b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/JsonBuilderTest.java @@ -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 diff --git a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/LargeFileTest.java b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/LargeFileTest.java index fade4c6..611b8ea 100644 --- a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/LargeFileTest.java +++ b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/LargeFileTest.java @@ -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()); } } diff --git a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/ParserTest.java b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/ParserTest.java index f88905e..6cb2610 100644 --- a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/ParserTest.java +++ b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/ParserTest.java @@ -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()); } } } diff --git a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/PlainParserTest.java b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/PlainParserTest.java index d405775..5d5f8d2 100644 --- a/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/PlainParserTest.java +++ b/datastructures-json-tiny/src/test/java/org/xbib/datastructures/json/tiny/test/PlainParserTest.java @@ -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()); } } diff --git a/datastructures-json-tiny/src/test/resources/org/xbib/datastructures/json/tiny/test/test.json b/datastructures-json-tiny/src/test/resources/org/xbib/datastructures/json/tiny/test/test.json index 25178d9..7f13dd8 100644 --- a/datastructures-json-tiny/src/test/resources/org/xbib/datastructures/json/tiny/test/test.json +++ b/datastructures-json-tiny/src/test/resources/org/xbib/datastructures/json/tiny/test/test.json @@ -20,5 +20,6 @@ "v": -2.1, "w": "", "x": "\u20AB", - "y": "Jörg" + "y": "Jörg", + "z": "This is \"quoted\" text\nafter line break" }