• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.cfg;
2 
3 import com.fasterxml.jackson.databind.AbstractTypeResolver;
4 import com.fasterxml.jackson.databind.deser.*;
5 import com.fasterxml.jackson.databind.deser.std.StdKeyDeserializers;
6 import com.fasterxml.jackson.databind.util.ArrayBuilders;
7 import com.fasterxml.jackson.databind.util.ArrayIterator;
8 
9 /**
10  * Configuration settings container class for {@link DeserializerFactory}.
11  */
12 public class DeserializerFactoryConfig
13     implements java.io.Serializable // since 2.1
14 {
15     private static final long serialVersionUID = 1L; // since 2.5
16 
17     protected final static Deserializers[] NO_DESERIALIZERS = new Deserializers[0];
18     protected final static BeanDeserializerModifier[] NO_MODIFIERS = new BeanDeserializerModifier[0];
19     protected final static AbstractTypeResolver[] NO_ABSTRACT_TYPE_RESOLVERS = new AbstractTypeResolver[0];
20     protected final static ValueInstantiators[] NO_VALUE_INSTANTIATORS = new ValueInstantiators[0];
21 
22     /**
23      * By default we plug default key deserializers using as "just another" set of
24      * of key deserializers.
25      *
26      * @since 2.2
27      */
28     protected final static KeyDeserializers[] DEFAULT_KEY_DESERIALIZERS = new KeyDeserializers[] {
29         new StdKeyDeserializers()
30     };
31 
32     /**
33      * List of providers for additional deserializers, checked before considering default
34      * basic or bean deserializers.
35      */
36     protected final Deserializers[] _additionalDeserializers;
37 
38     /**
39      * List of providers for additional key deserializers, checked before considering
40      * standard key deserializers.
41      */
42     protected final KeyDeserializers[] _additionalKeyDeserializers;
43 
44     /**
45      * List of modifiers that can change the way {@link BeanDeserializer} instances
46      * are configured and constructed.
47      */
48     protected final BeanDeserializerModifier[] _modifiers;
49 
50     /**
51      * List of objects that may be able to resolve abstract types to
52      * concrete types. Used by functionality like "mr Bean" to materialize
53      * types as needed.
54      */
55     protected final AbstractTypeResolver[] _abstractTypeResolvers;
56 
57     /**
58      * List of objects that know how to create instances of POJO types;
59      * possibly using custom construction (non-annoted constructors; factory
60      * methods external to value type etc).
61      * Used to support objects that are created using non-standard methods;
62      * or to support post-constructor functionality.
63      */
64     protected final ValueInstantiators[] _valueInstantiators;
65 
66     /**
67      * Constructor for creating basic configuration with no additional
68      * handlers.
69      */
DeserializerFactoryConfig()70     public DeserializerFactoryConfig() {
71         this(null, null, null, null, null);
72     }
73 
74     /**
75      * Copy-constructor that will create an instance that contains defined
76      * set of additional deserializer providers.
77      */
DeserializerFactoryConfig(Deserializers[] allAdditionalDeserializers, KeyDeserializers[] allAdditionalKeyDeserializers, BeanDeserializerModifier[] modifiers, AbstractTypeResolver[] atr, ValueInstantiators[] vi)78     protected DeserializerFactoryConfig(Deserializers[] allAdditionalDeserializers,
79             KeyDeserializers[] allAdditionalKeyDeserializers,
80             BeanDeserializerModifier[] modifiers,
81             AbstractTypeResolver[] atr,
82             ValueInstantiators[] vi)
83     {
84         _additionalDeserializers = (allAdditionalDeserializers == null) ?
85                 NO_DESERIALIZERS : allAdditionalDeserializers;
86         _additionalKeyDeserializers = (allAdditionalKeyDeserializers == null) ?
87                 DEFAULT_KEY_DESERIALIZERS : allAdditionalKeyDeserializers;
88         _modifiers = (modifiers == null) ? NO_MODIFIERS : modifiers;
89         _abstractTypeResolvers = (atr == null) ? NO_ABSTRACT_TYPE_RESOLVERS : atr;
90         _valueInstantiators = (vi == null) ? NO_VALUE_INSTANTIATORS : vi;
91     }
92 
93     /**
94      * Fluent/factory method used to construct a configuration object that
95      * has same deserializer providers as this instance, plus one specified
96      * as argument. Additional provider will be added before existing ones,
97      * meaning it has priority over existing definitions.
98      */
withAdditionalDeserializers(Deserializers additional)99     public DeserializerFactoryConfig withAdditionalDeserializers(Deserializers additional)
100     {
101         if (additional == null) {
102             throw new IllegalArgumentException("Cannot pass null Deserializers");
103         }
104         Deserializers[] all = ArrayBuilders.insertInListNoDup(_additionalDeserializers, additional);
105         return new DeserializerFactoryConfig(all, _additionalKeyDeserializers, _modifiers,
106                 _abstractTypeResolvers, _valueInstantiators);
107     }
108 
109     /**
110      * Fluent/factory method used to construct a configuration object that
111      * has same key deserializer providers as this instance, plus one specified
112      * as argument. Additional provider will be added before existing ones,
113      * meaning it has priority over existing definitions.
114      */
withAdditionalKeyDeserializers(KeyDeserializers additional)115     public DeserializerFactoryConfig withAdditionalKeyDeserializers(KeyDeserializers additional)
116     {
117         if (additional == null) {
118             throw new IllegalArgumentException("Cannot pass null KeyDeserializers");
119         }
120         KeyDeserializers[] all = ArrayBuilders.insertInListNoDup(_additionalKeyDeserializers, additional);
121         return new DeserializerFactoryConfig(_additionalDeserializers, all, _modifiers,
122                 _abstractTypeResolvers, _valueInstantiators);
123     }
124 
125     /**
126      * Fluent/factory method used to construct a configuration object that
127      * has same configuration as this instance plus one additional
128      * deserialiazer modifier. Added modifier has the highest priority (that is, it
129      * gets called before any already registered modifier).
130      */
withDeserializerModifier(BeanDeserializerModifier modifier)131     public DeserializerFactoryConfig withDeserializerModifier(BeanDeserializerModifier modifier)
132     {
133         if (modifier == null) {
134             throw new IllegalArgumentException("Cannot pass null modifier");
135         }
136         BeanDeserializerModifier[] all = ArrayBuilders.insertInListNoDup(_modifiers, modifier);
137         return new DeserializerFactoryConfig(_additionalDeserializers, _additionalKeyDeserializers, all,
138                 _abstractTypeResolvers, _valueInstantiators);
139     }
140 
141     /**
142      * Fluent/factory method used to construct a configuration object that
143      * has same configuration as this instance plus one additional
144      * abstract type resolver.
145      * Added resolver has the highest priority (that is, it
146      * gets called before any already registered resolver).
147      */
withAbstractTypeResolver(AbstractTypeResolver resolver)148     public DeserializerFactoryConfig withAbstractTypeResolver(AbstractTypeResolver resolver)
149     {
150         if (resolver == null) {
151             throw new IllegalArgumentException("Cannot pass null resolver");
152         }
153         AbstractTypeResolver[] all = ArrayBuilders.insertInListNoDup(_abstractTypeResolvers, resolver);
154         return new DeserializerFactoryConfig(_additionalDeserializers, _additionalKeyDeserializers, _modifiers,
155                 all, _valueInstantiators);
156     }
157 
158     /**
159      * Fluent/factory method used to construct a configuration object that
160      * has same configuration as this instance plus specified additional
161      * value instantiator provider object.
162      * Added instantiator provider has the highest priority (that is, it
163      * gets called before any already registered resolver).
164      *
165      * @param instantiators Object that can provide {@link com.fasterxml.jackson.databind.deser.ValueInstantiator}s for
166      *    constructing POJO values during deserialization
167      */
withValueInstantiators(ValueInstantiators instantiators)168     public DeserializerFactoryConfig withValueInstantiators(ValueInstantiators instantiators)
169     {
170         if (instantiators == null) {
171             throw new IllegalArgumentException("Cannot pass null resolver");
172         }
173         ValueInstantiators[] all = ArrayBuilders.insertInListNoDup(_valueInstantiators, instantiators);
174         return new DeserializerFactoryConfig(_additionalDeserializers, _additionalKeyDeserializers, _modifiers,
175                 _abstractTypeResolvers, all);
176     }
177 
hasDeserializers()178     public boolean hasDeserializers() { return _additionalDeserializers.length > 0; }
179 
hasKeyDeserializers()180     public boolean hasKeyDeserializers() { return _additionalKeyDeserializers.length > 0; }
181 
hasDeserializerModifiers()182     public boolean hasDeserializerModifiers() { return _modifiers.length > 0; }
183 
hasAbstractTypeResolvers()184     public boolean hasAbstractTypeResolvers() { return _abstractTypeResolvers.length > 0; }
185 
hasValueInstantiators()186     public boolean hasValueInstantiators() { return _valueInstantiators.length > 0; }
187 
deserializers()188     public Iterable<Deserializers> deserializers() {
189         return new ArrayIterator<Deserializers>(_additionalDeserializers);
190     }
191 
keyDeserializers()192     public Iterable<KeyDeserializers> keyDeserializers() {
193         return new ArrayIterator<KeyDeserializers>(_additionalKeyDeserializers);
194     }
195 
deserializerModifiers()196     public Iterable<BeanDeserializerModifier> deserializerModifiers() {
197         return new ArrayIterator<BeanDeserializerModifier>(_modifiers);
198     }
199 
abstractTypeResolvers()200     public Iterable<AbstractTypeResolver> abstractTypeResolvers() {
201         return new ArrayIterator<AbstractTypeResolver>(_abstractTypeResolvers);
202     }
203 
valueInstantiators()204     public Iterable<ValueInstantiators> valueInstantiators() {
205         return new ArrayIterator<ValueInstantiators>(_valueInstantiators);
206     }
207 }
208