|
4 | 4 |
|
5 | 5 | import com.fasterxml.jackson.annotation.JacksonInject;
|
6 | 6 | import com.fasterxml.jackson.annotation.JsonCreator;
|
| 7 | +import com.fasterxml.jackson.annotation.JsonProperty; |
7 | 8 |
|
8 | 9 | import com.fasterxml.jackson.core.JsonParser;
|
9 | 10 | import com.fasterxml.jackson.core.JsonToken;
|
@@ -82,6 +83,32 @@ public MapBean(Map<String, Long> map) {
|
82 | 83 | }
|
83 | 84 | }
|
84 | 85 |
|
| 86 | + // [databind#2353]: allow delegating and properties-based |
| 87 | + static class SuperToken2353 { |
| 88 | + public long time; |
| 89 | + public String username; |
| 90 | + |
| 91 | + @JsonCreator(mode=JsonCreator.Mode.DELEGATING) // invoked when a string is passed |
| 92 | + public static SuperToken2353 from(String username) { |
| 93 | + SuperToken2353 token = new SuperToken2353(); |
| 94 | + token.username = username; |
| 95 | + token.time = System.currentTimeMillis(); |
| 96 | + return token; |
| 97 | + } |
| 98 | + |
| 99 | + @JsonCreator(mode=JsonCreator.Mode.PROPERTIES) // invoked when an object is passed, pre-validating property existence |
| 100 | + public static SuperToken2353 create( |
| 101 | + @JsonProperty("name") String username, |
| 102 | + @JsonProperty("time") long time) |
| 103 | + { |
| 104 | + SuperToken2353 token = new SuperToken2353(); |
| 105 | + token.username = username; |
| 106 | + token.time = time; |
| 107 | + |
| 108 | + return token; |
| 109 | + } |
| 110 | + } |
| 111 | + |
85 | 112 | /*
|
86 | 113 | /**********************************************************
|
87 | 114 | /* Test methods
|
@@ -179,4 +206,17 @@ public void testIssue465() throws Exception
|
179 | 206 | bean = MAPPER.readValue(EMPTY_JSON, MapBean.class);
|
180 | 207 | assertEquals(0, bean.map.size());
|
181 | 208 | }
|
| 209 | + |
| 210 | + // [databind#2353]: allow delegating and properties-based |
| 211 | + public void testMultipleCreators2353() throws Exception |
| 212 | + { |
| 213 | + // first, test delegating |
| 214 | + SuperToken2353 result = MAPPER.readValue(quote("Bob"), SuperToken2353.class); |
| 215 | + assertEquals("Bob", result.username); |
| 216 | + |
| 217 | + // and then properties-based |
| 218 | + result = MAPPER.readValue(aposToQuotes("{'name':'Billy', 'time':123}"), SuperToken2353.class); |
| 219 | + assertEquals("Billy", result.username); |
| 220 | + assertEquals(123L, result.time); |
| 221 | + } |
182 | 222 | }
|
0 commit comments