@@ -132,6 +132,24 @@ protected enum EnumWithJsonProperty {
132
132
A ;
133
133
}
134
134
135
+ // [databind#2871]: add `@JsonKey`
136
+ protected enum EnumWithJsonKey {
137
+ A ("a" ), B ("b" );
138
+ private final String name ;
139
+ private EnumWithJsonKey (String n ) {
140
+ name = n ;
141
+ }
142
+
143
+ @ Override
144
+ public String toString () { return name ; }
145
+
146
+ @ JsonKey
147
+ public String externalKey () { return "key:" +name ; }
148
+
149
+ @ JsonValue
150
+ public String externalValue () { return "value:" +name ; }
151
+ }
152
+
135
153
/*
136
154
/**********************************************************************
137
155
/* Test methods
@@ -298,6 +316,20 @@ public void testEnumsWithJsonPropertyAsKey() throws Exception
298
316
input .put (EnumWithJsonProperty .A , "b" );
299
317
assertEquals ("{\" aleph\" :\" b\" }" , MAPPER .writeValueAsString (input ));
300
318
}
319
+
320
+ // [databind#2871]
321
+ public void testEnumWithJsonKey () throws Exception
322
+ {
323
+ // First with EnumMap
324
+ EnumMap <EnumWithJsonKey , EnumWithJsonKey > input1 = new EnumMap <>(EnumWithJsonKey .class );
325
+ input1 .put (EnumWithJsonKey .A , EnumWithJsonKey .B );
326
+ assertEquals (a2q ("{'key:a':'value:b'}" ), MAPPER .writeValueAsString (input1 ));
327
+
328
+ // Then regular Map with Enums
329
+ Map <EnumWithJsonKey , EnumWithJsonKey > input2
330
+ = Collections .singletonMap (EnumWithJsonKey .A , EnumWithJsonKey .B );
331
+ assertEquals (a2q ("{'key:a':'value:b'}" ), MAPPER .writeValueAsString (input2 ));
332
+ }
301
333
}
302
334
303
335
// [JACKSON-757], non-inner enum
0 commit comments