Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@

<!-- Moshi, by some of same folks as GSON -->
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>0.9.0</version>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.2.0</version>
</dependency>

<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.8.0</version>
</dependency>

<!-- and Json.io from https://github.com/jdereg/json-io -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
*<p>
* 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.
*<p>
* If anyone wants to tackle write-to-Stream / write-to-Writer cases, PRs happily accepted.
*/
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.SECONDS)
Expand All @@ -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();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing sink.emit()? You might leave this method with unflushed bytes.

}

@Override
public int _writeItems(MeasurementPOJO items, Writer out) throws Exception
{
out.write(adapter.toJson(items));
out.write(_writeAsString(items));
return items.size();
}

Expand Down