Skip to content

Commit be47247

Browse files
committed
compare with moshi
1 parent 8e01eb4 commit be47247

File tree

5 files changed

+31
-37
lines changed

5 files changed

+31
-37
lines changed

demo/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>fastjson</artifactId>
8787
<version>1.2.22</version>
8888
</dependency>
89+
<dependency>
90+
<groupId>com.squareup.moshi</groupId>
91+
<artifactId>moshi</artifactId>
92+
<version>1.3.1</version>
93+
</dependency>
8994

9095
</dependencies>
9196

demo/src/test/java/com/jsoniter/demo/ModelTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.jsoniter.DecodingMode;
88
import com.jsoniter.JsonIterator;
99
import com.jsoniter.spi.TypeLiteral;
10+
import com.squareup.moshi.JsonAdapter;
11+
import com.squareup.moshi.Moshi;
1012
import org.junit.Test;
1113
import org.openjdk.jmh.Main;
1214
import org.openjdk.jmh.annotations.*;
@@ -29,6 +31,7 @@ public class ModelTest {
2931
private TypeLiteral<Model> modelTypeLiteral; // this is thread-safe can reused
3032
private ObjectMapper jackson;
3133
private TypeReference<Model> modelTypeReference;
34+
private JsonAdapter<Model> moshiAdapter;
3235

3336
@Setup(Level.Trial)
3437
public void benchSetup(BenchmarkParams params) {
@@ -43,6 +46,8 @@ public void benchSetup(BenchmarkParams params) {
4346
jackson.registerModule(new AfterburnerModule());
4447
modelTypeReference = new TypeReference<Model>() {
4548
};
49+
Moshi moshi = new Moshi.Builder().build();
50+
moshiAdapter = moshi.adapter(Model.class);
4651
}
4752

4853
public static void main(String[] args) throws IOException, RunnerException {
@@ -60,6 +65,7 @@ public void test() throws IOException {
6065
benchSetup(null);
6166
iter.reset(inputBytes);
6267
System.out.println(iter.read(modelTypeLiteral).name);
68+
System.out.println(moshiAdapter.fromJson(input).name);
6369
}
6470

6571
// public static void main(String[] args) throws Exception {
@@ -90,14 +96,19 @@ public void jsoniter_easy_mode(Blackhole bh) throws IOException {
9096
bh.consume(JsonIterator.deserialize(inputBytes, Model.class));
9197
}
9298

93-
@Benchmark
99+
// @Benchmark
94100
public void fastjson(Blackhole bh) throws IOException {
95101
// this is not a exactly fair comparison,
96102
// as string => object is not
97103
// bytes => object
98104
bh.consume(JSON.parseObject(input, Model.class));
99105
}
100106

107+
@Benchmark
108+
public void moshi(Blackhole bh) throws IOException {
109+
bh.consume(moshiAdapter.fromJson(input));
110+
}
111+
101112
// @Benchmark
102113
public void jackson(Blackhole bh) throws IOException {
103114
bh.consume(jackson.readValue(inputBytes, modelTypeReference));

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,6 @@ public static Any readAny(JsonIterator iter) throws IOException {
181181
case '"':
182182
skipString(iter);
183183
return Any.lazyString(iter.buf, start, iter.head);
184-
case '-':
185-
case '0':
186-
case '1':
187-
case '2':
188-
case '3':
189-
case '4':
190-
case '5':
191-
case '6':
192-
case '7':
193-
case '8':
194-
case '9':
195-
if (skipNumber(iter)) {
196-
return Any.lazyDouble(iter.buf, start, iter.head);
197-
} else {
198-
return Any.lazyLong(iter.buf, start, iter.head);
199-
}
200184
case 't':
201185
skipFixedBytes(iter, 3);
202186
return Any.wrap(true);
@@ -213,7 +197,11 @@ public static Any readAny(JsonIterator iter) throws IOException {
213197
skipObject(iter);
214198
return Any.lazyObject(iter.buf, start, iter.head);
215199
default:
216-
throw iter.reportError("IterImplSkip", "do not know how to skip: " + c);
200+
if (skipNumber(iter)) {
201+
return Any.lazyDouble(iter.buf, start, iter.head);
202+
} else {
203+
return Any.lazyLong(iter.buf, start, iter.head);
204+
}
217205
}
218206
}
219207

src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -320,24 +320,6 @@ public static Any readAny(JsonIterator iter) throws IOException {
320320
skipString(iter);
321321
byte[] copied = copySkippedBytes(iter);
322322
return Any.lazyString(copied, 0, copied.length);
323-
case '-':
324-
case '0':
325-
case '1':
326-
case '2':
327-
case '3':
328-
case '4':
329-
case '5':
330-
case '6':
331-
case '7':
332-
case '8':
333-
case '9':
334-
if (skipNumber(iter)) {
335-
copied = copySkippedBytes(iter);
336-
return Any.lazyDouble(copied, 0, copied.length);
337-
} else {
338-
copied = copySkippedBytes(iter);
339-
return Any.lazyLong(copied, 0, copied.length);
340-
}
341323
case 't':
342324
skipFixedBytes(iter, 3);
343325
iter.skipStartedAt = -1;
@@ -359,7 +341,13 @@ public static Any readAny(JsonIterator iter) throws IOException {
359341
copied = copySkippedBytes(iter);
360342
return Any.lazyObject(copied, 0, copied.length);
361343
default:
362-
throw iter.reportError("IterImplSkip", "do not know how to skip: " + c);
344+
if (skipNumber(iter)) {
345+
copied = copySkippedBytes(iter);
346+
return Any.lazyDouble(copied, 0, copied.length);
347+
} else {
348+
copied = copySkippedBytes(iter);
349+
return Any.lazyLong(copied, 0, copied.length);
350+
}
363351
}
364352
}
365353

src/main/java/com/jsoniter/any/ObjectLazyAny.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ public EntryIterator entries() {
163163
return new LazyIterator();
164164
}
165165

166+
167+
// TODO: lastParsedPos can not share with underlying Any, as it might be changed during iteration
166168
private class LazyIterator implements EntryIterator {
167169

168170
private Iterator<Map.Entry<Object, Any>> mapIter;

0 commit comments

Comments
 (0)