|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | + |
| 7 | +public class DelegatingExternalProperty1003Test extends BaseMapTest |
| 8 | +{ |
| 9 | + static class HeroBattle { |
| 10 | + |
| 11 | + private final Hero hero; |
| 12 | + |
| 13 | + private HeroBattle(Hero hero) { |
| 14 | + if (hero == null) throw new Error(); |
| 15 | + this.hero = hero; |
| 16 | + } |
| 17 | + |
| 18 | + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "heroType") |
| 19 | + public Hero getHero() { |
| 20 | + return hero; |
| 21 | + } |
| 22 | + |
| 23 | + @JsonCreator |
| 24 | + static HeroBattle fromJson(Delegate json) { |
| 25 | + return new HeroBattle(json.hero); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + static class Delegate { |
| 30 | + @JsonProperty |
| 31 | + @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "heroType") |
| 32 | + public Hero hero; |
| 33 | + } |
| 34 | + |
| 35 | + public interface Hero { } |
| 36 | + |
| 37 | + static class Superman implements Hero { |
| 38 | + public String getName() { |
| 39 | + return "superman"; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void testExtrnalPropertyDelegatingCreator() throws Exception |
| 44 | + { |
| 45 | + ObjectMapper mapper = new ObjectMapper(); |
| 46 | + |
| 47 | + final String json = mapper.writeValueAsString(new HeroBattle(new Superman())); |
| 48 | + |
| 49 | +//System.err.println("JSON: "+json); |
| 50 | + final HeroBattle battle = mapper.readValue(json, HeroBattle.class); |
| 51 | + |
| 52 | + assert battle.getHero() instanceof Superman; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +} |
0 commit comments