7
7
import com .fasterxml .jackson .core .JsonGenerator ;
8
8
import com .fasterxml .jackson .core .type .TypeReference ;
9
9
import com .fasterxml .jackson .databind .*;
10
- import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
11
10
import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
12
11
import com .fasterxml .jackson .databind .jsontype .TypeResolverBuilder ;
13
12
import com .fasterxml .jackson .databind .module .SimpleModule ;
14
- import com .fasterxml .jackson .databind .module .SimpleSerializers ;
15
- import org .junit .Test ;
16
13
17
14
public class TestKeySerializers extends BaseMapTest
18
15
{
@@ -56,12 +53,19 @@ public String toLC() {
56
53
57
54
static class ABCKeySerializer extends JsonSerializer <ABC > {
58
55
@ Override
59
- public void serialize (ABC value , JsonGenerator jgen ,
56
+ public void serialize (ABC value , JsonGenerator gen ,
60
57
SerializerProvider provider ) throws IOException {
61
- jgen .writeFieldName ("xxx" +value );
58
+ gen .writeFieldName ("xxx" +value );
62
59
}
63
60
}
64
61
62
+ @ JsonSerialize (keyUsing = ABCKeySerializer .class )
63
+ public static enum ABCMixin { }
64
+
65
+ public static enum Outer {
66
+ inner ;
67
+ }
68
+
65
69
static class ABCMapWrapper {
66
70
public Map <ABC ,String > stuff = new HashMap <ABC ,String >();
67
71
public ABCMapWrapper () {
@@ -120,7 +124,7 @@ public void testKarl() throws IOException {
120
124
assertEquals ("{\" map\" :{\" Karl\" :1}}" , serialized );
121
125
}
122
126
123
- // [Issue #75]: caching of KeySerializers
127
+ // [databind #75]: caching of KeySerializers
124
128
public void testBoth () throws IOException
125
129
{
126
130
// Let's NOT use shared one, to ensure caching starts from clean slate
@@ -144,32 +148,21 @@ public void testCustomForEnum() throws IOException
144
148
assertEquals ("{\" stuff\" :{\" xxxB\" :\" bar\" }}" , json );
145
149
}
146
150
147
- @ JsonSerialize (keyUsing = ABCKeySerializer .class )
148
- public static enum ABCMixin { }
149
-
150
- public static enum Outer {
151
- inner ;
152
- }
153
-
154
- public void testCustomEnumInnerMapKey () {
151
+ public void testCustomEnumInnerMapKey () throws Exception {
155
152
Map <Outer , Object > outerMap = new HashMap <Outer , Object >();
156
153
Map <ABC , Map <String , String >> map = new EnumMap <ABC , Map <String , String >>(ABC .class );
157
154
Map <String , String > innerMap = new HashMap <String , String >();
158
155
innerMap .put ("one" , "1" );
159
156
map .put (ABC .A , innerMap );
160
157
outerMap .put (Outer .inner , map );
161
158
final ObjectMapper mapper = new ObjectMapper ();
162
- SimpleModule mod = new SimpleModule ("test" ) {
163
- @ Override
164
- public void setupModule (SetupContext context ) {
165
- context .setMixInAnnotations (ABC .class , ABCMixin .class );
166
- SimpleSerializers keySerializers = new SimpleSerializers ();
167
- keySerializers .addSerializer (ABC .class , new ABCKeySerializer ());
168
- context .addKeySerializers (keySerializers );
169
- }
170
- };
159
+ SimpleModule mod = new SimpleModule ("test" );
160
+ mod .setMixInAnnotation (ABC .class , ABCMixin .class );
161
+ mod .addKeySerializer (ABC .class , new ABCKeySerializer ());
171
162
mapper .registerModule (mod );
163
+
172
164
JsonNode tree = mapper .convertValue (outerMap , JsonNode .class );
165
+
173
166
JsonNode innerNode = tree .get ("inner" );
174
167
String key = innerNode .fieldNames ().next ();
175
168
assertEquals ("xxxA" , key );
0 commit comments