@@ -17,28 +17,66 @@ public class TestCustomTypeIdResolver extends BaseMapTest
17
17
{
18
18
@ JsonTypeInfo (use =Id .CUSTOM , include =As .WRAPPER_OBJECT )
19
19
@ JsonTypeIdResolver (CustomResolver .class )
20
- static class CustomBean {
20
+ static abstract class CustomBean { }
21
+
22
+ static class CustomBeanImpl extends CustomBean {
21
23
public int x ;
22
24
23
- public CustomBean () { }
24
- public CustomBean (int x ) { this .x = x ; }
25
+ public CustomBeanImpl () { }
26
+ public CustomBeanImpl (int x ) { this .x = x ; }
25
27
}
26
-
27
- static class CustomResolver implements TypeIdResolver
28
- {
28
+
29
+ static class ExtBeanWrapper {
30
+ @ JsonTypeInfo (use =Id .CUSTOM , include =As .EXTERNAL_PROPERTY , property ="type" )
31
+ @ JsonTypeIdResolver (ExtResolver .class )
32
+ public ExtBean value ;
33
+ }
34
+
35
+ static class CustomResolver extends CustomResolverBase {
36
+ // yes, static: just for test purposes, not real use
29
37
static List <JavaType > initTypes ;
30
38
31
- public CustomResolver () { }
32
-
33
- @ Override
34
- public Id getMechanism () {
35
- return Id .CUSTOM ;
39
+ public CustomResolver () {
40
+ super (CustomBean .class , CustomBeanImpl .class );
36
41
}
37
42
38
43
@ Override
39
- public String idFromValue (Object value )
40
- {
41
- if (value .getClass () == CustomBean .class ) {
44
+ public void init (JavaType baseType ) {
45
+ if (initTypes != null ) {
46
+ initTypes .add (baseType );
47
+ }
48
+ }
49
+ }
50
+
51
+ static abstract class ExtBean { }
52
+
53
+ static class ExtBeanImpl extends ExtBean {
54
+ public int y ;
55
+
56
+ public ExtBeanImpl () { }
57
+ public ExtBeanImpl (int y ) { this .y = y ; }
58
+ }
59
+
60
+ static class ExtResolver extends CustomResolverBase {
61
+ public ExtResolver () {
62
+ super (ExtBean .class , ExtBeanImpl .class );
63
+ }
64
+ }
65
+
66
+ static class CustomResolverBase implements TypeIdResolver
67
+ {
68
+ protected final Class <?> superType ;
69
+ protected final Class <?> subType ;
70
+
71
+ public CustomResolverBase (Class <?> baseType , Class <?> implType ) {
72
+ superType = baseType ;
73
+ subType = implType ;
74
+ }
75
+
76
+ @ Override public Id getMechanism () { return Id .CUSTOM ; }
77
+
78
+ @ Override public String idFromValue (Object value ) {
79
+ if (superType .isAssignableFrom (value .getClass ())) {
42
80
return "*" ;
43
81
}
44
82
return "unknown" ;
@@ -50,17 +88,13 @@ public String idFromValueAndType(Object value, Class<?> type) {
50
88
}
51
89
52
90
@ Override
53
- public void init (JavaType baseType ) {
54
- if (initTypes != null ) {
55
- initTypes .add (baseType );
56
- }
57
- }
91
+ public void init (JavaType baseType ) { }
58
92
59
93
@ Override
60
94
public JavaType typeFromId (String id )
61
95
{
62
96
if ("*" .equals (id )) {
63
- return TypeFactory .defaultInstance ().constructType (CustomBean . class );
97
+ return TypeFactory .defaultInstance ().constructType (subType );
64
98
}
65
99
return null ;
66
100
}
@@ -77,24 +111,38 @@ public String idFromBaseType() {
77
111
/**********************************************************
78
112
*/
79
113
114
+ private final ObjectMapper MAPPER = objectMapper ();
115
+
80
116
// for [JACKSON-359]
81
117
public void testCustomTypeIdResolver () throws Exception
82
118
{
83
- ObjectMapper m = new ObjectMapper ();
84
119
List <JavaType > types = new ArrayList <JavaType >();
85
120
CustomResolver .initTypes = types ;
86
- String json = m .writeValueAsString (new CustomBean [] { new CustomBean (28 ) });
121
+ String json = MAPPER .writeValueAsString (new CustomBean [] { new CustomBeanImpl (28 ) });
87
122
assertEquals ("[{\" *\" :{\" x\" :28}}]" , json );
88
123
assertEquals (1 , types .size ());
89
124
assertEquals (CustomBean .class , types .get (0 ).getRawClass ());
90
125
91
126
types = new ArrayList <JavaType >();
92
127
CustomResolver .initTypes = types ;
93
- CustomBean [] result = m .readValue (json , CustomBean [].class );
128
+ CustomBean [] result = MAPPER .readValue (json , CustomBean [].class );
94
129
assertNotNull (result );
95
130
assertEquals (1 , result .length );
96
- assertEquals (28 , result [0 ].x );
131
+ assertEquals (28 , (( CustomBeanImpl ) result [0 ]) .x );
97
132
assertEquals (1 , types .size ());
98
133
assertEquals (CustomBean .class , types .get (0 ).getRawClass ());
99
134
}
135
+
136
+ public void testCustomWithExternal () throws Exception
137
+ {
138
+ ExtBeanWrapper w = new ExtBeanWrapper ();
139
+ w .value = new ExtBeanImpl (12 );
140
+
141
+ String json = MAPPER .writeValueAsString (w );
142
+
143
+ ExtBeanWrapper out = MAPPER .readValue (json , ExtBeanWrapper .class );
144
+ assertNotNull (out );
145
+
146
+ assertEquals (12 , ((ExtBeanImpl ) out .value ).y );
147
+ }
100
148
}
0 commit comments