|
| 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 | +} |
0 commit comments