Skip to content

Commit ff871f5

Browse files
committed
#64 complete gson benchmark
1 parent 1836034 commit ff871f5

5 files changed

Lines changed: 157 additions & 39 deletions

File tree

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public final void reset(Slice value) {
104104
}
105105

106106
public final void reset(InputStream in) {
107+
JsonIterator.enableStreamingSupport();
107108
this.in = in;
108109
this.head = 0;
109110
this.tail = 0;

src/main/java/com/jsoniter/JsonIteratorPool.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public static JsonIterator borrowJsonIterator() {
1616
slot2.set(null);
1717
return iter;
1818
}
19-
iter = new JsonIterator();
20-
iter.reset(new byte[512]);
19+
iter = JsonIterator.parse(new byte[512], 0, 0);
2120
return iter;
2221
}
2322

src/main/java/com/jsoniter/extra/GsonCompatibilityMode.java

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class GsonCompatibilityMode extends Config {
3434
private final static int SURR2_LAST = 0xDFFF;
3535
private static final String[] REPLACEMENT_CHARS;
3636
private static final String[] HTML_SAFE_REPLACEMENT_CHARS;
37+
3738
static {
3839
REPLACEMENT_CHARS = new String[128];
3940
for (int i = 0; i <= 0x1f; i++) {
@@ -53,6 +54,7 @@ public class GsonCompatibilityMode extends Config {
5354
HTML_SAFE_REPLACEMENT_CHARS['='] = "\\u003d";
5455
HTML_SAFE_REPLACEMENT_CHARS['\''] = "\\u0027";
5556
}
57+
5658
private GsonCompatibilityMode(String configName, Builder builder) {
5759
super(configName, builder);
5860
}
@@ -278,7 +280,7 @@ public void encode(Object obj, JsonStream stream) throws IOException {
278280
_surrogate = 0;
279281
// Ok, then, is the second part valid?
280282
if (c < SURR2_FIRST || c > SURR2_LAST) {
281-
throw new JsonException("Broken surrogate pair: first char 0x"+Integer.toHexString(firstPart)+", second 0x"+Integer.toHexString(c)+"; illegal combination");
283+
throw new JsonException("Broken surrogate pair: first char 0x" + Integer.toHexString(firstPart) + ", second 0x" + Integer.toHexString(c) + "; illegal combination");
282284
}
283285
c = 0x10000 + ((firstPart - SURR1_FIRST) << 10) + (c - SURR2_FIRST);
284286
if (c > 0x10FFFF) { // illegal in JSON as well as in XML
@@ -334,6 +336,81 @@ public Object decode(JsonIterator iter) throws IOException {
334336
}
335337
}
336338
};
339+
} else if (boolean.class == type) {
340+
return new Decoder.BooleanDecoder() {
341+
@Override
342+
public boolean decodeBoolean(JsonIterator iter) throws IOException {
343+
ValueType valueType = iter.whatIsNext();
344+
if (valueType == ValueType.BOOLEAN) {
345+
return iter.readBoolean();
346+
} else if (valueType == ValueType.NULL) {
347+
iter.skip();
348+
return false;
349+
} else {
350+
throw new JsonException("expect boolean, but found " + valueType);
351+
}
352+
}
353+
};
354+
} else if (long.class == type) {
355+
return new Decoder.LongDecoder() {
356+
@Override
357+
public long decodeLong(JsonIterator iter) throws IOException {
358+
ValueType valueType = iter.whatIsNext();
359+
if (valueType == ValueType.NUMBER) {
360+
return iter.readLong();
361+
} else if (valueType == ValueType.NULL) {
362+
iter.skip();
363+
return 0;
364+
} else {
365+
throw new JsonException("expect long, but found " + valueType);
366+
}
367+
}
368+
};
369+
} else if (int.class == type) {
370+
return new Decoder.IntDecoder() {
371+
@Override
372+
public int decodeInt(JsonIterator iter) throws IOException {
373+
ValueType valueType = iter.whatIsNext();
374+
if (valueType == ValueType.NUMBER) {
375+
return iter.readInt();
376+
} else if (valueType == ValueType.NULL) {
377+
iter.skip();
378+
return 0;
379+
} else {
380+
throw new JsonException("expect int, but found " + valueType);
381+
}
382+
}
383+
};
384+
} else if (float.class == type) {
385+
return new Decoder.FloatDecoder() {
386+
@Override
387+
public float decodeFloat(JsonIterator iter) throws IOException {
388+
ValueType valueType = iter.whatIsNext();
389+
if (valueType == ValueType.NUMBER) {
390+
return iter.readFloat();
391+
} else if (valueType == ValueType.NULL) {
392+
iter.skip();
393+
return 0.0f;
394+
} else {
395+
throw new JsonException("expect float, but found " + valueType);
396+
}
397+
}
398+
};
399+
} else if (double.class == type) {
400+
return new Decoder.DoubleDecoder() {
401+
@Override
402+
public double decodeDouble(JsonIterator iter) throws IOException {
403+
ValueType valueType = iter.whatIsNext();
404+
if (valueType == ValueType.NUMBER) {
405+
return iter.readDouble();
406+
} else if (valueType == ValueType.NULL) {
407+
iter.skip();
408+
return 0.0d;
409+
} else {
410+
throw new JsonException("expect float, but found " + valueType);
411+
}
412+
}
413+
};
337414
}
338415
return super.createDecoder(cacheKey, type);
339416
}

src/test/java/com/jsoniter/BenchGson.java

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ public void benchSetup(BenchmarkParams params) {
3131
.setDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
3232
.create();
3333
gsonCompatibilityMode = new GsonCompatibilityMode.Builder().setDateFormat("EEE MMM dd HH:mm:ss Z yyyy").build();
34+
JsoniterSpi.setCurrentConfig(gsonCompatibilityMode);
35+
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
3436
if (params != null) {
3537
if (params.getBenchmark().contains("jsoniterDynamicCodegenDecoder")) {
3638
JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
3739
}
3840
}
3941
}
4042

41-
// @Benchmark
43+
@Benchmark
4244
public void gsonDecoder(Blackhole bh) throws IOException {
4345
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
4446
InputStreamReader reader = new InputStreamReader(stream);
@@ -56,7 +58,6 @@ public void jsoniterReflectionDecoder(Blackhole bh) throws IOException {
5658
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
5759
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
5860
try {
59-
System.out.print(iter.currentBuffer());
6061
iter.reset(stream);
6162
bh.consume(iter.read(new TypeReference<List<Tweet>>() {
6263
}.getType()));
@@ -66,40 +67,6 @@ public void jsoniterReflectionDecoder(Blackhole bh) throws IOException {
6667
}
6768
}
6869

69-
@Test
70-
public void test() throws IOException {
71-
gson = new GsonBuilder()
72-
.setDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
73-
.create();
74-
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
75-
InputStreamReader reader = new InputStreamReader(stream);
76-
try {
77-
System.out.println(gson.fromJson(reader, new TypeReference<List<Tweet>>() {
78-
}.getType()));;
79-
} finally {
80-
reader.close();
81-
stream.close();
82-
}
83-
FileInputStream fileInputStream = new FileInputStream("/tmp/tweets.json");
84-
// byte[] input = new byte[1024 * 1024 * 32];
85-
// int len = fileInputStream.read(input);
86-
gsonCompatibilityMode = new GsonCompatibilityMode.Builder()
87-
.setDateFormat("EEE MMM dd HH:mm:ss Z yyyy")
88-
.build();
89-
JsoniterSpi.setCurrentConfig(gsonCompatibilityMode);
90-
JsonIterator iter = JsonIteratorPool.borrowJsonIterator();
91-
try {
92-
System.out.println("!!!");
93-
JsonIterator.enableStreamingSupport();
94-
iter.reset(fileInputStream);
95-
List<Tweet> obj = (List<Tweet>) iter.read( new TypeReference<List<Tweet>>() {}.getType());
96-
System.out.println(obj.size());
97-
} catch (RuntimeException e){
98-
e.printStackTrace();
99-
} finally {
100-
JsonIteratorPool.returnJsonIterator(iter);
101-
}
102-
}
10370
//
10471
// @Benchmark
10572
// public void jsoniterDynamicCodegenDecoder(Blackhole bh) throws IOException {

src/test/java/com/jsoniter/TestGson.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import com.jsoniter.extra.GsonCompatibilityMode;
99
import junit.framework.TestCase;
1010

11+
import java.io.FileInputStream;
12+
import java.io.FileNotFoundException;
13+
import java.io.IOException;
1114
import java.lang.reflect.Field;
1215
import java.util.Date;
1316
import java.util.TimeZone;
@@ -196,4 +199,75 @@ public void test_bool_as_string() {
196199
str = JsonIterator.deserialize(config, "true", String.class);
197200
assertEquals("true", str);
198201
}
202+
203+
public static class TestObject6 {
204+
public boolean field;
205+
}
206+
207+
public void test_null_as_boolean() {
208+
Gson gson = new Gson();
209+
TestObject6 obj = gson.fromJson("{\"field\":null}", TestObject6.class);
210+
assertFalse(obj.field);
211+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
212+
obj = JsonIterator.deserialize(config, "{\"field\":null}", TestObject6.class);
213+
assertFalse(obj.field);
214+
}
215+
216+
public static class TestObject7 {
217+
public long field;
218+
}
219+
220+
public void test_null_as_long() {
221+
Gson gson = new Gson();
222+
TestObject7 obj = gson.fromJson("{\"field\":null}", TestObject7.class);
223+
assertEquals(0, obj.field);
224+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
225+
obj = JsonIterator.deserialize(config, "{\"field\":null}", TestObject7.class);
226+
assertEquals(0, obj.field);
227+
}
228+
229+
public static class TestObject8 {
230+
public int field;
231+
}
232+
233+
public void test_null_as_int() {
234+
Gson gson = new Gson();
235+
TestObject8 obj = gson.fromJson("{\"field\":null}", TestObject8.class);
236+
assertEquals(0, obj.field);
237+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
238+
obj = JsonIterator.deserialize(config, "{\"field\":null}", TestObject8.class);
239+
assertEquals(0, obj.field);
240+
}
241+
242+
public static class TestObject9 {
243+
public float field;
244+
}
245+
246+
public void test_null_as_float() {
247+
Gson gson = new Gson();
248+
TestObject9 obj = gson.fromJson("{\"field\":null}", TestObject9.class);
249+
assertEquals(0.0f, obj.field);
250+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
251+
obj = JsonIterator.deserialize(config, "{\"field\":null}", TestObject9.class);
252+
assertEquals(0.0f, obj.field);
253+
}
254+
255+
public static class TestObject10 {
256+
public double field;
257+
}
258+
259+
public void test_null_as_double() {
260+
Gson gson = new Gson();
261+
TestObject10 obj = gson.fromJson("{\"field\":null}", TestObject10.class);
262+
assertEquals(0.0d, obj.field);
263+
GsonCompatibilityMode config = new GsonCompatibilityMode.Builder().build();
264+
obj = JsonIterator.deserialize(config, "{\"field\":null}", TestObject10.class);
265+
assertEquals(0.0d, obj.field);
266+
}
267+
268+
public void test() throws IOException {
269+
FileInputStream stream = new FileInputStream("/tmp/tweets.json");
270+
JsonIterator iter = JsonIterator.parse(stream, 4092);
271+
System.out.println(iter.whatIsNext());
272+
}
199273
}

0 commit comments

Comments
 (0)