add toCollection() methods, update benchmarks
This commit is contained in:
parent
07673324ca
commit
b59a36719b
5 changed files with 56 additions and 67 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Object> 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<Object>) parser.getResult();
|
||||
}
|
||||
throw new JsonException("unexpected, Json.toCollection got not a collection instance: " + object.getClass());
|
||||
}
|
||||
|
||||
public static Collection<Object> toCollection(Reader reader) throws IOException {
|
||||
Objects.requireNonNull(reader);
|
||||
StringWriter writer = new StringWriter();
|
||||
reader.transferTo(writer);
|
||||
return toCollection(writer.toString());
|
||||
}
|
||||
|
||||
public static Collection<Object> 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();
|
||||
|
|
|
@ -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<String, Object> toMap(Node<?> root) {
|
||||
return (Map<String, Object>) internalMap(root);
|
||||
return (Map<String, Object>) internalObject(root);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> toModifiableMap(Node<?> root) {
|
||||
return (Map<String, Object>) internalModifiableMap(root);
|
||||
return (Map<String, Object>) internalModifiableObject(root);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Collection<Object> toCollection(Node<?> root) {
|
||||
return (Collection<Object>) internalObject(root);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Collection<Object> toModifiableCollection(Node<?> root) {
|
||||
return (Collection<Object>) 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<String, Object> map = TinyMap.builder();
|
||||
for (Map.Entry<CharSequence, Node<?>> 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<Object> 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<String, Object> map = TinyMap.builder();
|
||||
for (Map.Entry<CharSequence, Node<?>> 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<Object> list = TinyList.builder();
|
||||
for (Node<?> node : listNode.get()) {
|
||||
list.add(internalMap(node));
|
||||
list.add(internalModifiableObject(node));
|
||||
}
|
||||
return list;
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue