5
5
import java .util .Date ;
6
6
7
7
import com .fasterxml .jackson .core .*;
8
-
9
- import com .fasterxml .jackson .databind .JavaType ;
10
- import com .fasterxml .jackson .databind .JsonSerializer ;
11
- import com .fasterxml .jackson .databind .SerializerProvider ;
8
+ import com .fasterxml .jackson .databind .*;
12
9
13
10
public class StdKeySerializers
14
11
{
@@ -20,33 +17,47 @@ public class StdKeySerializers
20
17
21
18
private StdKeySerializers () { }
22
19
20
+ /**
21
+ * @param config Serialization configuration in use, may be needed in choosing
22
+ * serializer to use
23
+ * @param rawKeyType Type of key values to serialize
24
+ * @param useDefault If no match is found, should we return fallback deserializer
25
+ * (true), or null (false)?
26
+ */
23
27
@ SuppressWarnings ("unchecked" )
24
- public static JsonSerializer <Object > getStdKeySerializer (JavaType keyType )
28
+ public static JsonSerializer <Object > getStdKeySerializer (SerializationConfig config ,
29
+ Class <?> rawKeyType , boolean useDefault )
25
30
{
26
- if (keyType == null ) {
27
- return DEFAULT_KEY_SERIALIZER ;
28
- }
29
- Class <?> cls = keyType .getRawClass ();
30
- if (cls == String .class ) {
31
- return DEFAULT_STRING_SERIALIZER ;
32
- }
33
- if (cls == Object .class || cls .isPrimitive () || Number .class .isAssignableFrom (cls )) {
34
- return DEFAULT_KEY_SERIALIZER ;
35
- }
36
- if (Date .class .isAssignableFrom (cls )) {
37
- return (JsonSerializer <Object >) DateKeySerializer .instance ;
38
- }
39
- if (Calendar .class .isAssignableFrom (cls )) {
40
- return (JsonSerializer <Object >) CalendarKeySerializer .instance ;
31
+ if (rawKeyType != null ) {
32
+ if (rawKeyType == String .class ) {
33
+ return DEFAULT_STRING_SERIALIZER ;
34
+ }
35
+ if (rawKeyType == Object .class || rawKeyType .isPrimitive ()
36
+ || Number .class .isAssignableFrom (rawKeyType )) {
37
+ return DEFAULT_KEY_SERIALIZER ;
38
+ }
39
+ if (Date .class .isAssignableFrom (rawKeyType )) {
40
+ return (JsonSerializer <Object >) DateKeySerializer .instance ;
41
+ }
42
+ if (Calendar .class .isAssignableFrom (rawKeyType )) {
43
+ return (JsonSerializer <Object >) CalendarKeySerializer .instance ;
44
+ }
41
45
}
42
- /* 14-Mar-2014, tatu: Should support @JsonValue, as per #47; but that
43
- * requires extensive introspection, and passing in more information
44
- * to this method.
45
- */
46
- // If no match, just use default one:
47
- return DEFAULT_KEY_SERIALIZER ;
46
+ return useDefault ? DEFAULT_KEY_SERIALIZER : null ;
47
+ }
48
+
49
+ /**
50
+ * @deprecated Since 2.5
51
+ */
52
+ @ Deprecated
53
+ public static JsonSerializer <Object > getStdKeySerializer (JavaType keyType ) {
54
+ return getStdKeySerializer (null , keyType .getRawClass (), true );
48
55
}
49
56
57
+ public static JsonSerializer <Object > getDefault () {
58
+ return DEFAULT_KEY_SERIALIZER ;
59
+ }
60
+
50
61
/*
51
62
/**********************************************************
52
63
/* Standard implementations
0 commit comments