Skip to content

Commit 33ed9f6

Browse files
committed
support field matching
1 parent bc9b6b0 commit 33ed9f6

17 files changed

Lines changed: 686 additions & 77 deletions
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.jsoniter.demo;
2+
3+
import com.dslplatform.json.DslJson;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
7+
import com.jsoniter.JsonIterator;
8+
import com.jsoniter.annotation.JacksonAnnotationSupport;
9+
import com.jsoniter.spi.TypeLiteral;
10+
import org.junit.Test;
11+
import org.openjdk.jmh.Main;
12+
import org.openjdk.jmh.annotations.*;
13+
import org.openjdk.jmh.infra.BenchmarkParams;
14+
import org.openjdk.jmh.infra.Blackhole;
15+
16+
import java.io.IOException;
17+
import java.util.Arrays;
18+
19+
@State(Scope.Thread)
20+
public class ArrayBinding {
21+
private TypeLiteral<String[]> typeLiteral;
22+
private ObjectMapper jackson;
23+
private byte[] input;
24+
private TypeReference<String[]> typeRef;
25+
private String inputStr;
26+
27+
private JsonIterator iter;
28+
private DslJson dslJson;
29+
30+
@Setup(Level.Trial)
31+
public void benchSetup(BenchmarkParams params) {
32+
inputStr = "['jackson','jsoniter','fastjson']".replace('\'', '"');
33+
input = inputStr.getBytes();
34+
iter = JsonIterator.parse(input);
35+
typeLiteral = new TypeLiteral<String[]>() {
36+
};
37+
typeRef = new TypeReference<String[]>() {
38+
};
39+
JacksonAnnotationSupport.enable();
40+
jackson = new ObjectMapper();
41+
jackson.registerModule(new AfterburnerModule());
42+
dslJson = new DslJson();
43+
}
44+
45+
@Test
46+
public void test() throws IOException {
47+
benchSetup(null);
48+
System.out.println(Arrays.toString(withJsoniter()));
49+
System.out.println(Arrays.toString(withJackson()));
50+
System.out.println(Arrays.toString(withDsljson()));
51+
}
52+
53+
public static void main(String[] args) throws Exception {
54+
Main.main(new String[]{
55+
"ArrayBinding",
56+
"-i", "5",
57+
"-wi", "5",
58+
"-f", "1",
59+
});
60+
}
61+
62+
@Benchmark
63+
public void withJsoniterBinding(Blackhole bh) throws IOException {
64+
bh.consume(withJsoniter());
65+
}
66+
67+
@Benchmark
68+
public void withJackson(Blackhole bh) throws IOException {
69+
bh.consume(withJackson());
70+
}
71+
72+
@Benchmark
73+
public void withDsljson(Blackhole bh) throws IOException {
74+
bh.consume(withDsljson());
75+
}
76+
77+
private String[] withJsoniter() throws IOException {
78+
iter.reset();
79+
return iter.read(typeLiteral);
80+
}
81+
82+
private String[] withJackson() throws IOException {
83+
return jackson.readValue(input, typeRef);
84+
}
85+
86+
private String[] withDsljson() throws IOException {
87+
return (String[]) dslJson.deserialize(String[].class, input, input.length);
88+
}
89+
}

demo/src/main/java/com/jsoniter/demo/ConstructorBinding.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public TestObject(
4444

4545
@Override
4646
public String toString() {
47-
return "TestObject{" +
47+
return "TestObject1{" +
4848
"field1=" + field1 +
4949
", field2=" + field2 +
5050
'}';
@@ -93,12 +93,12 @@ public static void main(String[] args) throws Exception {
9393
});
9494
}
9595

96-
// @Benchmark
96+
@Benchmark
9797
public void withJsoniterHashMode(Blackhole bh) throws IOException {
9898
bh.consume(withJsoniter());
9999
}
100100

