Skip to content

Commit

Permalink
json-simple
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienrenaud committed Nov 5, 2016
1 parent e8cfa70 commit 1e1e559
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
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 @@ -66,4 +66,8 @@ public Object logansquare() throws Exception {
return null;
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface StreamSerializer<T> {
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Libapi> libapis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum Library {
JOHNZON,
JSONSMART,
DSLJSON,
LOGANSQUARE;
LOGANSQUARE,
JSONSIMPLE;

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 @@ -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());
}
}
}

0 comments on commit 1e1e559

Please sign in to comment.