Skip to content

Commit 27714b4

Browse files
committed
Update to Moshi 1.2.0.
1 parent 2cdcc34 commit 27714b4

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,15 @@
9999

100100
<!-- Moshi, by some of same folks as GSON -->
101101
<dependency>
102-
<groupId>com.squareup.moshi</groupId>
103-
<artifactId>moshi</artifactId>
104-
<version>0.9.0</version>
102+
<groupId>com.squareup.moshi</groupId>
103+
<artifactId>moshi</artifactId>
104+
<version>1.2.0</version>
105+
</dependency>
106+
107+
<dependency>
108+
<groupId>com.squareup.okio</groupId>
109+
<artifactId>okio</artifactId>
110+
<version>1.8.0</version>
105111
</dependency>
106112

107113
<!-- and Json.io from https://github.com/jdereg/json-io -->

src/main/java/com/cowtowncoder/jsonperf/dzone/read/DZoneReadPojoMoshi.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.cowtowncoder.jsonperf.dzone.MeasurementPOJO;
1010
import com.squareup.moshi.JsonAdapter;
1111
import com.squareup.moshi.Moshi;
12+
import okio.Buffer;
1213

1314
@State(Scope.Thread)
1415
@OutputTimeUnit(TimeUnit.SECONDS)
@@ -24,9 +25,9 @@ public DZoneReadPojoMoshi()
2425

2526
@Override
2627
public MeasurementPOJO _readItems(byte[] input) throws Exception {
27-
// In theory there may be a way to use Writer etc; but it gets complicated
28-
// enough with the strange API that... yeah.
29-
return adapter.fromJson(new String(input, "UTF-8"));
28+
Buffer buffer = new Buffer();
29+
buffer.write(input);
30+
return adapter.fromJson(buffer);
3031
}
3132

3233
@Override

src/main/java/com/cowtowncoder/jsonperf/dzone/write/DZoneWriteMoshi.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.cowtowncoder.jsonperf.dzone.write;
22

3-
import java.io.*;
3+
import java.io.OutputStream;
4+
import java.io.Writer;
45
import java.util.concurrent.TimeUnit;
56

67
import org.openjdk.jmh.annotations.OutputTimeUnit;
@@ -10,20 +11,11 @@
1011
import com.cowtowncoder.jsonperf.dzone.MeasurementPOJO;
1112
import com.squareup.moshi.JsonAdapter;
1213
import com.squareup.moshi.Moshi;
14+
import okio.BufferedSink;
15+
import okio.Okio;
1316

1417
/**
1518
* Test codec for Moshi (https://github.com/square/moshi)
16-
*<p>
17-
* Challenges here are mostly due to rather exotic API that Moshi exposes: it seems
18-
* to aim at highly optimized I/O handling, buffering; but the end result is something
19-
* that is difficult use in ways other than just buffering the whole contents in
20-
* memory. Since the goal here is to minimize actual buffering overhead there does not
21-
* seem to be a good match, and what we shall do then is to build a JSON String,
22-
* write that in varius no-op outputs. This is not optimal, but with given API there
23-
* isn't much we can do, without digging knee-deep in obscure buffer/sink classes
24-
* that Moshi exposes from ok-io.
25-
*<p>
26-
* If anyone wants to tackle write-to-Stream / write-to-Writer cases, PRs happily accepted.
2719
*/
2820
@State(Scope.Thread)
2921
@OutputTimeUnit(TimeUnit.SECONDS)
@@ -40,16 +32,15 @@ public DZoneWriteMoshi()
4032
@Override
4133
public int _writeItems(MeasurementPOJO items, OutputStream out) throws Exception
4234
{
43-
OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
44-
w.write(adapter.toJson(items));
45-
w.close();
35+
BufferedSink sink = Okio.buffer(Okio.sink(out));
36+
adapter.toJson(sink, items);
4637
return items.size();
4738
}
4839

4940
@Override
5041
public int _writeItems(MeasurementPOJO items, Writer out) throws Exception
5142
{
52-
out.write(adapter.toJson(items));
43+
out.write(_writeAsString(items));
5344
return items.size();
5445
}
5546

0 commit comments

Comments
 (0)