|
| 1 | +package com.jsoniter; |
| 2 | + |
| 3 | +import com.jsoniter.annotation.JsonExtraProperties; |
| 4 | +import com.jsoniter.annotation.JsonObject; |
| 5 | +import com.jsoniter.any.Any; |
| 6 | +import com.jsoniter.spi.JsonException; |
| 7 | +import junit.framework.TestCase; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.Map; |
| 11 | + |
| 12 | +public class TestAnnotationJsonObject extends TestCase { |
| 13 | + |
| 14 | + @JsonObject(asExtraForUnknownProperties = true) |
| 15 | + public static class TestObject9 { |
| 16 | + @JsonExtraProperties |
| 17 | + public Map<String, Any> extraProperties; |
| 18 | + } |
| 19 | + |
| 20 | + public void test_extra_properties() throws IOException { |
| 21 | + JsonIterator iter = JsonIterator.parse("{\"field1\": 100}"); |
| 22 | + TestObject9 obj = iter.read(TestObject9.class); |
| 23 | + assertEquals(100, obj.extraProperties.get("field1").toInt()); |
| 24 | + } |
| 25 | + |
| 26 | + @JsonObject(asExtraForUnknownProperties = true) |
| 27 | + public static class TestObject13 { |
| 28 | + } |
| 29 | + |
| 30 | + public void test_unknown_properties() throws IOException { |
| 31 | + JsonIterator iter = JsonIterator.parse("{\"field-1\": 100, \"field-1\": 101}"); |
| 32 | + try { |
| 33 | + iter.read(TestObject13.class); |
| 34 | + fail(); |
| 35 | + } catch (JsonException e) { |
| 36 | + System.out.println(e); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @JsonObject(unknownPropertiesBlacklist = {"field1"}) |
| 41 | + public static class TestObject15 { |
| 42 | + } |
| 43 | + |
| 44 | + public void test_unknown_properties_blacklist() throws IOException { |
| 45 | + JsonIterator iter = JsonIterator.parse("{\"field1\": 100}"); |
| 46 | + try { |
| 47 | + iter.read(TestObject15.class); |
| 48 | + fail(); |
| 49 | + } catch (JsonException e) { |
| 50 | + System.out.println(e); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @JsonObject(asExtraForUnknownProperties = true) |
| 55 | + public static class TestObject14 { |
| 56 | + public int id; |
| 57 | + } |
| 58 | + |
| 59 | + public void test_no_unknown_properties() throws IOException { |
| 60 | + String json = "{ \"id\": 100 }"; |
| 61 | + TestObject14 obj = JsonIterator.deserialize(json, TestObject14.class); |
| 62 | + assertEquals(100, obj.id); |
| 63 | + } |
| 64 | +} |
0 commit comments