• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.cfg;
2 
3 import java.util.*;
4 
5 import com.fasterxml.jackson.annotation.JsonFormat;
6 import com.fasterxml.jackson.annotation.JsonInclude;
7 import com.fasterxml.jackson.annotation.JsonSetter;
8 import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
9 
10 /**
11  * Container for individual {@link ConfigOverride} values.
12  *
13  * @since 2.8
14  */
15 public class ConfigOverrides
16     implements java.io.Serializable
17 {
18     private static final long serialVersionUID = 1L;
19 
20     /**
21      * Per-type override definitions
22      */
23     protected Map<Class<?>, MutableConfigOverride> _overrides;
24 
25     // // // Global defaulting
26 
27     /**
28      * @since 2.9
29      */
30     protected JsonInclude.Value _defaultInclusion;
31 
32     /**
33      * @since 2.9
34      */
35     protected JsonSetter.Value _defaultSetterInfo;
36 
37     /**
38      * @since 2.9
39      */
40     protected VisibilityChecker<?> _visibilityChecker;
41 
42     /**
43      * @since 2.9
44      */
45     protected Boolean _defaultMergeable;
46 
47     /**
48      * Global default setting (if any) for leniency: if disabled ({link Boolean#TRUE}),
49      * "strict" (not lenient): default setting if absence of value is considered "lenient"
50      * in Jackson 2.x. Default setting may be overridden by per-type and per-property
51      * settings.
52      *
53      * @since 2.10
54      */
55     protected Boolean _defaultLeniency;
56 
57     /*
58     /**********************************************************************
59     /* Life cycle
60     /**********************************************************************
61      */
62 
ConfigOverrides()63     public ConfigOverrides() {
64         this(null,
65                 // !!! TODO: change to (ALWAYS, ALWAYS)?
66                 JsonInclude.Value.empty(),
67                 JsonSetter.Value.empty(),
68                 VisibilityChecker.Std.defaultInstance(),
69                 null, null
70         );
71     }
72 
73     /**
74      * @since 2.10
75      */
ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides, JsonInclude.Value defIncl, JsonSetter.Value defSetter, VisibilityChecker<?> defVisibility, Boolean defMergeable, Boolean defLeniency)76     protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
77             JsonInclude.Value defIncl, JsonSetter.Value defSetter,
78             VisibilityChecker<?> defVisibility, Boolean defMergeable, Boolean defLeniency)
79     {
80         _overrides = overrides;
81         _defaultInclusion = defIncl;
82         _defaultSetterInfo = defSetter;
83         _visibilityChecker = defVisibility;
84         _defaultMergeable = defMergeable;
85         _defaultLeniency = defLeniency;
86     }
87 
88     /**
89      * @deprecated Since 2.10
90      */
91     @Deprecated // since 2.10
ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides, JsonInclude.Value defIncl, JsonSetter.Value defSetter, VisibilityChecker<?> defVisibility, Boolean defMergeable)92     protected ConfigOverrides(Map<Class<?>, MutableConfigOverride> overrides,
93             JsonInclude.Value defIncl, JsonSetter.Value defSetter,
94             VisibilityChecker<?> defVisibility, Boolean defMergeable) {
95         this(overrides, defIncl, defSetter, defVisibility, defMergeable, null);
96     }
97 
copy()98     public ConfigOverrides copy()
99     {
100         Map<Class<?>, MutableConfigOverride> newOverrides;
101         if (_overrides == null) {
102             newOverrides = null;
103         } else {
104             newOverrides = _newMap();
105             for (Map.Entry<Class<?>, MutableConfigOverride> entry : _overrides.entrySet()) {
106                 newOverrides.put(entry.getKey(), entry.getValue().copy());
107             }
108         }
109         return new ConfigOverrides(newOverrides,
110                 _defaultInclusion, _defaultSetterInfo, _visibilityChecker,
111                 _defaultMergeable, _defaultLeniency);
112     }
113 
114     /*
115     /**********************************************************************
116     /* Per-type override access
117     /**********************************************************************
118      */
119 
findOverride(Class<?> type)120     public ConfigOverride findOverride(Class<?> type) {
121         if (_overrides == null) {
122             return null;
123         }
124         return _overrides.get(type);
125     }
126 
findOrCreateOverride(Class<?> type)127     public MutableConfigOverride findOrCreateOverride(Class<?> type) {
128         if (_overrides == null) {
129             _overrides = _newMap();
130         }
131         MutableConfigOverride override = _overrides.get(type);
132         if (override == null) {
133             override = new MutableConfigOverride();
134             _overrides.put(type, override);
135         }
136         return override;
137     }
138 
139     /**
140      * Specific accessor for finding {code JsonFormat.Value} for given type,
141      * considering global default for leniency as well as per-type format
142      * override (if any).
143      *
144      * @return Default format settings for type; never null.
145      *
146      * @since 2.10
147      */
findFormatDefaults(Class<?> type)148     public JsonFormat.Value findFormatDefaults(Class<?> type) {
149         if (_overrides != null) {
150             ConfigOverride override = _overrides.get(type);
151             if (override != null) {
152                 JsonFormat.Value format = override.getFormat();
153                 if (format != null) {
154                     if (!format.hasLenient()) {
155                         return format.withLenient(_defaultLeniency);
156                     }
157                     return format;
158                 }
159             }
160         }
161         if (_defaultLeniency == null) {
162             return JsonFormat.Value.empty();
163         }
164         return JsonFormat.Value.forLeniency(_defaultLeniency);
165     }
166 
167     /*
168     /**********************************************************************
169     /* Global defaults access
170     /**********************************************************************
171      */
172 
getDefaultInclusion()173     public JsonInclude.Value getDefaultInclusion() {
174         return _defaultInclusion;
175     }
176 
getDefaultSetterInfo()177     public JsonSetter.Value getDefaultSetterInfo() {
178         return _defaultSetterInfo;
179     }
180 
getDefaultMergeable()181     public Boolean getDefaultMergeable() {
182         return _defaultMergeable;
183     }
184 
185     /**
186      * @since 2.10
187      */
getDefaultLeniency()188     public Boolean getDefaultLeniency() {
189         return _defaultLeniency;
190     }
191 
192     /**
193      * @since 2.9
194      */
getDefaultVisibility()195     public VisibilityChecker<?> getDefaultVisibility() {
196         return _visibilityChecker;
197     }
198 
199     /**
200      * @since 2.9
201      */
setDefaultInclusion(JsonInclude.Value v)202     public void setDefaultInclusion(JsonInclude.Value v) {
203         _defaultInclusion = v;
204     }
205 
206     /**
207      * @since 2.9
208      */
setDefaultSetterInfo(JsonSetter.Value v)209     public void setDefaultSetterInfo(JsonSetter.Value v) {
210         _defaultSetterInfo = v;
211     }
212 
213     /**
214      * @since 2.9
215      */
setDefaultMergeable(Boolean v)216     public void setDefaultMergeable(Boolean v) {
217         _defaultMergeable = v;
218     }
219 
220     /**
221      * @since 2.10
222      */
setDefaultLeniency(Boolean v)223     public void setDefaultLeniency(Boolean v) {
224         _defaultLeniency = v;
225     }
226 
227     /**
228      * @since 2.9
229      */
setDefaultVisibility(VisibilityChecker<?> v)230     public void setDefaultVisibility(VisibilityChecker<?> v) {
231         _visibilityChecker = v;
232     }
233 
234     /*
235     /**********************************************************************
236     /* Helper methods
237     /**********************************************************************
238      */
239 
_newMap()240     protected Map<Class<?>, MutableConfigOverride> _newMap() {
241         return new HashMap<Class<?>, MutableConfigOverride>();
242     }
243 }
244