101-
// @Benchmark
101+
@Benchmark
102102
public void withJsoniterStrictMode(Blackhole bh) throws IOException {
103103
bh.consume(withJsoniter());
104104
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.jsoniter.demo;
2+
3+
import com.jsoniter.JsonException;
4+
import com.jsoniter.JsonIterator;
5+
import com.jsoniter.annotation.JsonProperty;
6+
import com.jsoniter.annotation.JsonUnknownProperties;
7+
import com.jsoniter.annotation.JsoniterAnnotationSupport;
8+
import com.jsoniter.spi.TypeLiteral;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
import org.openjdk.jmh.Main;
12+
import org.openjdk.jmh.annotations.*;
13+
import org.openjdk.jmh.infra.Blackhole;
14+
15+
import java.io.IOException;
16+
17+
@State(Scope.Thread)
18+
public class FieldMatching {
19+
private TypeLiteral<TestObject0> testObject0Type;
20+
private TypeLiteral<TestObject1> testObject1Type;
21+
private TypeLiteral<TestObject2> testObject2Type;
22+
private TypeLiteral<TestObject3> testObject3Type;
23+
private TypeLiteral<TestObject4> testObject4Type;
24+
private JsonIterator iter0;
25+
private JsonIterator iter1Success;
26+
27+
public static class TestObject0 {
28+
public int field1;
29+
public int field2;
30+
public int field3;
31+
}
32+
33+
public static class TestObject1 {
34+
@JsonProperty(required = true)
35+
public int field1;
36+
@JsonProperty(required = true)
37+
public int field2;
38+
@JsonProperty(required = true)
39+
public int field3;
40+
}
41+
42+
@JsonUnknownProperties(failOnUnkown = true)
43+
public static class TestObject2 {
44+
public int field1;
45+
public int field2;
46+
}
47+
48+
@JsonUnknownProperties(failOnUnkown = true, whitelist = {"field2"})
49+
public static class TestObject3 {
50+
public int field1;
51+
}
52+
53+
@JsonUnknownProperties(blacklist = {"field3"})
54+
public static class TestObject4 {
55+
public int field1;
56+
}
57+
58+
@Setup(Level.Trial)
59+
public void benchSetup() {
60+
JsoniterAnnotationSupport.enable();
61+
JsonIterator.enableStrictMode();
62+
iter0 = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
63+
iter1Success = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
64+
testObject0Type = new TypeLiteral<TestObject0>() {
65+
};
66+
testObject1Type = new TypeLiteral<TestObject1>() {
67+
};
68+
testObject2Type = new TypeLiteral<TestObject2>() {
69+
};
70+
testObject3Type = new TypeLiteral<TestObject3>() {
71+
};
72+
testObject4Type = new TypeLiteral<TestObject4>() {
73+
};
74+
}
75+
76+
@Test
77+
public void test() throws IOException {
78+
benchSetup();
79+
try {
80+
JsonIterator iter1Failure = JsonIterator.parse("{'field2':101}".replace('\'', '"').getBytes());
81+
iter1Failure.read(testObject1Type);
82+
Assert.fail();
83+
} catch (JsonException e) {
84+
System.out.println(e);
85+
}
86+
try {
87+
JsonIterator iter2Failure = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
88+
iter2Failure.read(testObject2Type);
89+
Assert.fail();
90+
} catch (JsonException e) {
91+
System.out.println(e);
92+
}
93+
try {
94+
JsonIterator iter3Failure = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
95+
iter3Failure.read(testObject3Type);
96+
Assert.fail();
97+
} catch (JsonException e) {
98+
System.out.println(e);
99+
}
100+
try {
101+
JsonIterator iter4Failure = JsonIterator.parse("{'field1':101,'field2':101,'field3':101}".replace('\'', '"').getBytes());
102+
iter4Failure.read(testObject4Type);
103+
Assert.fail();
104+
} catch (JsonException e) {
105+
System.out.println(e);
106+
}
107+
}
108+
109+
public static void main(String[] args) throws Exception {
110+
Main.main(new String[]{
111+
"FieldMatching",
112+
"-i", "5",
113+
"-wi", "5",
114+
"-f", "1",
115+
});
116+
}
117+
118+
@Benchmark
119+
public void iter0(Blackhole bh) throws IOException {
120+
iter0.reset();
121+
bh.consume(iter0.read(testObject0Type));
122+
}
123+
124+
@Benchmark
125+
public void iter1Success(Blackhole bh) throws IOException {
126+
iter1Success.reset();
127+
bh.consume(iter1Success.read(testObject1Type));
128+
}
129+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.jsoniter.demo;
2+
3+
import com.dslplatform.json.DslJson;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
7+
import com.jsoniter.JsonIterator;
8+
import com.jsoniter.annotation.JacksonAnnotationSupport;
9+
import com.jsoniter.spi.TypeLiteral;
10+
import org.junit.Test;
11+
import org.openjdk.jmh.Main;
12+
import org.openjdk.jmh.annotations.*;
13+
import org.openjdk.jmh.infra.BenchmarkParams;
14+
import org.openjdk.jmh.infra.Blackhole;
15+
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
@State(Scope.Thread)
20+
public class ListBinding {
21+
private TypeLiteral<List<String>> typeLiteral;
22+
private ObjectMapper jackson;
23+
private byte[] input;
24+
private TypeReference<List<String>> typeRef;
25+
private String inputStr;
26+
27+
private JsonIterator iter;
28+
private DslJson dslJson;
29+
30+
@Setup(Level.Trial)
31+
public void benchSetup(BenchmarkParams params) {
32+
inputStr = "['jackson','jsoniter','fastjson']".replace('\'', '"');
33+
input = inputStr.getBytes();
34+
iter = JsonIterator.parse(input);
35+
typeLiteral = new TypeLiteral<List<String>>() {
36+
};
37+
typeRef = new TypeReference<List<String>>() {
38+
};
39+
JacksonAnnotationSupport.enable();
40+
jackson = new ObjectMapper();
41+
jackson.registerModule(new AfterburnerModule());
42+
dslJson = new DslJson();
43+
}
44+
45+
@Test
46+
public void test() throws IOException {
47+
benchSetup(null);
48+
System.out.println(withJsoniter());
49+
System.out.println(withJackson());
50+
System.out.println(withDsljson());
51+
}
52+
53+
public static void main(String[] args) throws Exception {
54+
Main.main(new String[]{
55+
"ListBinding",
56+
"-i", "5",
57+
"-wi", "5",
58+
"-f", "1",
59+
});
60+
}
61+
62+
@Benchmark
63+
public void withJsoniterBinding(Blackhole bh) throws IOException {
64+
bh.consume(withJsoniter());
65+
}
66+
67+
@Benchmark
68+
public void withJackson(Blackhole bh) throws IOException {
69+
bh.consume(withJackson());
70+
}
71+
72+
@Benchmark
73+
public void withDsljson(Blackhole bh) throws IOException {
74+
bh.consume(withDsljson());
75+
}
76+
77+
private List<String> withJsoniter() throws IOException {
78+
iter.reset();
79+
return iter.read(typeLiteral);
80+
}
81+
82+
private List<String> withJackson() throws IOException {
83+
return jackson.readValue(input, typeRef);
84+
}
85+
86+
private List<String> withDsljson() throws IOException {
87+
return (List<String>) dslJson.deserializeList(String.class, input, input.length);
88+
}
89+
}

0 commit comments

Comments
 (0)