From b59a36719bee193c3cd64ee043e10d7dc504be60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Fri, 3 Dec 2021 17:18:50 +0100 Subject: [PATCH] add toCollection() methods, update benchmarks --- .../benchmark/JsonLargeBenchmark.java | 22 ++------------ .../benchmark/JsonMediumBenchmark.java | 20 ++----------- .../benchmark/JsonSmallBenchmark.java | 24 ++------------- .../xbib/datastructures/json/tiny/Json.java | 30 +++++++++++++++++++ .../json/tiny/JsonGenerator.java | 27 ++++++++++++----- 5 files changed, 56 insertions(+), 67 deletions(-) diff --git a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonLargeBenchmark.java b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonLargeBenchmark.java index 945f871..28e37b6 100644 --- a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonLargeBenchmark.java +++ b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonLargeBenchmark.java @@ -103,27 +103,11 @@ public class JsonLargeBenchmark { @Benchmark public Object jsondsl() throws IOException { byte[] b = largeInput.getBytes(StandardCharsets.UTF_8); - return dslJson.deserialize(Map.class, b, b.length); + return dslJson.deserialize(List.class, b, b.length); } @Benchmark - public Object datastructuresEmpty() { - StringParser stringParser = new StringParser(new EmptyJsonListener()); - stringParser.parse(largeInput); - return stringParser.getNode(); // there is no object in get() - } - - @Benchmark - public Object datastructuresTiny() { - StringParser stringParser = new StringParser(new TinyJsonListener()); - stringParser.parse(largeInput); - return stringParser.getNode().get().toString(); - } - - @Benchmark - public Object datastructuresStandard() { - StringParser stringParser = new StringParser(new StandardJsonListener()); - stringParser.parse(largeInput); - return stringParser.getNode().get().toString(); + public Object datastructuresJsonTiny() { + return org.xbib.datastructures.json.tiny.Json.toCollection(largeInput).toString(); } } diff --git a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonMediumBenchmark.java b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonMediumBenchmark.java index 76af194..5c623a3 100644 --- a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonMediumBenchmark.java +++ b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonMediumBenchmark.java @@ -106,23 +106,7 @@ public class JsonMediumBenchmark { } @Benchmark - public Object datastructuresEmpty() { - StringParser stringParser = new StringParser(new EmptyJsonListener()); - stringParser.parse(mediumInput); - return stringParser.getNode(); // there is no object in get() - } - - @Benchmark - public Object datastructuresTiny() { - StringParser stringParser = new StringParser(new TinyJsonListener()); - stringParser.parse(mediumInput); - return stringParser.getNode().get().toString(); - } - - @Benchmark - public Object datastructuresStandard() { - StringParser stringParser = new StringParser(new StandardJsonListener()); - stringParser.parse(mediumInput); - return stringParser.getNode().get().toString(); + public Object datastructuresJsonTiny() { + return org.xbib.datastructures.json.tiny.Json.toMap(mediumInput).toString(); } } diff --git a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonSmallBenchmark.java b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonSmallBenchmark.java index 6261a53..7266c5d 100644 --- a/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonSmallBenchmark.java +++ b/benchmark/src/jmh/java/org/xbib/datastructures/benchmark/JsonSmallBenchmark.java @@ -15,10 +15,6 @@ import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.Threads; import org.openjdk.jmh.annotations.Timeout; import org.openjdk.jmh.annotations.Warmup; -import org.xbib.datastructures.json.tiny.EmptyJsonListener; -import org.xbib.datastructures.json.tiny.StandardJsonListener; -import org.xbib.datastructures.json.tiny.StringParser; -import org.xbib.datastructures.json.tiny.TinyJsonListener; import org.xbib.datastructures.json.flat.Json; import org.xbib.datastructures.json.noggit.ObjectBuilder; import org.xbib.datastructures.json.simple.JSONParser; @@ -107,23 +103,7 @@ public class JsonSmallBenchmark { } @Benchmark - public Object datastructuresEmpty() { - StringParser stringParser = new StringParser(new EmptyJsonListener()); - stringParser.parse(smallInput); - return stringParser.getNode(); // there is no object in get() - } - - @Benchmark - public Object datastructuresTiny() { - StringParser stringParser = new StringParser(new TinyJsonListener()); - stringParser.parse(smallInput); - return stringParser.getNode().get().toString(); - } - - @Benchmark - public Object datastructuresStandard() { - StringParser stringParser = new StringParser(new StandardJsonListener()); - stringParser.parse(smallInput); - return stringParser.getNode().get().toString(); + public Object datastructuresJsonTiny() { + return org.xbib.datastructures.json.tiny.Json.toMap(smallInput).toString(); } } diff --git a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/Json.java b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/Json.java index e4d3d57..66c04f3 100644 --- a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/Json.java +++ b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/Json.java @@ -12,6 +12,7 @@ import java.io.IOException; import java.io.Reader; import java.io.StringWriter; import java.time.Instant; +import java.util.Collection; import java.util.Map; import java.util.Objects; import java.util.function.Consumer; @@ -70,6 +71,35 @@ public class Json implements DataStructure { } } + @SuppressWarnings("unchecked") + public static Collection toCollection(String json) { + if (json == null) { + return null; + } + PlainParser parser = new PlainParser(); + parser.parse(json); + Object object = parser.getResult(); + if (object instanceof Collection) { + return (Collection) parser.getResult(); + } + throw new JsonException("unexpected, Json.toCollection got not a collection instance: " + object.getClass()); + } + + public static Collection toCollection(Reader reader) throws IOException { + Objects.requireNonNull(reader); + StringWriter writer = new StringWriter(); + reader.transferTo(writer); + return toCollection(writer.toString()); + } + + public static Collection toModifiableCollection(Reader reader) throws IOException { + Objects.requireNonNull(reader); + // buffered reader is required for mark() support + try (BufferedReader bufferedReader = new BufferedReader(reader)){ + return JsonGenerator.toModifiableCollection(INSTANCE.createParser().parse(bufferedReader)); + } + } + @Override public Parser createParser() { return new StreamParser(); diff --git a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonGenerator.java b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonGenerator.java index b1f820e..b75cd8d 100644 --- a/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonGenerator.java +++ b/datastructures-json-tiny/src/main/java/org/xbib/datastructures/json/tiny/JsonGenerator.java @@ -7,6 +7,7 @@ import org.xbib.datastructures.tiny.TinyMap; import java.io.IOException; import java.io.Writer; +import java.util.Collection; import java.util.Map; public class JsonGenerator implements Generator { @@ -31,12 +32,22 @@ public class JsonGenerator implements Generator { @SuppressWarnings("unchecked") public static Map toMap(Node root) { - return (Map) internalMap(root); + return (Map) internalObject(root); } @SuppressWarnings("unchecked") public static Map toModifiableMap(Node root) { - return (Map) internalModifiableMap(root); + return (Map) internalModifiableObject(root); + } + + @SuppressWarnings("unchecked") + public static Collection toCollection(Node root) { + return (Collection) internalObject(root); + } + + @SuppressWarnings("unchecked") + public static Collection toModifiableCollection(Node root) { + return (Collection) internalModifiableObject(root); } private void internalWrite(Node curnode) throws IOException { @@ -62,7 +73,7 @@ public class JsonGenerator implements Generator { } } - private static Object internalMap(Node curnode) { + private static Object internalObject(Node curnode) { if (curnode instanceof ValueNode) { ValueNode valueNode = (ValueNode) curnode; return valueNode.get(); @@ -70,14 +81,14 @@ public class JsonGenerator implements Generator { MapNode mapNode = (MapNode) curnode; TinyMap.Builder map = TinyMap.builder(); for (Map.Entry> e : mapNode.get().entrySet()) { - map.put(e.getKey().toString(), internalMap(e.getValue())); + map.put(e.getKey().toString(), internalObject(e.getValue())); } return map.build(); } else if (curnode instanceof ListNode) { ListNode listNode = (ListNode) curnode; TinyList.Builder list = TinyList.builder(); for (Node node : listNode.get()) { - list.add(internalMap(node)); + list.add(internalObject(node)); } return list.build(); } else { @@ -85,7 +96,7 @@ public class JsonGenerator implements Generator { } } - private static Object internalModifiableMap(Node curnode) { + private static Object internalModifiableObject(Node curnode) { if (curnode instanceof ValueNode) { ValueNode valueNode = (ValueNode) curnode; return valueNode.get(); @@ -93,14 +104,14 @@ public class JsonGenerator implements Generator { MapNode mapNode = (MapNode) curnode; TinyMap.Builder map = TinyMap.builder(); for (Map.Entry> e : mapNode.get().entrySet()) { - map.put(e.getKey().toString(), internalMap(e.getValue())); + map.put(e.getKey().toString(), internalModifiableObject(e.getValue())); } return map; } else if (curnode instanceof ListNode) { ListNode listNode = (ListNode) curnode; TinyList.Builder list = TinyList.builder(); for (Node node : listNode.get()) { - list.add(internalMap(node)); + list.add(internalModifiableObject(node)); } return list; } else {