Skip to content

Commit 6af2c0a

Browse files
committed
Moshi
1 parent f7f0c11 commit 6af2c0a

File tree

11 files changed

+54
-7
lines changed

11 files changed

+54
-7
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ dependencies {
6060
compile group: 'com.grack', name: 'nanojson', version: '1.2'
6161
// jodd
6262
compile group: 'org.jodd', name: 'jodd-json', version: '3.8.0'
63+
// moshi
64+
compile group: 'com.squareup.moshi', name: 'moshi', version: '1.3.1'
6365

6466
// Test
6567
testCompile group: 'junit', name: 'junit', version: '4.12'

src/main/java/com/github/fabienrenaud/jjb/JsonBench.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ public Object jodd() throws Exception {
7878
return null;
7979
}
8080

81+
public Object moshi() throws Exception {
82+
return null;
83+
}
84+
8185
}

src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,4 @@ protected com.dslplatform.json.JsonWriter initialValue() {
5656
}
5757
};
5858

59-
6059
}

src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import com.github.fabienrenaud.jjb.provider.JsonProvider;
55
import com.github.fabienrenaud.jjb.stream.StreamDeserializer;
66
import com.github.fabienrenaud.jjb.stream.StreamSerializer;
7+
import okio.*;
78

8-
import java.io.ByteArrayInputStream;
9-
import java.io.InputStream;
10-
import java.io.InputStreamReader;
11-
import java.io.Reader;
9+
import java.io.*;
1210
import java.util.Random;
1311

1412
/**
@@ -94,6 +92,14 @@ public Reader nextReader() {
9492
return new InputStreamReader(nextInputStream());
9593
}
9694

95+
public BufferedSource nextOkioBufferedSource() {
96+
return Okio.buffer(new ForwardingSource(Okio.source(nextInputStream())) {
97+
@Override
98+
public void close() throws IOException {
99+
}
100+
});
101+
}
102+
97103
public T nextPojo() {
98104
return jsonAsObject[index(jsonAsObject.length)];
99105
}

src/main/java/com/github/fabienrenaud/jjb/databind/Deserialization.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ public Object logansquare() throws Exception {
8686
public Object jodd() throws Exception {
8787
return JSON_SOURCE.provider().joddDeser().parse(JSON_SOURCE.nextString(), JSON_SOURCE.pojoType());
8888
}
89+
90+
@Benchmark
91+
@Override
92+
public Object moshi() throws Exception {
93+
return JSON_SOURCE.provider().moshi().fromJson(JSON_SOURCE.nextOkioBufferedSource());
94+
}
8995
}

src/main/java/com/github/fabienrenaud/jjb/databind/Serialization.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.bluelinelabs.logansquare.LoganSquare;
66
import com.github.fabienrenaud.jjb.JsonBench;
77
import com.github.fabienrenaud.jjb.JsonUtils;
8+
import okio.BufferedSink;
9+
import okio.Okio;
810
import org.openjdk.jmh.annotations.Benchmark;
911

1012
import java.io.ByteArrayOutputStream;
@@ -106,4 +108,14 @@ public Object logansquare() throws Exception {
106108
public Object jodd() throws Exception {
107109
return JSON_SOURCE.provider().joddSer().serialize(JSON_SOURCE.nextPojo());
108110
}
111+
112+
@Benchmark
113+
@Override
114+
public Object moshi() throws Exception {
115+
ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream();
116+
BufferedSink sink = Okio.buffer(Okio.sink(baos));
117+
JSON_SOURCE.provider().moshi().toJson(sink, JSON_SOURCE.nextPojo());
118+
sink.flush();
119+
return baos;
120+
}
109121
}

src/main/java/com/github/fabienrenaud/jjb/provider/JsonProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public interface JsonProvider<T> {
4141
jodd.json.JsonParser joddDeser();
4242

4343
jodd.json.JsonSerializer joddSer();
44+
45+
com.squareup.moshi.JsonAdapter<T> moshi();
4446
}

src/main/java/com/github/fabienrenaud/jjb/provider/UsersJsonProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.fabienrenaud.jjb.model.Users;
1010
import com.google.gson.Gson;
1111
import com.owlike.genson.Genson;
12+
import com.squareup.moshi.Moshi;
1213
import flexjson.JSONDeserializer;
1314
import flexjson.JSONSerializer;
1415
import org.apache.johnzon.mapper.Mapper;
@@ -29,6 +30,7 @@ public class UsersJsonProvider implements JsonProvider<Users> {
2930
private final JSONDeserializer<Users> flexjsonDeser = new JSONDeserializer<>();
3031
private final org.boon.json.ObjectMapper boon = org.boon.json.JsonFactory.create();
3132
private final org.apache.johnzon.mapper.Mapper johnson;
33+
private final com.squareup.moshi.JsonAdapter<Users> moshi = new Moshi.Builder().build().adapter(Users.class);
3234

3335
/*
3436
* DSL-json
@@ -112,6 +114,11 @@ public jodd.json.JsonSerializer joddSer() {
112114
return JODD_SER.get();
113115
}
114116

117+
@Override
118+
public com.squareup.moshi.JsonAdapter<Users> moshi() {
119+
return moshi;
120+
}
121+
115122
private static final ThreadLocal<flexjson.JSONSerializer> FLEXJSON_SER = new ThreadLocal<flexjson.JSONSerializer>() {
116123
@Override
117124
protected JSONSerializer initialValue() {

src/main/java/com/github/fabienrenaud/jjb/support/BenchSupport.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public enum BenchSupport {
2626
new Libapi(Library.LOGANSQUARE, Api.DATABIND),
2727
new Libapi(Library.JSONSIMPLE, Api.STREAM),
2828
new Libapi(Library.NANOJSON, Api.STREAM),
29-
new Libapi(Library.JODD, Api.DATABIND)
29+
new Libapi(Library.JODD, Api.DATABIND),
30+
new Libapi(Library.MOSHI, Api.DATABIND)
3031
);
3132

3233
private final List<Libapi> libapis;

src/main/java/com/github/fabienrenaud/jjb/support/Library.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum Library {
2323
LOGANSQUARE,
2424
JSONSIMPLE,
2525
NANOJSON,
26-
JODD;
26+
JODD,
27+
MOSHI;
2728

2829
public static Set<Library> fromCsv(String str) {
2930
if (str == null || str.trim().isEmpty()) {

src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,11 @@ public void jodd() throws Exception {
176176
test(Library.JODD, BENCH.jodd());
177177
}
178178
}
179+
180+
@Test
181+
public void moshi() throws Exception {
182+
for (int i = 0; i < ITERATIONS; i++) {
183+
test(Library.MOSHI, BENCH.moshi());
184+
}
185+
}
179186
}

0 commit comments

Comments
 (0)