1 package com.fasterxml.jackson.failing;
2 
3 import java.util.ArrayList;
4 import java.util.List;
5 
6 import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7 import com.fasterxml.jackson.databind.*;
8 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
9 
10 public class StaticTyping1515Test extends BaseMapTest
11 {
12     static abstract class Base {
13         public int a = 1;
14     }
15 
16     static class Derived extends Base {
17         public int b = 2;
18     }
19 
20     @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
21     static abstract class BaseDynamic {
22         public int a = 3;
23     }
24 
25     static class DerivedDynamic extends BaseDynamic {
26         public int b = 4;
27     }
28 
29     @JsonPropertyOrder({ "value", "aValue", "dValue" })
30     static class Issue515Singles {
31         public Base value = new Derived();
32 
33         @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
34         public Base aValue = new Derived();
35 
36         public BaseDynamic dValue = new DerivedDynamic();
37     }
38 
39     @JsonPropertyOrder({ "list", "aList", "dList" })
40     static class Issue515Lists {
41         public List<Base> list = new ArrayList<>(); {
list.add(new Derived())42             list.add(new Derived());
43         }
44 
45         @JsonSerialize(typing = JsonSerialize.Typing.DYNAMIC)
46         public List<Base> aList = new ArrayList<>(); {
aList.add(new Derived())47             aList.add(new Derived());
48         }
49 
50         public List<BaseDynamic> dList = new ArrayList<>(); {
dList.add(new DerivedDynamic())51             dList.add(new DerivedDynamic());
52         }
53     }
54 
55     /*
56     /**********************************************************
57     /* Test methods
58     /**********************************************************
59      */
60 
61     private final ObjectMapper STAT_MAPPER = jsonMapperBuilder()
62             .enable(MapperFeature.USE_STATIC_TYPING)
63             .build();
64 
testStaticTypingForProperties()65     public void testStaticTypingForProperties() throws Exception
66     {
67         String json = STAT_MAPPER.writeValueAsString(new Issue515Singles());
68         assertEquals(aposToQuotes("{'value':{'a':1},'aValue':{'a':1,'b':2},'dValue':{'a':3,'b':4}}"), json);
69     }
70 
testStaticTypingForLists()71     public void testStaticTypingForLists() throws Exception
72     {
73         String json = STAT_MAPPER.writeValueAsString(new Issue515Lists());
74         assertEquals(aposToQuotes("{'list':[{'a':1}],'aList':[{'a':1,'b':2}],'dList:[{'a':3,'b':4}]}"), json);
75     }
76 }
77