From 1e1e559c39a6eddc3dd7d7cea777fc7861415469 Mon Sep 17 00:00:00 2001 From: Fabien Renaud Date: Sat, 5 Nov 2016 10:16:04 -0700 Subject: [PATCH] json-simple --- README.md | 1 + build.gradle | 2 + .../github/fabienrenaud/jjb/JsonBench.java | 4 + .../jjb/stream/Deserialization.java | 5 ++ .../jjb/stream/Serialization.java | 12 +++ .../jjb/stream/StreamSerializer.java | 2 + .../jjb/stream/UsersStreamSerializer.java | 87 ++++++++++++++++++- .../jjb/support/BenchSupport.java | 3 +- .../fabienrenaud/jjb/support/Library.java | 3 +- .../fabienrenaud/jjb/JsonBenchmark.java | 7 ++ 10 files changed, 123 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index fe12568..eb2f4e0 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,7 @@ The following libraries are evaluated: * [johnzon](http://johnzon.apache.org/) * [logansquare](https://github.com/bluelinelabs/LoganSquare) * [dsl-json](https://github.com/ngs-doo/dsl-json) +* [json-simple](https://code.google.com/archive/p/json-simple/) This benchmark tests throughput performance of serialization and deserialization algorithms of the databind and stream API when available. Random payloads of various sizes are generated at runtime before each benchmark. diff --git a/build.gradle b/build.gradle index 5cdae7a..1221c54 100644 --- a/build.gradle +++ b/build.gradle @@ -54,6 +54,8 @@ dependencies { // LoganSquare compile group: 'com.bluelinelabs', name: 'logansquare', version: '1.3.7' apt group: 'com.bluelinelabs', name: 'logansquare-compiler', version: '1.3.7' + // json-simple + compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' // Test testCompile group: 'junit', name: 'junit', version: '4.12' diff --git a/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java b/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java index d39959f..dad9f45 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java +++ b/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java @@ -66,4 +66,8 @@ public Object logansquare() throws Exception { return null; } + public Object jsonsimple() throws Exception { + return null; + } + } diff --git a/src/main/java/com/github/fabienrenaud/jjb/stream/Deserialization.java b/src/main/java/com/github/fabienrenaud/jjb/stream/Deserialization.java index 206dbfe..aaa67aa 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/stream/Deserialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/stream/Deserialization.java @@ -57,4 +57,9 @@ public Object jsonio() throws Exception { return com.cedarsoftware.util.io.JsonReader.jsonToJava(JSON_SOURCE.nextInputStream(), JSON_SOURCE.provider().jsonioStreamOptions()); } + @Benchmark + @Override + public Object jsonsimple() throws Exception { + return org.json.simple.JSONValue.parse(JSON_SOURCE.nextReader()); + } } diff --git a/src/main/java/com/github/fabienrenaud/jjb/stream/Serialization.java b/src/main/java/com/github/fabienrenaud/jjb/stream/Serialization.java index 731aac8..9eef5b1 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/stream/Serialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/stream/Serialization.java @@ -68,4 +68,16 @@ public Object genson() throws Exception { public Object jsonio() throws Exception { return com.cedarsoftware.util.io.JsonWriter.objectToJson(JSON_SOURCE.nextPojo(), JSON_SOURCE.provider().jsonioStreamOptions()); } + + @Benchmark + @Override + public Object jsonsimple() throws Exception { + org.json.simple.JSONObject jso = JSON_SOURCE.streamSerializer().jsonsimple(JSON_SOURCE.nextPojo()); + + ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); + Writer w = new OutputStreamWriter(baos); + org.json.simple.JSONValue.writeJSONString(jso, w); + w.close(); + return baos; + } } diff --git a/src/main/java/com/github/fabienrenaud/jjb/stream/StreamSerializer.java b/src/main/java/com/github/fabienrenaud/jjb/stream/StreamSerializer.java index 9bcaded..332316c 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/stream/StreamSerializer.java +++ b/src/main/java/com/github/fabienrenaud/jjb/stream/StreamSerializer.java @@ -16,4 +16,6 @@ public interface StreamSerializer { void gson(final JsonWriter j, final T obj) throws IOException; void jackson(final JsonGenerator j, final T obj) throws IOException; + + org.json.simple.JSONObject jsonsimple(final T obj) throws IOException; } diff --git a/src/main/java/com/github/fabienrenaud/jjb/stream/UsersStreamSerializer.java b/src/main/java/com/github/fabienrenaud/jjb/stream/UsersStreamSerializer.java index eadd747..08396a1 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/stream/UsersStreamSerializer.java +++ b/src/main/java/com/github/fabienrenaud/jjb/stream/UsersStreamSerializer.java @@ -2,8 +2,8 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.github.fabienrenaud.jjb.model.Users; -import com.github.fabienrenaud.jjb.model.Users.User; import com.github.fabienrenaud.jjb.model.Users.Friend; +import com.github.fabienrenaud.jjb.model.Users.User; import com.google.gson.stream.JsonWriter; import com.owlike.genson.stream.ObjectWriter; @@ -321,4 +321,89 @@ private void jackson(final JsonGenerator j, final User u) throws IOException { } j.writeEndObject(); } + + @Override + public org.json.simple.JSONObject jsonsimple(final Users obj) throws IOException { + org.json.simple.JSONObject jso = new org.json.simple.JSONObject(); + if (obj.users != null) { + org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray(); + for (User u : obj.users) { + jsarr.add(jsonsimple(u)); + } + jso.put("users", jsarr); + } + return jso; + } + + private org.json.simple.JSONObject jsonsimple(final User u) throws IOException { + org.json.simple.JSONObject jso = new org.json.simple.JSONObject(); + if (u._id != null) { + jso.put("_id", u._id); + } + jso.put("index", u.index); + if (u.guid != null) { + jso.put("guid", u.guid); + } + jso.put("isActive", u.isActive); + if (u.balance != null) { + jso.put("balance", u.balance); + } + if (u.picture != null) { + jso.put("picture", u.picture); + } + jso.put("age", u.age); + if (u.eyeColor != null) { + jso.put("eyeColor", u.eyeColor); + } + if (u.name != null) { + jso.put("name", u.name); + } + if (u.gender != null) { + jso.put("gender", u.gender); + } + if (u.company != null) { + jso.put("company", u.company); + } + if (u.email != null) { + jso.put("email", u.email); + } + if (u.phone != null) { + jso.put("phone", u.phone); + } + if (u.address != null) { + jso.put("address", u.address); + } + if (u.about != null) { + jso.put("about", u.about); + } + if (u.registered != null) { + jso.put("registered", u.registered); + } + jso.put("latitude", u.latitude); + jso.put("longitude", u.longitude); + if (u.tags != null) { + org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray(); + for (String t : u.tags) { + jsarr.add(t); + } + jso.put("tags", jsarr); + } + if (u.friends != null) { + org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray(); + for (Friend f : u.friends) { + org.json.simple.JSONObject jso0 = new org.json.simple.JSONObject(); + jso0.put("id", f.id); + jso0.put("name", f.name); + jsarr.add(jso0); + } + jso.put("friends", jsarr); + } + if (u.greeting != null) { + jso.put("greeting", u.greeting); + } + if (u.favoriteFruit != null) { + jso.put("favoriteFruit", u.favoriteFruit); + } + return jso; + } } diff --git a/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java b/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java index 96f95bb..4ea3568 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java +++ b/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java @@ -23,7 +23,8 @@ public enum BenchSupport { new Libapi(Library.JOHNZON, Api.DATABIND), new Libapi(Library.JSONSMART, Api.DATABIND), new Libapi(Library.DSLJSON, Api.DATABIND), - new Libapi(Library.LOGANSQUARE, Api.DATABIND) + new Libapi(Library.LOGANSQUARE, Api.DATABIND), + new Libapi(Library.JSONSIMPLE, Api.STREAM) ); private final List libapis; diff --git a/src/main/java/com/github/fabienrenaud/jjb/support/Library.java b/src/main/java/com/github/fabienrenaud/jjb/support/Library.java index f359f20..900ae9f 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/support/Library.java +++ b/src/main/java/com/github/fabienrenaud/jjb/support/Library.java @@ -20,7 +20,8 @@ public enum Library { JOHNZON, JSONSMART, DSLJSON, - LOGANSQUARE; + LOGANSQUARE, + JSONSIMPLE; public static Set fromCsv(String str) { if (str == null || str.trim().isEmpty()) { diff --git a/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java b/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java index da86fe3..a197a30 100644 --- a/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java +++ b/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java @@ -152,4 +152,11 @@ public void logansquare() throws Exception { test(Library.LOGANSQUARE, BENCH.logansquare()); } } + + @Test + public void jsonsimple() throws Exception { + for (int i = 0; i < ITERATIONS; i++) { + test(Library.JSONSIMPLE, BENCH.jsonsimple()); + } + } }