|
| 1 | +package com.fasterxml.jackson.databind.creators; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 6 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 7 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 8 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 9 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 10 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author dharaburda |
| 14 | + */ |
| 15 | +public class TestCreatorsWithIdentity extends BaseMapTest { |
| 16 | + |
| 17 | + @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope=Parent.class) |
| 18 | + public static class Parent { |
| 19 | + @JsonProperty("id") |
| 20 | + String id; |
| 21 | + |
| 22 | + @JsonProperty |
| 23 | + String parentProp; |
| 24 | + |
| 25 | + @JsonCreator |
| 26 | + public Parent(@JsonProperty("parentProp") String parentProp) { |
| 27 | + this.parentProp = parentProp; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + public static class Child { |
| 33 | + @JsonProperty |
| 34 | + Parent parent; |
| 35 | + |
| 36 | + @JsonProperty |
| 37 | + String childProp; |
| 38 | + |
| 39 | + @JsonCreator |
| 40 | + public Child(@JsonProperty("parent") Parent parent, @JsonProperty("childProp") String childProp) { |
| 41 | + this.parent = parent; |
| 42 | + this.childProp = childProp; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); |
| 47 | + |
| 48 | + public void test() throws IOException { |
| 49 | + String parentStr = "{\"id\" : \"1\", \"parentProp\" : \"parent\"}"; |
| 50 | + String childStr = "{\"childProp\" : \"child\", \"parent\" : " + parentStr + "}"; |
| 51 | + Parent parent = JSON_MAPPER.readValue(parentStr, Parent.class); |
| 52 | + Child child = JSON_MAPPER.readValue(childStr, Child.class); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments