diff --git a/build.gradle b/build.gradle index 6787a18..58b8fc5 100644 --- a/build.gradle +++ b/build.gradle @@ -60,6 +60,8 @@ dependencies { compile group: 'com.grack', name: 'nanojson', version: '1.2' // jodd compile group: 'org.jodd', name: 'jodd-json', version: '3.8.0' + // moshi + compile group: 'com.squareup.moshi', name: 'moshi', version: '1.3.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 7231489..8162ea9 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java +++ b/src/main/java/com/github/fabienrenaud/jjb/JsonBench.java @@ -78,4 +78,8 @@ public Object jodd() throws Exception { return null; } + public Object moshi() throws Exception { + return null; + } + } diff --git a/src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java b/src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java index 0a98bef..d53bc81 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java +++ b/src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java @@ -56,5 +56,4 @@ protected com.dslplatform.json.JsonWriter initialValue() { } }; - } diff --git a/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java b/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java index 4ddbfd1..885b2e3 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java +++ b/src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java @@ -4,11 +4,9 @@ import com.github.fabienrenaud.jjb.provider.JsonProvider; import com.github.fabienrenaud.jjb.stream.StreamDeserializer; import com.github.fabienrenaud.jjb.stream.StreamSerializer; +import okio.*; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; +import java.io.*; import java.util.Random; /** @@ -94,6 +92,14 @@ public Reader nextReader() { return new InputStreamReader(nextInputStream()); } + public BufferedSource nextOkioBufferedSource() { + return Okio.buffer(new ForwardingSource(Okio.source(nextInputStream())) { + @Override + public void close() throws IOException { + } + }); + } + public T nextPojo() { return jsonAsObject[index(jsonAsObject.length)]; } diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java index 7376ef2..ae5845c 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java @@ -86,4 +86,10 @@ public Object logansquare() throws Exception { public Object jodd() throws Exception { return JSON_SOURCE.provider().joddDeser().parse(JSON_SOURCE.nextString(), JSON_SOURCE.pojoType()); } + + @Benchmark + @Override + public Object moshi() throws Exception { + return JSON_SOURCE.provider().moshi().fromJson(JSON_SOURCE.nextOkioBufferedSource()); + } } diff --git a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java index ebcf46b..baaf04b 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java +++ b/src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java @@ -5,6 +5,8 @@ import com.bluelinelabs.logansquare.LoganSquare; import com.github.fabienrenaud.jjb.JsonBench; import com.github.fabienrenaud.jjb.JsonUtils; +import okio.BufferedSink; +import okio.Okio; import org.openjdk.jmh.annotations.Benchmark; import java.io.ByteArrayOutputStream; @@ -106,4 +108,14 @@ public Object logansquare() throws Exception { public Object jodd() throws Exception { return JSON_SOURCE.provider().joddSer().serialize(JSON_SOURCE.nextPojo()); } + + @Benchmark + @Override + public Object moshi() throws Exception { + ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream(); + BufferedSink sink = Okio.buffer(Okio.sink(baos)); + JSON_SOURCE.provider().moshi().toJson(sink, JSON_SOURCE.nextPojo()); + sink.flush(); + return baos; + } } diff --git a/src/main/java/com/github/fabienrenaud/jjb/provider/JsonProvider.java b/src/main/java/com/github/fabienrenaud/jjb/provider/JsonProvider.java index d70ef15..84a99b2 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/provider/JsonProvider.java +++ b/src/main/java/com/github/fabienrenaud/jjb/provider/JsonProvider.java @@ -41,4 +41,6 @@ public interface JsonProvider { jodd.json.JsonParser joddDeser(); jodd.json.JsonSerializer joddSer(); + + com.squareup.moshi.JsonAdapter moshi(); } diff --git a/src/main/java/com/github/fabienrenaud/jjb/provider/UsersJsonProvider.java b/src/main/java/com/github/fabienrenaud/jjb/provider/UsersJsonProvider.java index 5d02bd4..00c0ac7 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/provider/UsersJsonProvider.java +++ b/src/main/java/com/github/fabienrenaud/jjb/provider/UsersJsonProvider.java @@ -9,6 +9,7 @@ import com.github.fabienrenaud.jjb.model.Users; import com.google.gson.Gson; import com.owlike.genson.Genson; +import com.squareup.moshi.Moshi; import flexjson.JSONDeserializer; import flexjson.JSONSerializer; import org.apache.johnzon.mapper.Mapper; @@ -29,6 +30,7 @@ public class UsersJsonProvider implements JsonProvider { private final JSONDeserializer flexjsonDeser = new JSONDeserializer<>(); private final org.boon.json.ObjectMapper boon = org.boon.json.JsonFactory.create(); private final org.apache.johnzon.mapper.Mapper johnson; + private final com.squareup.moshi.JsonAdapter moshi = new Moshi.Builder().build().adapter(Users.class); /* * DSL-json @@ -112,6 +114,11 @@ public jodd.json.JsonSerializer joddSer() { return JODD_SER.get(); } + @Override + public com.squareup.moshi.JsonAdapter moshi() { + return moshi; + } + private static final ThreadLocal FLEXJSON_SER = new ThreadLocal() { @Override protected JSONSerializer initialValue() { 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 8145956..7db89cc 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java +++ b/src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java @@ -26,7 +26,8 @@ public enum BenchSupport { new Libapi(Library.LOGANSQUARE, Api.DATABIND), new Libapi(Library.JSONSIMPLE, Api.STREAM), new Libapi(Library.NANOJSON, Api.STREAM), - new Libapi(Library.JODD, Api.DATABIND) + new Libapi(Library.JODD, Api.DATABIND), + new Libapi(Library.MOSHI, Api.DATABIND) ); 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 e95e597..c3f1430 100644 --- a/src/main/java/com/github/fabienrenaud/jjb/support/Library.java +++ b/src/main/java/com/github/fabienrenaud/jjb/support/Library.java @@ -23,7 +23,8 @@ public enum Library { LOGANSQUARE, JSONSIMPLE, NANOJSON, - JODD; + JODD, + MOSHI; 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 390a520..3fc9c0d 100644 --- a/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java +++ b/src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java @@ -176,4 +176,11 @@ public void jodd() throws Exception { test(Library.JODD, BENCH.jodd()); } } + + @Test + public void moshi() throws Exception { + for (int i = 0; i < ITERATIONS; i++) { + test(Library.MOSHI, BENCH.moshi()); + } + } }