Skip to content

Commit 1e1e559

Browse files
committed
json-simple
1 parent e8cfa70 commit 1e1e559

File tree

10 files changed

+123
-3
lines changed

10 files changed

+123
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following libraries are evaluated:
1818
* [johnzon](http://johnzon.apache.org/)
1919
* [logansquare](https://github.com/bluelinelabs/LoganSquare)
2020
* [dsl-json](https://github.com/ngs-doo/dsl-json)
21+
* [json-simple](https://code.google.com/archive/p/json-simple/)
2122

2223
This benchmark tests throughput performance of serialization and deserialization algorithms of the databind and stream API when available.
2324
Random payloads of various sizes are generated at runtime before each benchmark.

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ dependencies {
5454
// LoganSquare
5555
compile group: 'com.bluelinelabs', name: 'logansquare', version: '1.3.7'
5656
apt group: 'com.bluelinelabs', name: 'logansquare-compiler', version: '1.3.7'
57+
// json-simple
58+
compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'
5759

5860
// Test
5961
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
@@ -66,4 +66,8 @@ public Object logansquare() throws Exception {
6666
return null;
6767
}
6868

69+
public Object jsonsimple() throws Exception {
70+
return null;
71+
}
72+
6973
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public Object jsonio() throws Exception {
5757
return com.cedarsoftware.util.io.JsonReader.jsonToJava(JSON_SOURCE.nextInputStream(), JSON_SOURCE.provider().jsonioStreamOptions());
5858
}
5959

60+
@Benchmark
61+
@Override
62+
public Object jsonsimple() throws Exception {
63+
return org.json.simple.JSONValue.parse(JSON_SOURCE.nextReader());
64+
}
6065
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,16 @@ public Object genson() throws Exception {
6868
public Object jsonio() throws Exception {
6969
return com.cedarsoftware.util.io.JsonWriter.objectToJson(JSON_SOURCE.nextPojo(), JSON_SOURCE.provider().jsonioStreamOptions());
7070
}
71+
72+
@Benchmark
73+
@Override
74+
public Object jsonsimple() throws Exception {
75+
org.json.simple.JSONObject jso = JSON_SOURCE.streamSerializer().jsonsimple(JSON_SOURCE.nextPojo());
76+
77+
ByteArrayOutputStream baos = JsonUtils.byteArrayOutputStream();
78+
Writer w = new OutputStreamWriter(baos);
79+
org.json.simple.JSONValue.writeJSONString(jso, w);
80+
w.close();
81+
return baos;
82+
}
7183
}

src/main/java/com/github/fabienrenaud/jjb/stream/StreamSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public interface StreamSerializer<T> {
1616
void gson(final JsonWriter j, final T obj) throws IOException;
1717

1818
void jackson(final JsonGenerator j, final T obj) throws IOException;
19+
20+
org.json.simple.JSONObject jsonsimple(final T obj) throws IOException;
1921
}

src/main/java/com/github/fabienrenaud/jjb/stream/UsersStreamSerializer.java

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.fasterxml.jackson.core.JsonGenerator;
44
import com.github.fabienrenaud.jjb.model.Users;
5-
import com.github.fabienrenaud.jjb.model.Users.User;
65
import com.github.fabienrenaud.jjb.model.Users.Friend;
6+
import com.github.fabienrenaud.jjb.model.Users.User;
77
import com.google.gson.stream.JsonWriter;
88
import com.owlike.genson.stream.ObjectWriter;
99

@@ -321,4 +321,89 @@ private void jackson(final JsonGenerator j, final User u) throws IOException {
321321
}
322322
j.writeEndObject();
323323
}
324+
325+
@Override
326+
public org.json.simple.JSONObject jsonsimple(final Users obj) throws IOException {
327+
org.json.simple.JSONObject jso = new org.json.simple.JSONObject();
328+
if (obj.users != null) {
329+
org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray();
330+
for (User u : obj.users) {
331+
jsarr.add(jsonsimple(u));
332+
}
333+
jso.put("users", jsarr);
334+
}
335+
return jso;
336+
}
337+
338+
private org.json.simple.JSONObject jsonsimple(final User u) throws IOException {
339+
org.json.simple.JSONObject jso = new org.json.simple.JSONObject();
340+
if (u._id != null) {
341+
jso.put("_id", u._id);
342+
}
343+
jso.put("index", u.index);
344+
if (u.guid != null) {
345+
jso.put("guid", u.guid);
346+
}
347+
jso.put("isActive", u.isActive);
348+
if (u.balance != null) {
349+
jso.put("balance", u.balance);
350+
}
351+
if (u.picture != null) {
352+
jso.put("picture", u.picture);
353+
}
354+
jso.put("age", u.age);
355+
if (u.eyeColor != null) {
356+
jso.put("eyeColor", u.eyeColor);
357+
}
358+
if (u.name != null) {
359+
jso.put("name", u.name);
360+
}
361+
if (u.gender != null) {
362+
jso.put("gender", u.gender);
363+
}
364+
if (u.company != null) {
365+
jso.put("company", u.company);
366+
}
367+
if (u.email != null) {
368+
jso.put("email", u.email);
369+
}
370+
if (u.phone != null) {
371+
jso.put("phone", u.phone);
372+
}
373+
if (u.address != null) {
374+
jso.put("address", u.address);
375+
}
376+
if (u.about != null) {
377+
jso.put("about", u.about);
378+
}
379+
if (u.registered != null) {
380+
jso.put("registered", u.registered);
381+
}
382+
jso.put("latitude", u.latitude);
383+
jso.put("longitude", u.longitude);
384+
if (u.tags != null) {
385+
org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray();
386+
for (String t : u.tags) {
387+
jsarr.add(t);
388+
}
389+
jso.put("tags", jsarr);
390+
}
391+
if (u.friends != null) {
392+
org.json.simple.JSONArray jsarr = new org.json.simple.JSONArray();
393+
for (Friend f : u.friends) {
394+
org.json.simple.JSONObject jso0 = new org.json.simple.JSONObject();
395+
jso0.put("id", f.id);
396+
jso0.put("name", f.name);
397+
jsarr.add(jso0);
398+
}
399+
jso.put("friends", jsarr);
400+
}
401+
if (u.greeting != null) {
402+
jso.put("greeting", u.greeting);
403+
}
404+
if (u.favoriteFruit != null) {
405+
jso.put("favoriteFruit", u.favoriteFruit);
406+
}
407+
return jso;
408+
}
324409
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum BenchSupport {
2323
new Libapi(Library.JOHNZON, Api.DATABIND),
2424
new Libapi(Library.JSONSMART, Api.DATABIND),
2525
new Libapi(Library.DSLJSON, Api.DATABIND),
26-
new Libapi(Library.LOGANSQUARE, Api.DATABIND)
26+
new Libapi(Library.LOGANSQUARE, Api.DATABIND),
27+
new Libapi(Library.JSONSIMPLE, Api.STREAM)
2728
);
2829

2930
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
@@ -20,7 +20,8 @@ public enum Library {
2020
JOHNZON,
2121
JSONSMART,
2222
DSLJSON,
23-
LOGANSQUARE;
23+
LOGANSQUARE,
24+
JSONSIMPLE;
2425

2526
public static Set<Library> fromCsv(String str) {
2627
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
@@ -152,4 +152,11 @@ public void logansquare() throws Exception {
152152
test(Library.LOGANSQUARE, BENCH.logansquare());
153153
}
154154
}
155+
156+
@Test
157+
public void jsonsimple() throws Exception {
158+
for (int i = 0; i < ITERATIONS; i++) {
159+
test(Library.JSONSIMPLE, BENCH.jsonsimple());
160+
}
161+
}
155162
}

0 commit comments

Comments
 (0)