Skip to content

Commit

Permalink
Moshi
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienrenaud committed Nov 19, 2016
1 parent f7f0c11 commit 6af2c0a
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 7 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/github/fabienrenaud/jjb/JsonBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ public Object jodd() throws Exception {
return null;
}

public Object moshi() throws Exception {
return null;
}

}
1 change: 0 additions & 1 deletion src/main/java/com/github/fabienrenaud/jjb/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,4 @@ protected com.dslplatform.json.JsonWriter initialValue() {
}
};


}
14 changes: 10 additions & 4 deletions src/main/java/com/github/fabienrenaud/jjb/data/JsonSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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)];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public interface JsonProvider<T> {
jodd.json.JsonParser joddDeser();

jodd.json.JsonSerializer joddSer();

com.squareup.moshi.JsonAdapter<T> moshi();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,7 @@ public class UsersJsonProvider implements JsonProvider<Users> {
private final JSONDeserializer<Users> 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<Users> moshi = new Moshi.Builder().build().adapter(Users.class);

/*
* DSL-json
Expand Down Expand Up @@ -112,6 +114,11 @@ public jodd.json.JsonSerializer joddSer() {
return JODD_SER.get();
}

@Override
public com.squareup.moshi.JsonAdapter<Users> moshi() {
return moshi;
}

private static final ThreadLocal<flexjson.JSONSerializer> FLEXJSON_SER = new ThreadLocal<flexjson.JSONSerializer>() {
@Override
protected JSONSerializer initialValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Libapi> libapis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum Library {
LOGANSQUARE,
JSONSIMPLE,
NANOJSON,
JODD;
JODD,
MOSHI;

public static Set<Library> fromCsv(String str) {
if (str == null || str.trim().isEmpty()) {
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/github/fabienrenaud/jjb/JsonBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit 6af2c0a

Please sign in to comment.