1 package com.fasterxml.jackson.databind.module;
2 
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6 
7 
8 import com.fasterxml.jackson.databind.*;
9 import com.fasterxml.jackson.databind.deser.KeyDeserializers;
10 import com.fasterxml.jackson.databind.type.ClassKey;
11 
12 /**
13  * Simple implementation {@link KeyDeserializers} which allows registration of
14  * deserializers based on raw (type erased class).
15  * It can work well for basic bean and scalar type deserializers, but is not
16  * a good fit for handling generic types (like {@link Map}s and {@link Collection}s
17  * or array types).
18  *<p>
19  * Unlike {@link SimpleSerializers}, this class does not currently support generic mappings;
20  * all mappings must be to exact declared deserialization type.
21  */
22 public class SimpleKeyDeserializers
23     implements KeyDeserializers, java.io.Serializable // since 2.1
24 {
25     private static final long serialVersionUID = 1L;
26 
27     protected HashMap<ClassKey,KeyDeserializer> _classMappings = null;
28 
29     /*
30     /**********************************************************
31     /* Life-cycle, construction and configuring
32     /**********************************************************
33      */
34 
SimpleKeyDeserializers()35     public SimpleKeyDeserializers() { }
36 
addDeserializer(Class<?> forClass, KeyDeserializer deser)37     public SimpleKeyDeserializers addDeserializer(Class<?> forClass, KeyDeserializer deser)
38     {
39         if (_classMappings == null) {
40             _classMappings = new HashMap<ClassKey,KeyDeserializer>();
41         }
42         _classMappings.put(new ClassKey(forClass), deser);
43         return this;
44     }
45 
46     /*
47     /**********************************************************
48     /* Serializers implementation
49     /**********************************************************
50      */
51 
52     @Override
findKeyDeserializer(JavaType type, DeserializationConfig config, BeanDescription beanDesc)53     public KeyDeserializer findKeyDeserializer(JavaType type,
54             DeserializationConfig config, BeanDescription beanDesc)
55     {
56         if (_classMappings == null) {
57             return null;
58         }
59         return _classMappings.get(new ClassKey(type.getRawClass()));
60     }
61 }
62