All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.codehaus.jackson.map.DeserializationConfig Maven / Gradle / Ivy

Go to download

Data Mapper package is a high-performance data binding package built on Jackson JSON processor

There is a newer version: 1.9.13
Show newest version
package org.codehaus.jackson.map;

import java.text.DateFormat;

import org.codehaus.jackson.Base64Variant;
import org.codehaus.jackson.Base64Variants;
import org.codehaus.jackson.annotate.*;
import org.codehaus.jackson.map.introspect.AnnotatedClass;
import org.codehaus.jackson.map.util.LinkedNode;
import org.codehaus.jackson.map.util.StdDateFormat;
import org.codehaus.jackson.type.JavaType;

/**
 * Object that contains baseline configuration for deserialization
 * process. An instance is owned by {@link ObjectMapper}, which makes
 * a copy that is passed during serialization process to
 * {@link DeserializerProvider} and {@link DeserializerFactory}.
 *

* Note: although configuration settings can be changed at any time * (for factories and instances), they are not guaranteed to have * effect if called after constructing relevant mapper or deserializer * instance. This because some objects may be configured, constructed and * cached first time they are needed. */ public class DeserializationConfig { /** * Enumeration that defines togglable features that guide * the serialization feature. */ public enum Feature { // // // Class introspection configuration /** * Feature that determines whether "setter" methods are * automatically detected based on standard Bean naming convention * or not. If yes, then all public one-argument methods that * start with prefix "set" * are considered setters. If disabled, only methods explicitly * annotated are considered setters. *

* Note that this feature has lower precedence than per-class * annotations, and is only used if there isn't more granular * configuration available. *

* Feature is enabled by default. */ AUTO_DETECT_SETTERS(true), /** * Feature that determines whether "creator" methods are * automatically detected by consider public constructors, * and static single argument methods with name "valueOf". * If disabled, only methods explicitly annotated are considered * creator methods (except for the no-arg default constructor which * is always considered a factory method). *

* Note that this feature has lower precedence than per-class * annotations, and is only used if there isn't more granular * configuration available. *

* Feature is enabled by default. */ AUTO_DETECT_CREATORS(true), /** * Feature that determines whether non-static fields are recognized as * properties. * If yes, then all public member fields * are considered as properties. If disabled, only fields explicitly * annotated are considered property fields. *

* Note that this feature has lower precedence than per-class * annotations, and is only used if there isn't more granular * configuration available. *

* Feature is enabled by default. * * @since 1.1 */ AUTO_DETECT_FIELDS(true), /** * Feature that determines whether otherwise regular "getter" * methods (but only ones that handle Collections and Maps, * not getters of other type) * can be used for purpose of getting a reference to a Collection * and Map to modify the property, without requiring a setter * method. * This is similar to how JAXB framework sets Collections and * Maps: no setter is involved, just setter. *

* Note that such getters-as-setters methods have lower * precedence than setters, so they are only used if no * setter is found for the Map/Collection property. *

* Feature is enabled by default. */ USE_GETTERS_AS_SETTERS(true), /** * Feature that determines whether method and field access * modifier settings can be overridden when accessing * properties. If enabled, method * {@link java.lang.reflect.AccessibleObject#setAccessible} * may be called to enable access to otherwise unaccessible * objects. */ CAN_OVERRIDE_ACCESS_MODIFIERS(true), // // // Type conversion configuration /** * Feature that determines whether Json floating point numbers * are to be deserialized into {@link java.math.BigDecimal}s * if only generic type description (either {@link Object} or * {@link Number}, or within untyped {@link java.util.Map} * or {@link java.util.Collection} context) is available. * If enabled such values will be deserialized as {@link java.math.BigDecimal}s; * if disabled, will be deserialized as {@link Double}s. *

* Feature is disabled by default, meaning that "untyped" floating * point numbers will by default be deserialized as {@link Double}s * (choice is for performance reason -- BigDecimals are slower than * Doubles) */ USE_BIG_DECIMAL_FOR_FLOATS(false), /** * Feature that determines whether Json integral (non-floating-point) * numbers are to be deserialized into {@link java.math.BigInteger}s * if only generic type description (either {@link Object} or * {@link Number}, or within untyped {@link java.util.Map} * or {@link java.util.Collection} context) is available. * If enabled such values will be deserialized as * {@link java.math.BigInteger}s; * if disabled, will be deserialized as "smallest" available type, * which is either {@link Integer}, {@link Long} or * {@link java.math.BigInteger}, depending on number of digits. *

* Feature is disabled by default, meaning that "untyped" floating * point numbers will by default be deserialized using whatever * is the most compact integral type, to optimize efficiency. */ USE_BIG_INTEGER_FOR_INTS(false) ; final boolean _defaultState; /** * Method that calculates bit set (flags) of all features that * are enabled by default. */ public static int collectDefaults() { int flags = 0; for (Feature f : values()) { if (f.enabledByDefault()) { flags |= f.getMask(); } } return flags; } private Feature(boolean defaultState) { _defaultState = defaultState; } public boolean enabledByDefault() { return _defaultState; } public int getMask() { return (1 << ordinal()); } } /** * Bitfield (set of flags) of all Features that are enabled * by default. */ protected final static int DEFAULT_FEATURE_FLAGS = Feature.collectDefaults(); protected final static DateFormat DEFAULT_DATE_FORMAT = StdDateFormat.instance; /* /////////////////////////////////////////////////////////// // Configuration settings /////////////////////////////////////////////////////////// */ /** * Introspector used to figure out Bean properties needed for bean serialization * and deserialization. Overridable so that it is possible to change low-level * details of introspection, like adding new annotation types. */ protected ClassIntrospector _classIntrospector; /** * Introspector used for accessing annotation value based configuration. */ protected AnnotationIntrospector _annotationIntrospector; /** * Bitset that contains all enabled features */ protected int _featureFlags = DEFAULT_FEATURE_FLAGS; /** * Linked list that contains all registered problem handlers. * Implementation as front-added linked list allows for sharing * of the list (tail) without copying the list. */ protected LinkedNode _problemHandlers; /** * Custom date format to use for de-serialization. If specified, will be * used instead of {@link org.codehaus.jackson.map.util.StdDateFormat}. *

* Note that the configured format object will be cloned once per * deserialization process (first time it is needed) */ protected DateFormat _dateFormat = DEFAULT_DATE_FORMAT; /* /////////////////////////////////////////////////////////// // Life-cycle /////////////////////////////////////////////////////////// */ public DeserializationConfig(ClassIntrospector intr, AnnotationIntrospector annIntr) { _classIntrospector = intr; _annotationIntrospector = annIntr; } protected DeserializationConfig(DeserializationConfig src) { _classIntrospector = src._classIntrospector; _annotationIntrospector = src._annotationIntrospector; _featureFlags = src._featureFlags; _problemHandlers = src._problemHandlers; _dateFormat = src._dateFormat; } /** * Method that is called to create a non-shared copy of the configuration * to be used for a deserialization operation. * Note that if sub-classing * and sub-class has additional instance methods, * this method must be overridden to produce proper sub-class * instance. */ public DeserializationConfig createUnshared() { return new DeserializationConfig(this); } /** * Method that checks class annotations that the argument Object has, * and modifies settings of this configuration object accordingly, * similar to how those annotations would affect actual value classes * annotated with them, but with global scope. Note that not all * annotations have global significance, and thus only subset of * Jackson annotations will have any effect. *

* Ones that are known to have effect are: *

    *
  • {@link JsonWriteNullProperties}
  • *
  • {@link JsonAutoDetect}
  • *
* * @param cls Class of which class annotations to use * for changing configuration settings */ public void fromAnnotations(Class cls) { /* no class annotation for: * * - CAN_OVERRIDE_ACCESS_MODIFIERS * - USE_BIG_DECIMAL_FOR_FLOATS * - USE_BIG_INTEGER_FOR_INTS * - USE_GETTERS_AS_SETTERS */ AnnotatedClass ac = AnnotatedClass.construct(cls, _annotationIntrospector); // Auto-detect setters, creators? Boolean ad = _annotationIntrospector.findSetterAutoDetection(ac); if (ad != null) { set(Feature.AUTO_DETECT_SETTERS, ad.booleanValue()); } ad = _annotationIntrospector.findCreatorAutoDetection(ac); if (ad != null) { set(Feature.AUTO_DETECT_CREATORS, ad.booleanValue()); } } /** * Method that can be used to add a handler that can (try to) * resolve non-fatal deserialization problems. */ public void addHandler(DeserializationProblemHandler h) { /* Sanity check: let's prevent adding same handler multiple * times */ if (!LinkedNode.contains(_problemHandlers, h)) { _problemHandlers = new LinkedNode(h, _problemHandlers); } } /** * Method for removing all configuring problem handlers; usually done to replace * existing handler(s) with different one(s) * * @since 1.1 */ public void clearHandlers() { _problemHandlers = null; } /* /////////////////////////////////////////////////////////// // Accessors /////////////////////////////////////////////////////////// */ /** * Method for checking whether given feature is enabled or not */ public final boolean isEnabled(Feature f) { return (_featureFlags & f.getMask()) != 0; } /** * Method called during deserialization if Base64 encoded content * needs to be decoded. Default version just returns default Jackson * uses, which is modified-mime which does not add linefeeds (because * those would have to be escaped in Json strings). */ public Base64Variant getBase64Variant() { return Base64Variants.getDefaultVariant(); } /** * Method for getting head of the problem handler chain. May be null, * if no handlers have been added. */ public LinkedNode getProblemHandlers() { return _problemHandlers; } public DateFormat getDateFormat() { return _dateFormat; } /** * Method for getting {@link AnnotationIntrospector} configured * to introspect annotation values used for configuration. */ public AnnotationIntrospector getAnnotationIntrospector() { return _annotationIntrospector; } public void setAnnotationIntrospector(AnnotationIntrospector introspector) { this._annotationIntrospector = introspector; } /** * Method that will introspect full bean properties for the purpose * of building a bean deserializer * * @param type Type of class to be introspected */ @SuppressWarnings("unchecked") public T introspect(JavaType type) { return (T) _classIntrospector.forDeserialization(this, type); } /** * Method that will introspect subset of bean properties needed to * construct bean instance. */ @SuppressWarnings("unchecked") public T introspectForCreation(Class cls) { return (T) _classIntrospector.forCreation(this, cls); } /** * Accessor for getting bean description that only contains class * annotations: useful if no getter/setter/creator information is needed. */ @SuppressWarnings("unchecked") public T introspectClassAnnotations(Class cls) { return (T) _classIntrospector.forClassAnnotations(this, cls); } /* //////////////////////////////////////////////////// // Configuration: on/off features //////////////////////////////////////////////////// */ /** * Method for enabling specified feature. */ public void enable(Feature f) { _featureFlags |= f.getMask(); } /** * Method for disabling specified feature. */ public void disable(Feature f) { _featureFlags &= ~f.getMask(); } /** * Method for enabling or disabling specified feature. */ public void set(Feature f, boolean state) { if (state) { enable(f); } else { disable(f); } } //protected int getFeatures() { return _features; } /* //////////////////////////////////////////////////// // Configuration: other //////////////////////////////////////////////////// */ /** * Method that will set the textual deserialization to use for * deserializing Dates (and Calendars). If null is passed, will * use {@link StdDateFormat}. */ public void setDateFormat(DateFormat df) { _dateFormat = (df == null) ? StdDateFormat.instance : df; } public void setIntrospector(ClassIntrospector i) { _classIntrospector = i; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy