• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.deser.std;
2 
3 import java.lang.reflect.Constructor;
4 import java.lang.reflect.Method;
5 
6 import com.fasterxml.jackson.databind.*;
7 import com.fasterxml.jackson.databind.deser.KeyDeserializers;
8 import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
9 import com.fasterxml.jackson.databind.util.ClassUtil;
10 import com.fasterxml.jackson.databind.util.EnumResolver;
11 
12 /**
13  * Helper class used to contain simple/well-known key deserializers.
14  * Following kinds of Objects can be handled currently:
15  *<ul>
16  * <li>Primitive wrappers (Boolean, Byte, Char, Short, Integer, Float, Long, Double)</li>
17  * <li>Enums (usually not needed, since EnumMap doesn't call us)</li>
18  * <li>{@link java.util.Date}</li>
19  * <li>{@link java.util.Calendar}</li>
20  * <li>{@link java.util.UUID}</li>
21  * <li>{@link java.util.Locale}</li>
22  * <li>Anything with constructor that takes a single String arg
23  *   (if not explicitly @JsonIgnore'd)</li>
24  * <li>Anything with {@code static T valueOf(String)} factory method
25  *   (if not explicitly @JsonIgnore'd)</li>
26  *</ul>
27  */
28 public class StdKeyDeserializers
29     implements KeyDeserializers, java.io.Serializable
30 {
31     private static final long serialVersionUID = 1L;
32 
constructEnumKeyDeserializer(EnumResolver enumResolver)33     public static KeyDeserializer constructEnumKeyDeserializer(EnumResolver enumResolver) {
34         return new StdKeyDeserializer.EnumKD(enumResolver, null);
35     }
36 
constructEnumKeyDeserializer(EnumResolver enumResolver, AnnotatedMethod factory)37     public static KeyDeserializer constructEnumKeyDeserializer(EnumResolver enumResolver,
38             AnnotatedMethod factory) {
39         return new StdKeyDeserializer.EnumKD(enumResolver, factory);
40     }
41 
constructDelegatingKeyDeserializer(DeserializationConfig config, JavaType type, JsonDeserializer<?> deser)42     public static KeyDeserializer constructDelegatingKeyDeserializer(DeserializationConfig config,
43             JavaType type, JsonDeserializer<?> deser)
44     {
45         return new StdKeyDeserializer.DelegatingKD(type.getRawClass(), deser);
46     }
47 
findStringBasedKeyDeserializer(DeserializationConfig config, JavaType type)48     public static KeyDeserializer findStringBasedKeyDeserializer(DeserializationConfig config,
49             JavaType type)
50     {
51         // We don't need full deserialization information, just need to know creators.
52         BeanDescription beanDesc = config.introspect(type);
53         // Ok, so: can we find T(String) constructor?
54         Constructor<?> ctor = beanDesc.findSingleArgConstructor(String.class);
55         if (ctor != null) {
56             if (config.canOverrideAccessModifiers()) {
57                 ClassUtil.checkAndFixAccess(ctor, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
58             }
59             return new StdKeyDeserializer.StringCtorKeyDeserializer(ctor);
60         }
61         // or if not, "static T valueOf(String)" (or equivalent marked
62         // with @JsonCreator annotation?)
63         Method m = beanDesc.findFactoryMethod(String.class);
64         if (m != null){
65             if (config.canOverrideAccessModifiers()) {
66                 ClassUtil.checkAndFixAccess(m, config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
67             }
68             return new StdKeyDeserializer.StringFactoryKeyDeserializer(m);
69         }
70         // nope, no such luck...
71         return null;
72     }
73 
74     /*
75     /**********************************************************
76     /* KeyDeserializers implementation
77     /**********************************************************
78      */
79 
80     @Override
findKeyDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc)81     public KeyDeserializer findKeyDeserializer(JavaType type,
82             DeserializationConfig config, BeanDescription beanDesc) throws JsonMappingException
83     {
84         Class<?> raw = type.getRawClass();
85         // 23-Apr-2013, tatu: Map primitive types, just in case one was given
86         if (raw.isPrimitive()) {
87             raw = ClassUtil.wrapperType(raw);
88         }
89         return StdKeyDeserializer.forType(raw);
90     }
91 }
92