Skip to content

Commit e674ad8

Browse files
committed
update demo
1 parent eb3d9dc commit e674ad8

6 files changed

Lines changed: 244 additions & 4 deletions

File tree

demo/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
<artifactId>dsl-json-processor</artifactId>
8383
<version>LATEST</version>
8484
</dependency>
85+
<dependency>
86+
<groupId>com.alibaba</groupId>
87+
<artifactId>fastjson</artifactId>
88+
<version>LATEST</version>
89+
</dependency>
8590

8691
</dependencies>
8792

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.jsoniter.demo;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
8+
import com.jsoniter.JsonIterator;
9+
import com.jsoniter.annotation.JsoniterAnnotationSupport;
10+
import com.jsoniter.spi.TypeLiteral;
11+
import org.junit.Test;
12+
import org.openjdk.jmh.Main;
13+
import org.openjdk.jmh.annotations.*;
14+
import org.openjdk.jmh.infra.Blackhole;
15+
16+
import java.io.IOException;
17+
18+
@State(Scope.Thread)
19+
public class ConstructorBinding {
20+
21+
private TypeLiteral<TestObject> typeLiteral;
22+
private ObjectMapper jackson;
23+
private byte[] input;
24+
private TypeReference<TestObject> typeRef;
25+
private String inputStr;
26+
27+
public static class TestObject {
28+
private int field1;
29+
private int field2;
30+
31+
@JsonCreator
32+
@com.jsoniter.annotation.JsonCreator
33+
public TestObject(
34+
@JsonProperty("field1") @com.jsoniter.annotation.JsonProperty("field1") int field1,
35+
@JsonProperty("field2") @com.jsoniter.annotation.JsonProperty("field2") int field2) {
36+
this.field1 = field1;
37+
this.field2 = field2;
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return "TestObject{" +
43+
"field1=" + field1 +
44+
", field2=" + field2 +
45+
'}';
46+
}
47+
}
48+
49+
50+
private JsonIterator iter;
51+
52+
@Setup(Level.Trial)
53+
public void benchSetup() {
54+
inputStr = "{'field1':100,'field2':101}";
55+
input = inputStr.replace('\'', '"').getBytes();
56+
iter = JsonIterator.parse(input);
57+
typeLiteral = new TypeLiteral<TestObject>() {
58+
};
59+
typeRef = new TypeReference<TestObject>() {
60+
};
61+
JsoniterAnnotationSupport.enable();
62+
jackson = new ObjectMapper();
63+
jackson.registerModule(new AfterburnerModule());
64+
}
65+
66+
@Test
67+
public void test() throws IOException {
68+
benchSetup();
69+
System.out.println(withJsoniter());
70+
System.out.println(withJackson());
71+
}
72+
73+
@Benchmark
74+
public void withJsoniter(Blackhole bh) throws IOException {
75+
bh.consume(withJsoniter());
76+
}
77+
78+
@Benchmark
79+
public void withJackson(Blackhole bh) throws IOException {
80+
bh.consume(withJackson());
81+
}
82+
83+
public static void main(String[] args) throws Exception {
84+
Main.main(new String[]{
85+
"ConstructorBinding",
86+
"-i", "5",
87+
"-wi", "5",
88+
"-f", "1",
89+
});
90+
}
91+
92+
private TestObject withJsoniter() throws IOException {
93+
iter.reset();
94+
return iter.read(typeLiteral);
95+
}
96+
97+
private TestObject withJackson() throws IOException {
98+
return jackson.readValue(input, typeRef);
99+
}
100+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.jsoniter.demo;
2+
3+
import com.alibaba.fastjson.annotation.JSONField;
4+
import com.alibaba.fastjson.parser.DefaultJSONParser;
5+
import com.alibaba.fastjson.parser.ParserConfig;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.core.type.TypeReference;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
11+
import com.jsoniter.JsonIterator;
12+
import com.jsoniter.ReflectionDecoder;
13+
import com.jsoniter.annotation.JsonIgnore;
14+
import com.jsoniter.annotation.JsoniterAnnotationSupport;
15+
import com.jsoniter.spi.ExtensionManager;
16+
import com.jsoniter.spi.TypeLiteral;
17+
import org.junit.Test;
18+
import org.openjdk.jmh.Main;
19+
import org.openjdk.jmh.annotations.*;
20+
import org.openjdk.jmh.infra.Blackhole;
21+
22+
import java.io.IOException;
23+
24+
@State(Scope.Thread)
25+
public class PrivateFieldBinding {
26+
27+
private TypeLiteral<TestObject> typeLiteral;
28+
private ObjectMapper jackson;
29+
private byte[] input;
30+
private TypeReference<TestObject> typeRef;
31+
private String inputStr;
32+
33+
public static class TestObject {
34+
@JsonIgnore
35+
private int field1;
36+
@JsonIgnore
37+
private int field2;
38+
39+
@JsonCreator
40+
@com.jsoniter.annotation.JsonCreator
41+
private TestObject(
42+
@JsonProperty("field1") @com.jsoniter.annotation.JsonProperty("field1") int field1,
43+
@JsonProperty("field2") @com.jsoniter.annotation.JsonProperty("field2") int field2) {
44+
this.field1 = field1;
45+
this.field2 = field2;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "TestObject{" +
51+
"field1=" + field1 +
52+
", field2=" + field2 +
53+
'}';
54+
}
55+
}
56+
57+
58+
private JsonIterator iter;
59+
60+
@Setup(Level.Trial)
61+
public void benchSetup() {
62+
inputStr = "{'field1':100,'field2':101}";
63+
input = inputStr.replace('\'', '"').getBytes();
64+
iter = JsonIterator.parse(input);
65+
typeLiteral = new TypeLiteral<TestObject>() {
66+
};
67+
typeRef = new TypeReference<TestObject>() {
68+
};
69+
JsoniterAnnotationSupport.enable();
70+
ExtensionManager.registerTypeDecoder(TestObject.class, new ReflectionDecoder(TestObject.class));
71+
jackson = new ObjectMapper();
72+
jackson.registerModule(new AfterburnerModule());
73+
}
74+
75+
@Test
76+
public void test() throws IOException {
77+
benchSetup();
78+
System.out.println(withJsoniter());
79+
System.out.println(withJackson());
80+
}
81+
82+
@Benchmark
83+
public void withJsoniter(Blackhole bh) throws IOException {
84+
bh.consume(withJsoniter());
85+
}
86+
87+
@Benchmark
88+
public void withJackson(Blackhole bh) throws IOException {
89+
bh.consume(withJackson());
90+
}
91+
92+
public static void main(String[] args) throws Exception {
93+
Main.main(new String[]{
94+
"PrivateFieldBinding.*",
95+
"-i", "5",
96+
"-wi", "5",
97+
"-f", "1",
98+
});
99+
}
100+
101+
private TestObject withJsoniter() throws IOException {
102+
iter.reset();
103+
return iter.read(typeLiteral);
104+
}
105+
106+
private TestObject withJackson() throws IOException {
107+
return jackson.readValue(input, typeRef);
108+
}
109+
}

demo/src/main/java/com/jsoniter/demo/ObjectBinding.java renamed to demo/src/main/java/com/jsoniter/demo/SimpleObjectBinding.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jsoniter.demo;
22

3+
import com.alibaba.fastjson.parser.DefaultJSONParser;
34
import com.dslplatform.json.CompiledJson;
45
import com.dslplatform.json.DslJson;
56
import com.fasterxml.jackson.core.type.TypeReference;
@@ -15,14 +16,15 @@
1516
import java.io.IOException;
1617

1718
@State(Scope.Thread)
18-
public class ObjectBinding {
19+
public class SimpleObjectBinding {
1920

2021
private TypeLiteral<TestObject> typeLiteral;
2122
private ObjectMapper jackson;
2223
private byte[] input;
2324
private TypeReference<TestObject> typeRef;
2425
private DslJson dslJson;
2526
private Class<TestObject> clazz;
27+
private String inputStr;
2628

2729
@CompiledJson
2830
public static class TestObject {
@@ -43,7 +45,8 @@ public String toString() {
4345

4446
@Setup(Level.Trial)
4547
public void benchSetup() {
46-
input = "{'field1':100,'field2':101}".replace('\'', '"').getBytes();
48+
inputStr = "{'field1':100,'field2':101}";
49+
input = inputStr.replace('\'', '"').getBytes();
4750
iter = JsonIterator.parse(input);
4851
typeLiteral = new TypeLiteral<TestObject>() {
4952
};
@@ -63,6 +66,7 @@ public void test() throws IOException {
6366
System.out.println(withBindApiStrictMode());
6467
System.out.println(withJackson());
6568
System.out.println(withDsljson());
69+
System.out.println(withFastjson());
6670
}
6771

6872
@Benchmark
@@ -90,8 +94,14 @@ public void withDsljson(Blackhole bh) throws IOException {
9094
bh.consume(withDsljson());
9195
}
9296

97+
@Benchmark
98+
public void withFastjson(Blackhole bh) throws IOException {
99+
bh.consume(withFastjson());
100+
}
101+
93102
public static void main(String[] args) throws Exception {
94103
Main.main(new String[]{
104+
"SimpleObjectBinding.*",
95105
"-i", "5",
96106
"-wi", "5",
97107
"-f", "1"
@@ -134,4 +144,8 @@ private TestObject withJackson() throws IOException {
134144
private TestObject withDsljson() throws IOException {
135145
return (TestObject) dslJson.deserialize(clazz, input, input.length);
136146
}
147+
148+
private TestObject withFastjson() {
149+
return new DefaultJSONParser(inputStr).parseObject(TestObject.class);
150+
}
137151
}

src/main/java/com/jsoniter/ReflectionDecoder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ private final void init(Class clazz) throws Exception {
4141
}
4242
this.ctor = desc.ctor.ctor;
4343
this.staticFactory = desc.ctor.staticFactory;
44+
if (this.ctor == null && this.staticFactory == null) {
45+
throw new JsonException("no constructor for: " + desc.clazz);
46+
}
4447
fields = desc.fields;
4548
for (Binding field : fields) {
4649
tempIdx = addBinding(clazz, tempIdx, field);

src/main/java/com/jsoniter/annotation/JsoniterAnnotationSupport.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.lang.reflect.Constructor;
88
import java.lang.reflect.Method;
99
import java.lang.reflect.Modifier;
10+
import java.util.ArrayList;
11+
import java.util.Arrays;
12+
import java.util.List;
1013

1114
public class JsoniterAnnotationSupport extends EmptyExtension {
1215

@@ -29,7 +32,7 @@ public void updateClassDescriptor(ClassDescriptor desc) {
2932
}
3033
}
3134
}
32-
for (Constructor ctor : desc.clazz.getConstructors()) {
35+
for (Constructor ctor : desc.clazz.getDeclaredConstructors()) {
3336
Annotation jsonCreator = ctor.getAnnotation(JsonCreator.class);
3437
if (jsonCreator == null) {
3538
continue;
@@ -51,7 +54,13 @@ public void updateClassDescriptor(ClassDescriptor desc) {
5154
desc.ctor.parameters.add(binding);
5255
}
5356
}
54-
for (Method method : desc.clazz.getMethods()) {
57+
List<Method> allMethods = new ArrayList<Method>();
58+
Class current = desc.clazz;
59+
while (current != null) {
60+
allMethods.addAll(Arrays.asList(current.getDeclaredMethods()));
61+
current = current.getSuperclass();
62+
}
63+
for (Method method : allMethods) {
5564
if (!Modifier.isStatic(method.getModifiers())) {
5665
continue;
5766
}

0 commit comments

Comments
 (0)