From 27714b47e83332f490e7deac31090c1c1ebe497b Mon Sep 17 00:00:00 2001 From: jwilson Date: Sat, 28 May 2016 16:08:29 -0400 Subject: [PATCH] Update to Moshi 1.2.0. --- pom.xml | 12 +++++++--- .../dzone/read/DZoneReadPojoMoshi.java | 7 +++--- .../jsonperf/dzone/write/DZoneWriteMoshi.java | 23 ++++++------------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index df30ad6..ddcd04a 100644 --- a/pom.xml +++ b/pom.xml @@ -99,9 +99,15 @@ - com.squareup.moshi - moshi - 0.9.0 + com.squareup.moshi + moshi + 1.2.0 + + + + com.squareup.okio + okio + 1.8.0 diff --git a/src/main/java/com/cowtowncoder/jsonperf/dzone/read/DZoneReadPojoMoshi.java b/src/main/java/com/cowtowncoder/jsonperf/dzone/read/DZoneReadPojoMoshi.java index 5a0e9ce..3da0c9b 100644 --- a/src/main/java/com/cowtowncoder/jsonperf/dzone/read/DZoneReadPojoMoshi.java +++ b/src/main/java/com/cowtowncoder/jsonperf/dzone/read/DZoneReadPojoMoshi.java @@ -9,6 +9,7 @@ import com.cowtowncoder.jsonperf.dzone.MeasurementPOJO; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; +import okio.Buffer; @State(Scope.Thread) @OutputTimeUnit(TimeUnit.SECONDS) @@ -24,9 +25,9 @@ public DZoneReadPojoMoshi() @Override public MeasurementPOJO _readItems(byte[] input) throws Exception { - // In theory there may be a way to use Writer etc; but it gets complicated - // enough with the strange API that... yeah. - return adapter.fromJson(new String(input, "UTF-8")); + Buffer buffer = new Buffer(); + buffer.write(input); + return adapter.fromJson(buffer); } @Override diff --git a/src/main/java/com/cowtowncoder/jsonperf/dzone/write/DZoneWriteMoshi.java b/src/main/java/com/cowtowncoder/jsonperf/dzone/write/DZoneWriteMoshi.java index f70554e..e71e3b4 100644 --- a/src/main/java/com/cowtowncoder/jsonperf/dzone/write/DZoneWriteMoshi.java +++ b/src/main/java/com/cowtowncoder/jsonperf/dzone/write/DZoneWriteMoshi.java @@ -1,6 +1,7 @@ package com.cowtowncoder.jsonperf.dzone.write; -import java.io.*; +import java.io.OutputStream; +import java.io.Writer; import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.OutputTimeUnit; @@ -10,20 +11,11 @@ import com.cowtowncoder.jsonperf.dzone.MeasurementPOJO; import com.squareup.moshi.JsonAdapter; import com.squareup.moshi.Moshi; +import okio.BufferedSink; +import okio.Okio; /** * Test codec for Moshi (https://github.com/square/moshi) - *

- * Challenges here are mostly due to rather exotic API that Moshi exposes: it seems - * to aim at highly optimized I/O handling, buffering; but the end result is something - * that is difficult use in ways other than just buffering the whole contents in - * memory. Since the goal here is to minimize actual buffering overhead there does not - * seem to be a good match, and what we shall do then is to build a JSON String, - * write that in varius no-op outputs. This is not optimal, but with given API there - * isn't much we can do, without digging knee-deep in obscure buffer/sink classes - * that Moshi exposes from ok-io. - *

- * If anyone wants to tackle write-to-Stream / write-to-Writer cases, PRs happily accepted. */ @State(Scope.Thread) @OutputTimeUnit(TimeUnit.SECONDS) @@ -40,16 +32,15 @@ public DZoneWriteMoshi() @Override public int _writeItems(MeasurementPOJO items, OutputStream out) throws Exception { - OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8"); - w.write(adapter.toJson(items)); - w.close(); + BufferedSink sink = Okio.buffer(Okio.sink(out)); + adapter.toJson(sink, items); return items.size(); } @Override public int _writeItems(MeasurementPOJO items, Writer out) throws Exception { - out.write(adapter.toJson(items)); + out.write(_writeAsString(items)); return items.size(); }