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