@@ -26,6 +26,10 @@ private StdKeySerializers() { }
26
26
public static JsonSerializer <Object > getStdKeySerializer (SerializationConfig config ,
27
27
Class <?> rawKeyType , boolean useDefault )
28
28
{
29
+ // 24-Sep-2015, tatu: Important -- should ONLY consider types for which `@JsonValue`
30
+ // can not be used, since caller has not yet checked for that annotation
31
+ // This is why Enum types are not handled here quite yet
32
+
29
33
if (rawKeyType != null ) {
30
34
if (rawKeyType == String .class ) {
31
35
return DEFAULT_STRING_SERIALIZER ;
@@ -43,14 +47,31 @@ public static JsonSerializer<Object> getStdKeySerializer(SerializationConfig con
43
47
if (Calendar .class .isAssignableFrom (rawKeyType )) {
44
48
return new Default (Default .TYPE_CALENDAR , rawKeyType );
45
49
}
46
- // other types we know convert properly with 'toString()'?
50
+ // other JDK types we know convert properly with 'toString()'?
47
51
if (rawKeyType == java .util .UUID .class ) {
48
52
return new Default (Default .TYPE_TO_STRING , rawKeyType );
49
53
}
54
+
50
55
}
51
56
return useDefault ? DEFAULT_KEY_SERIALIZER : null ;
52
57
}
53
58
59
+ /**
60
+ * Method called if no specified key serializer was located; will return a
61
+ * "default" key serializer.
62
+ *
63
+ * @since 2.7
64
+ */
65
+ public static JsonSerializer <Object > getFallbackKeySerializer (SerializationConfig config ,
66
+ Class <?> rawKeyType ) {
67
+ if (rawKeyType != null ) {
68
+ if (rawKeyType .isEnum ()) {
69
+ return new Default (Default .TYPE_ENUM , rawKeyType );
70
+ }
71
+ }
72
+ return DEFAULT_KEY_SERIALIZER ;
73
+ }
74
+
54
75
/**
55
76
* @deprecated Since 2.5
56
77
*/
@@ -59,10 +80,13 @@ public static JsonSerializer<Object> getStdKeySerializer(JavaType keyType) {
59
80
return getStdKeySerializer (null , keyType .getRawClass (), true );
60
81
}
61
82
83
+ /**
84
+ * @deprecated since 2.7
85
+ */
62
86
public static JsonSerializer <Object > getDefault () {
63
87
return DEFAULT_KEY_SERIALIZER ;
64
88
}
65
-
89
+
66
90
/*
67
91
/**********************************************************
68
92
/* Standard implementations used
@@ -73,7 +97,8 @@ public static class Default extends StdSerializer<Object> {
73
97
final static int TYPE_DATE = 1 ;
74
98
final static int TYPE_CALENDAR = 2 ;
75
99
final static int TYPE_CLASS = 3 ;
76
- final static int TYPE_TO_STRING = 4 ;
100
+ final static int TYPE_ENUM = 4 ;
101
+ final static int TYPE_TO_STRING = 5 ;
77
102
78
103
protected final int _typeId ;
79
104
@@ -83,20 +108,27 @@ public Default(int typeId, Class<?> type) {
83
108
}
84
109
85
110
@ Override
86
- public void serialize (Object value , JsonGenerator jgen , SerializerProvider provider ) throws IOException , JsonGenerationException {
111
+ public void serialize (Object value , JsonGenerator g , SerializerProvider provider ) throws IOException {
87
112
switch (_typeId ) {
88
113
case TYPE_DATE :
89
- provider .defaultSerializeDateKey ((Date )value , jgen );
114
+ provider .defaultSerializeDateKey ((Date )value , g );
90
115
break ;
91
116
case TYPE_CALENDAR :
92
- provider .defaultSerializeDateKey (((Calendar ) value ).getTimeInMillis (), jgen );
117
+ provider .defaultSerializeDateKey (((Calendar ) value ).getTimeInMillis (), g );
93
118
break ;
94
119
case TYPE_CLASS :
95
- jgen .writeFieldName (((Class <?>)value ).getName ());
120
+ g .writeFieldName (((Class <?>)value ).getName ());
121
+ break ;
122
+ case TYPE_ENUM :
123
+ {
124
+ String str = provider .isEnabled (SerializationFeature .WRITE_ENUMS_USING_TO_STRING )
125
+ ? value .toString () : ((Enum <?>) value ).name ();
126
+ g .writeFieldName (str );
127
+ }
96
128
break ;
97
129
case TYPE_TO_STRING :
98
130
default :
99
- jgen .writeFieldName (value .toString ());
131
+ g .writeFieldName (value .toString ());
100
132
}
101
133
}
102
134
}
@@ -106,11 +138,11 @@ public static class StringKeySerializer extends StdSerializer<Object>
106
138
public StringKeySerializer () { super (String .class , false ); }
107
139
108
140
@ Override
109
- public void serialize (Object value , JsonGenerator jgen , SerializerProvider provider ) throws IOException , JsonGenerationException {
110
- jgen .writeFieldName ((String ) value );
141
+ public void serialize (Object value , JsonGenerator g , SerializerProvider provider ) throws IOException {
142
+ g .writeFieldName ((String ) value );
111
143
}
112
144
}
113
-
145
+
114
146
/*
115
147
/**********************************************************
116
148
/* Deprecated implementations: to be removed in future
@@ -124,8 +156,8 @@ public static class DateKeySerializer extends StdSerializer<Date> {
124
156
public DateKeySerializer () { super (Date .class ); }
125
157
126
158
@ Override
127
- public void serialize (Date value , JsonGenerator jgen , SerializerProvider provider ) throws IOException , JsonGenerationException {
128
- provider .defaultSerializeDateKey (value , jgen );
159
+ public void serialize (Date value , JsonGenerator g , SerializerProvider provider ) throws IOException {
160
+ provider .defaultSerializeDateKey (value , g );
129
161
}
130
162
}
131
163
@@ -134,10 +166,10 @@ public static class CalendarKeySerializer extends StdSerializer<Calendar> {
134
166
protected final static JsonSerializer <?> instance = new CalendarKeySerializer ();
135
167
136
168
public CalendarKeySerializer () { super (Calendar .class ); }
137
-
169
+
138
170
@ Override
139
- public void serialize (Calendar value , JsonGenerator jgen , SerializerProvider provider ) throws IOException , JsonGenerationException {
140
- provider .defaultSerializeDateKey (value .getTimeInMillis (), jgen );
171
+ public void serialize (Calendar value , JsonGenerator g , SerializerProvider provider ) throws IOException {
172
+ provider .defaultSerializeDateKey (value .getTimeInMillis (), g );
141
173
}
142
174
}
143
175
}
0 commit comments