@@ -17,19 +17,27 @@ 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 ; }
27
+ }
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 ;
25
33
}
26
34
27
35
static class CustomResolver implements TypeIdResolver
28
36
{
29
37
static List <JavaType > initTypes ;
30
38
31
39
public CustomResolver () { }
32
-
40
+
33
41
@ Override
34
42
public Id getMechanism () {
35
43
return Id .CUSTOM ;
@@ -38,7 +46,7 @@ public Id getMechanism() {
38
46
@ Override
39
47
public String idFromValue (Object value )
40
48
{
41
- if (value . getClass () == CustomBean . class ) {
49
+ if (value instanceof CustomBean ) {
42
50
return "*" ;
43
51
}
44
52
return "unknown" ;
@@ -60,7 +68,7 @@ public void init(JavaType baseType) {
60
68
public JavaType typeFromId (String id )
61
69
{
62
70
if ("*" .equals (id )) {
63
- return TypeFactory .defaultInstance ().constructType (CustomBean .class );
71
+ return TypeFactory .defaultInstance ().constructType (CustomBeanImpl .class );
64
72
}
65
73
return null ;
66
74
}
@@ -77,24 +85,40 @@ public String idFromBaseType() {
77
85
/**********************************************************
78
86
*/
79
87
88
+ private final ObjectMapper MAPPER = objectMapper ();
89
+
80
90
// for [JACKSON-359]
81
91
public void testCustomTypeIdResolver () throws Exception
82
92
{
83
- ObjectMapper m = new ObjectMapper ();
84
93
List <JavaType > types = new ArrayList <JavaType >();
85
94
CustomResolver .initTypes = types ;
86
- String json = m .writeValueAsString (new CustomBean [] { new CustomBean (28 ) });
95
+ String json = MAPPER .writeValueAsString (new CustomBean [] { new CustomBeanImpl (28 ) });
87
96
assertEquals ("[{\" *\" :{\" x\" :28}}]" , json );
88
97
assertEquals (1 , types .size ());
89
98
assertEquals (CustomBean .class , types .get (0 ).getRawClass ());
90
99
91
100
types = new ArrayList <JavaType >();
92
101
CustomResolver .initTypes = types ;
93
- CustomBean [] result = m .readValue (json , CustomBean [].class );
102
+ CustomBean [] result = MAPPER .readValue (json , CustomBean [].class );
94
103
assertNotNull (result );
95
104
assertEquals (1 , result .length );
96
- assertEquals (28 , result [0 ].x );
105
+ assertEquals (28 , (( CustomBeanImpl ) result [0 ]) .x );
97
106
assertEquals (1 , types .size ());
98
107
assertEquals (CustomBean .class , types .get (0 ).getRawClass ());
99
108
}
109
+
110
+ public void testCustomWithExternal () throws Exception
111
+ {
112
+ CustomBeanWrapper w = new CustomBeanWrapper ();
113
+ w .value = new CustomBeanImpl (12 );
114
+
115
+ String json = MAPPER .writeValueAsString (w );
116
+
117
+ System .out .println ("JSON = " +json );
118
+
119
+ CustomBeanWrapper out = MAPPER .readValue (json , CustomBeanWrapper .class );
120
+ assertNotNull (out );
121
+
122
+ assertEquals (12 , ((CustomBeanImpl ) out .value ).x );
123
+ }
100
124
}
0 commit comments