@@ -26,27 +26,57 @@ public CustomBeanImpl() { }
26
26
public CustomBeanImpl (int x ) { this .x = x ; }
27
27
}
28
28
29
- static class CustomBeanWrapper {
30
- // @JsonTypeInfo(use=Id.NONE , include=As.EXTERNAL_PROPERTY, property="type")
31
- @ JsonTypeInfo ( use = Id . CUSTOM , include = As . PROPERTY , property = "type" )
32
- public CustomBean value ;
29
+ static class ExtBeanWrapper {
30
+ @ JsonTypeInfo (use =Id .CUSTOM , include =As .EXTERNAL_PROPERTY , property ="type" )
31
+ @ JsonTypeIdResolver ( ExtResolver . class )
32
+ public ExtBean value ;
33
33
}
34
-
35
- static class CustomResolver implements TypeIdResolver
36
- {
34
+
35
+ static class CustomResolver extends CustomResolverBase {
36
+ // yes, static: just for test purposes, not real use
37
37
static List <JavaType > initTypes ;
38
38
39
- public CustomResolver () { }
39
+ public CustomResolver () {
40
+ super (CustomBean .class , CustomBeanImpl .class );
41
+ }
40
42
41
43
@ Override
42
- public Id getMechanism () {
43
- return Id .CUSTOM ;
44
+ public void init (JavaType baseType ) {
45
+ if (initTypes != null ) {
46
+ initTypes .add (baseType );
47
+ }
44
48
}
49
+ }
50
+
51
+ static abstract class ExtBean { }
45
52
46
- @ Override
47
- public String idFromValue (Object value )
48
- {
49
- if (value instanceof CustomBean ) {
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 ())) {
50
80
return "*" ;
51
81
}
52
82
return "unknown" ;
@@ -58,17 +88,13 @@ public String idFromValueAndType(Object value, Class<?> type) {
58
88
}
59
89
60
90
@ Override
61
- public void init (JavaType baseType ) {
62
- if (initTypes != null ) {
63
- initTypes .add (baseType );
64
- }
65
- }
91
+ public void init (JavaType baseType ) { }
66
92
67
93
@ Override
68
94
public JavaType typeFromId (String id )
69
95
{
70
96
if ("*" .equals (id )) {
71
- return TypeFactory .defaultInstance ().constructType (CustomBeanImpl . class );
97
+ return TypeFactory .defaultInstance ().constructType (subType );
72
98
}
73
99
return null ;
74
100
}
@@ -109,16 +135,14 @@ public void testCustomTypeIdResolver() throws Exception
109
135
110
136
public void testCustomWithExternal () throws Exception
111
137
{
112
- CustomBeanWrapper w = new CustomBeanWrapper ();
113
- w .value = new CustomBeanImpl (12 );
138
+ ExtBeanWrapper w = new ExtBeanWrapper ();
139
+ w .value = new ExtBeanImpl (12 );
114
140
115
141
String json = MAPPER .writeValueAsString (w );
116
142
117
- System .out .println ("JSON = " +json );
118
-
119
- CustomBeanWrapper out = MAPPER .readValue (json , CustomBeanWrapper .class );
143
+ ExtBeanWrapper out = MAPPER .readValue (json , ExtBeanWrapper .class );
120
144
assertNotNull (out );
121
145
122
- assertEquals (12 , ((CustomBeanImpl ) out .value ).x );
146
+ assertEquals (12 , ((ExtBeanImpl ) out .value ).y );
123
147
}
124
148
}
0 commit comments