) conv;
}
}
DeserializationConfig.java 0000664 0000000 0000000 00000052226 12373034547 0035601 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.text.DateFormat;
import java.util.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.cfg.BaseSettings;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfigBase;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.introspect.AnnotatedClass;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector;
import com.fasterxml.jackson.databind.introspect.VisibilityChecker;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.jsontype.SubtypeResolver;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.type.ClassKey;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.LinkedNode;
/**
* Object that contains baseline configuration for deserialization
* process. An instance is owned by {@link ObjectMapper}, which
* passes an immutable instance to be used for deserialization process.
*
* Note that instances are considered immutable and as such no copies
* should need to be created for sharing; all copying is done with
* "fluent factory" methods.
* Note also that unlike with Jackson 1, these instances can not be
* assigned to {@link ObjectMapper}; in fact, application code should
* rarely interact directly with these instance (unlike core Jackson code)
*/
public final class DeserializationConfig
extends MapperConfigBase
implements java.io.Serializable // since 2.1
{
// for 2.1.0
private static final long serialVersionUID = -4227480407273773599L;
/**
* Set of features enabled; actual type (kind of features)
* depends on sub-classes.
*/
protected final int _deserFeatures;
/**
* 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 final LinkedNode _problemHandlers;
/**
* Factory used for constructing {@link com.fasterxml.jackson.databind.JsonNode} instances.
*/
protected final JsonNodeFactory _nodeFactory;
/*
/**********************************************************
/* Life-cycle, constructors
/**********************************************************
*/
/**
* Constructor used by ObjectMapper to create default configuration object instance.
*/
public DeserializationConfig(BaseSettings base,
SubtypeResolver str, Map> mixins)
{
super(base, str, mixins);
_deserFeatures = collectFeatureDefaults(DeserializationFeature.class);
_nodeFactory = JsonNodeFactory.instance;
_problemHandlers = null;
}
/**
* Copy constructor used to create a non-shared instance with given mix-in
* annotation definitions and subtype resolver.
*/
private DeserializationConfig(DeserializationConfig src, SubtypeResolver str)
{
super(src, str);
_deserFeatures = src._deserFeatures;
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
}
private DeserializationConfig(DeserializationConfig src,
int mapperFeatures, int deserFeatures)
{
super(src, mapperFeatures);
_deserFeatures = deserFeatures;
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
}
private DeserializationConfig(DeserializationConfig src, BaseSettings base)
{
super(src, base);
_deserFeatures = src._deserFeatures;
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
}
private DeserializationConfig(DeserializationConfig src, JsonNodeFactory f)
{
super(src);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = f;
}
private DeserializationConfig(DeserializationConfig src,
LinkedNode problemHandlers)
{
super(src);
_deserFeatures = src._deserFeatures;
_problemHandlers = problemHandlers;
_nodeFactory = src._nodeFactory;
}
private DeserializationConfig(DeserializationConfig src, String rootName)
{
super(src, rootName);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
}
private DeserializationConfig(DeserializationConfig src, Class> view)
{
super(src, view);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
}
/**
* @since 2.1
*/
protected DeserializationConfig(DeserializationConfig src, Map> mixins)
{
super(src, mixins);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
}
/**
* @since 2.3
*/
protected DeserializationConfig(DeserializationConfig src, ContextAttributes attrs)
{
super(src, attrs);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
}
// for unit tests only:
protected BaseSettings getBaseSettings() { return _base; }
/*
/**********************************************************
/* Life-cycle, factory methods from MapperConfig
/**********************************************************
*/
@Override
public DeserializationConfig with(MapperFeature... features)
{
int newMapperFlags = _mapperFeatures;
for (MapperFeature f : features) {
newMapperFlags |= f.getMask();
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures);
}
@Override
public DeserializationConfig without(MapperFeature... features)
{
int newMapperFlags = _mapperFeatures;
for (MapperFeature f : features) {
newMapperFlags &= ~f.getMask();
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures);
}
@Override
public DeserializationConfig with(MapperFeature feature, boolean state)
{
int newMapperFlags;
if (state) {
newMapperFlags = _mapperFeatures | feature.getMask();
} else {
newMapperFlags = _mapperFeatures & ~feature.getMask();
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures);
}
@Override
public DeserializationConfig with(ClassIntrospector ci) {
return _withBase(_base.withClassIntrospector(ci));
}
@Override
public DeserializationConfig with(AnnotationIntrospector ai) {
return _withBase(_base.withAnnotationIntrospector(ai));
}
@Override
public DeserializationConfig with(VisibilityChecker> vc) {
return _withBase(_base.withVisibilityChecker(vc));
}
@Override
public DeserializationConfig withVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility) {
return _withBase( _base.withVisibility(forMethod, visibility));
}
@Override
public DeserializationConfig with(TypeResolverBuilder> trb) {
return _withBase(_base.withTypeResolverBuilder(trb));
}
@Override
public DeserializationConfig with(SubtypeResolver str) {
return (_subtypeResolver == str) ? this : new DeserializationConfig(this, str);
}
@Override
public DeserializationConfig with(PropertyNamingStrategy pns) {
return _withBase(_base.withPropertyNamingStrategy(pns));
}
@Override
public DeserializationConfig withRootName(String rootName) {
if (rootName == null) {
if (_rootName == null) {
return this;
}
} else if (rootName.equals(_rootName)) {
return this;
}
return new DeserializationConfig(this, rootName);
}
@Override
public DeserializationConfig with(TypeFactory tf) {
return _withBase( _base.withTypeFactory(tf));
}
@Override
public DeserializationConfig with(DateFormat df) {
return _withBase(_base.withDateFormat(df));
}
@Override
public DeserializationConfig with(HandlerInstantiator hi) {
return _withBase(_base.withHandlerInstantiator(hi));
}
@Override
public DeserializationConfig withInsertedAnnotationIntrospector(AnnotationIntrospector ai) {
return _withBase(_base.withInsertedAnnotationIntrospector(ai));
}
@Override
public DeserializationConfig withAppendedAnnotationIntrospector(AnnotationIntrospector ai) {
return _withBase(_base.withAppendedAnnotationIntrospector(ai));
}
@Override
public DeserializationConfig withView(Class> view) {
return (_view == view) ? this : new DeserializationConfig(this, view);
}
@Override
public DeserializationConfig with(Locale l) {
return _withBase(_base.with(l));
}
@Override
public DeserializationConfig with(TimeZone tz) {
return _withBase(_base.with(tz));
}
@Override
public DeserializationConfig with(Base64Variant base64) {
return _withBase(_base.with(base64));
}
@Override
public DeserializationConfig with(ContextAttributes attrs) {
return (attrs == _attributes) ? this : new DeserializationConfig(this, attrs);
}
private final DeserializationConfig _withBase(BaseSettings newBase) {
return (_base == newBase) ? this : new DeserializationConfig(this, newBase);
}
/*
/**********************************************************
/* Life-cycle, deserialization-specific factory methods
/**********************************************************
*/
/**
* Fluent factory method that will construct a new instance with
* specified {@link JsonNodeFactory}
*/
public DeserializationConfig with(JsonNodeFactory f) {
if (_nodeFactory == f) {
return this;
}
return new DeserializationConfig(this, f);
}
/**
* Method that can be used to add a handler that can (try to)
* resolve non-fatal deserialization problems.
*/
public DeserializationConfig withHandler(DeserializationProblemHandler h)
{
// Sanity check: let's prevent adding same handler multiple times
if (LinkedNode.contains(_problemHandlers, h)) {
return this;
}
return new DeserializationConfig(this,
new LinkedNode(h, _problemHandlers));
}
/**
* Method for removing all configured problem handlers; usually done to replace
* existing handler(s) with different one(s)
*/
public DeserializationConfig withNoProblemHandlers() {
if (_problemHandlers == null) {
return this;
}
return new DeserializationConfig(this,
(LinkedNode) null);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features enabled.
*/
public DeserializationConfig with(DeserializationFeature feature)
{
int newDeserFeatures = (_deserFeatures | feature.getMask());
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features enabled.
*/
public DeserializationConfig with(DeserializationFeature first,
DeserializationFeature... features)
{
int newDeserFeatures = _deserFeatures | first.getMask();
for (DeserializationFeature f : features) {
newDeserFeatures |= f.getMask();
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features enabled.
*/
public DeserializationConfig withFeatures(DeserializationFeature... features)
{
int newDeserFeatures = _deserFeatures;
for (DeserializationFeature f : features) {
newDeserFeatures |= f.getMask();
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified feature disabled.
*/
public DeserializationConfig without(DeserializationFeature feature)
{
int newDeserFeatures = _deserFeatures & ~feature.getMask();
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features disabled.
*/
public DeserializationConfig without(DeserializationFeature first,
DeserializationFeature... features)
{
int newDeserFeatures = _deserFeatures & ~first.getMask();
for (DeserializationFeature f : features) {
newDeserFeatures &= ~f.getMask();
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features disabled.
*/
public DeserializationConfig withoutFeatures(DeserializationFeature... features)
{
int newDeserFeatures = _deserFeatures;
for (DeserializationFeature f : features) {
newDeserFeatures &= ~f.getMask();
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures);
}
/*
/**********************************************************
/* MapperConfig implementation
/**********************************************************
*/
/**
* Method for getting {@link AnnotationIntrospector} configured
* to introspect annotation values used for configuration.
*/
@Override
public AnnotationIntrospector getAnnotationIntrospector()
{
/* 29-Jul-2009, tatu: it's now possible to disable use of
* annotations; can be done using "no-op" introspector
*/
if (isEnabled(MapperFeature.USE_ANNOTATIONS)) {
return super.getAnnotationIntrospector();
}
return NopAnnotationIntrospector.instance;
}
@Override
public boolean useRootWrapping()
{
if (_rootName != null) { // empty String disables wrapping; non-empty enables
return (_rootName.length() > 0);
}
return isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE);
}
/**
* Accessor for getting bean description that only contains class
* annotations: useful if no getter/setter/creator information is needed.
*/
@Override
public BeanDescription introspectClassAnnotations(JavaType type) {
return getClassIntrospector().forClassAnnotations(this, type, this);
}
/**
* Accessor for getting bean description that only contains immediate class
* annotations: ones from the class, and its direct mix-in, if any, but
* not from super types.
*/
@Override
public BeanDescription introspectDirectClassAnnotations(JavaType type) {
return getClassIntrospector().forDirectClassAnnotations(this, type, this);
}
@Override
public VisibilityChecker> getDefaultVisibilityChecker()
{
VisibilityChecker> vchecker = super.getDefaultVisibilityChecker();
if (!isEnabled(MapperFeature.AUTO_DETECT_SETTERS)) {
vchecker = vchecker.withSetterVisibility(Visibility.NONE);
}
if (!isEnabled(MapperFeature.AUTO_DETECT_CREATORS)) {
vchecker = vchecker.withCreatorVisibility(Visibility.NONE);
}
if (!isEnabled(MapperFeature.AUTO_DETECT_FIELDS)) {
vchecker = vchecker.withFieldVisibility(Visibility.NONE);
}
return vchecker;
}
public final boolean isEnabled(DeserializationFeature f) {
return (_deserFeatures & f.getMask()) != 0;
}
/**
* "Bulk" access method for checking that all features specified by
* mask are enabled.
*
* @since 2.3
*/
public final boolean hasDeserializationFeatures(int featureMask) {
return (_deserFeatures & featureMask) == featureMask;
}
public final int getDeserializationFeatures() {
return _deserFeatures;
}
/*
/**********************************************************
/* Other configuration
/**********************************************************
*/
/**
* Method for getting head of the problem handler chain. May be null,
* if no handlers have been added.
*/
public LinkedNode getProblemHandlers() {
return _problemHandlers;
}
public final JsonNodeFactory getNodeFactory() {
return _nodeFactory;
}
/*
/**********************************************************
/* Introspection methods
/**********************************************************
*/
/**
* 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) getClassIntrospector().forDeserialization(this, type, this);
}
/**
* Method that will introspect subset of bean properties needed to
* construct bean instance.
*/
@SuppressWarnings("unchecked")
public T introspectForCreation(JavaType type) {
return (T) getClassIntrospector().forCreation(this, type, this);
}
/**
* @since 2.0
*/
@SuppressWarnings("unchecked")
public T introspectForBuilder(JavaType type) {
return (T) getClassIntrospector().forDeserializationWithBuilder(this, type, this);
}
/*
/**********************************************************
/* Support for polymorphic type handling
/**********************************************************
*/
/**
* Helper method that is needed to properly handle polymorphic referenced
* types, such as types referenced by {@link java.util.concurrent.atomic.AtomicReference},
* or various "optional" types.
*
* @since 2.4
*/
public TypeDeserializer findTypeDeserializer(JavaType baseType)
throws JsonMappingException
{
BeanDescription bean = introspectClassAnnotations(baseType.getRawClass());
AnnotatedClass ac = bean.getClassInfo();
TypeResolverBuilder> b = getAnnotationIntrospector().findTypeResolver(this, ac, baseType);
/* Ok: if there is no explicit type info handler, we may want to
* use a default. If so, config object knows what to use.
*/
Collection subtypes = null;
if (b == null) {
b = getDefaultTyper(baseType);
if (b == null) {
return null;
}
} else {
subtypes = getSubtypeResolver().collectAndResolveSubtypes(ac, this, getAnnotationIntrospector());
}
/* 04-May-2014, tatu: When called from DeserializerFactory, additional code like
* this is invoked. But here we do not actually have access to mappings, so not
* quite sure what to do, if anything. May need to revisit if the underlying
* problem re-surfaces...
*/
/*
if ((b.getDefaultImpl() == null) && baseType.isAbstract()) {
JavaType defaultType = mapAbstractType(config, baseType);
if (defaultType != null && defaultType.getRawClass() != baseType.getRawClass()) {
b = b.defaultImpl(defaultType.getRawClass());
}
}
*/
return b.buildTypeDeserializer(this, baseType, subtypes);
}
}
DeserializationContext.java 0000664 0000000 0000000 00000075647 12373034547 0036034 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdResolver;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ReadableObjectId;
import com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.*;
/**
* Context for the process of deserialization a single root-level value.
* Used to allow passing in configuration settings and reusable temporary
* objects (scrap arrays, containers).
*
* Instance life-cycle is such that an partially configured "blueprint" object
* is registered with {@link ObjectMapper} (and {@link ObjectReader},
* and when an actual instance is needed for deserialization,
* a fully configured instance will
* be created using a method in excented API of sub-class
* ({@link com.fasterxml.jackson.databind.deser.DefaultDeserializationContext#createInstance}).
* Each instance is guaranteed to only be used from single-threaded context;
* instances may be reused iff no configuration has changed.
*
* Defined as abstract class so that implementations must define methods
* for reconfiguring blueprints and creating instances.
*/
public abstract class DeserializationContext
extends DatabindContext
implements java.io.Serializable
{
private static final long serialVersionUID = -4290063686213707727L;
/**
* Let's limit length of error messages, for cases where underlying data
* may be very large -- no point in spamming logs with megs of meaningless
* data.
*/
private final static int MAX_ERROR_STR_LEN = 500;
/*
/**********************************************************
/* Configuration, immutable
/**********************************************************
*/
/**
* Object that handle details of {@link JsonDeserializer} caching.
*/
protected final DeserializerCache _cache;
/*
/**********************************************************
/* Configuration, changeable via fluent factories
/**********************************************************
*/
/**
* Read-only factory instance; exposed to let
* owners (ObjectMapper
, ObjectReader
)
* access it.
*/
protected final DeserializerFactory _factory;
/*
/**********************************************************
/* Configuration that gets set for instances (not blueprints)
/* (partly denormalized for performance)
/**********************************************************
*/
/**
* Generic deserialization processing configuration
*/
protected final DeserializationConfig _config;
/**
* Bitmap of {@link DeserializationFeature}s that are enabled
*/
protected final int _featureFlags;
/**
* Currently active view, if any.
*/
protected final Class> _view;
/**
* Currently active parser used for deserialization.
* May be different from the outermost parser
* when content is buffered.
*/
protected transient JsonParser _parser;
/**
* Object used for resolving references to injectable
* values.
*/
protected final InjectableValues _injectableValues;
/*
/**********************************************************
/* Per-operation reusable helper objects (not for blueprints)
/**********************************************************
*/
protected transient ArrayBuilders _arrayBuilders;
protected transient ObjectBuffer _objectBuffer;
protected transient DateFormat _dateFormat;
/**
* Lazily-constructed holder for per-call attributes.
*
* @since 2.3
*/
protected transient ContextAttributes _attributes;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
protected DeserializationContext(DeserializerFactory df) {
this(df, null);
}
protected DeserializationContext(DeserializerFactory df,
DeserializerCache cache)
{
if (df == null) {
throw new IllegalArgumentException("Can not pass null DeserializerFactory");
}
_factory = df;
_cache = (cache == null) ? new DeserializerCache() : cache;
_featureFlags = 0;
_config = null;
_injectableValues = null;
_view = null;
_attributes = null;
}
protected DeserializationContext(DeserializationContext src,
DeserializerFactory factory)
{
_cache = src._cache;
_factory = factory;
_config = src._config;
_featureFlags = src._featureFlags;
_view = src._view;
_parser = src._parser;
_injectableValues = src._injectableValues;
_attributes = src._attributes;
}
/**
* Constructor used for creating actual per-call instances.
*/
protected DeserializationContext(DeserializationContext src,
DeserializationConfig config, JsonParser p,
InjectableValues injectableValues)
{
_cache = src._cache;
_factory = src._factory;
_config = config;
_featureFlags = config.getDeserializationFeatures();
_view = config.getActiveView();
_parser = p;
_injectableValues = injectableValues;
_attributes = config.getAttributes();
}
/*
/**********************************************************
/* DatabindContext implementation
/**********************************************************
*/
@Override
public DeserializationConfig getConfig() { return _config; }
@Override
public final Class> getActiveView() { return _view; }
@Override
public final AnnotationIntrospector getAnnotationIntrospector() {
return _config.getAnnotationIntrospector();
}
@Override
public final TypeFactory getTypeFactory() {
return _config.getTypeFactory();
}
/*
/**********************************************************
/* Generic attributes (2.3+)
/**********************************************************
*/
@Override
public Object getAttribute(Object key) {
return _attributes.getAttribute(key);
}
@Override
public DeserializationContext setAttribute(Object key, Object value)
{
_attributes = _attributes.withPerCallAttribute(key, value);
return this;
}
/*
/**********************************************************
/* Public API, accessors
/**********************************************************
*/
/**
* Method for getting current {@link DeserializerFactory}.
*/
public DeserializerFactory getFactory() {
return _factory;
}
/**
* Convenience method for checking whether specified on/off
* feature is enabled
*/
public final boolean isEnabled(DeserializationFeature feat) {
/* 03-Dec-2010, tatu: minor shortcut; since this is called quite often,
* let's use a local copy of feature settings:
*/
return (_featureFlags & feat.getMask()) != 0;
}
/**
* "Bulk" access method for checking that all features specified by
* mask are enabled.
*
* @since 2.3
*/
public final boolean hasDeserializationFeatures(int featureMask) {
return _config.hasDeserializationFeatures(featureMask);
}
/**
* Method for accessing the currently active parser.
* May be different from the outermost parser
* when content is buffered.
*
* Use of this method is discouraged: if code has direct access
* to the active parser, that should be used instead.
*/
public final JsonParser getParser() { return _parser; }
public final Object findInjectableValue(Object valueId,
BeanProperty forProperty, Object beanInstance)
{
if (_injectableValues == null) {
throw new IllegalStateException("No 'injectableValues' configured, can not inject value with id ["+valueId+"]");
}
return _injectableValues.findInjectableValue(valueId, this, forProperty, beanInstance);
}
/**
* Convenience method for accessing the default Base64 encoding
* used for decoding base64 encoded binary content.
* Same as calling:
*
* getConfig().getBase64Variant();
*
*/
public final Base64Variant getBase64Variant() {
return _config.getBase64Variant();
}
/**
* Convenience method, functionally equivalent to:
*
* getConfig().getNodeFactory();
*
*/
public final JsonNodeFactory getNodeFactory() {
return _config.getNodeFactory();
}
/**
* Method for accessing default Locale to use: convenience method for
*
* getConfig().getLocale();
*
*/
public Locale getLocale() {
return _config.getLocale();
}
/**
* Method for accessing default TimeZone to use: convenience method for
*
* getConfig().getTimeZone();
*
*/
public TimeZone getTimeZone() {
return _config.getTimeZone();
}
/*
/**********************************************************
/* Public API, pass-through to DeserializerCache
/**********************************************************
*/
@Deprecated // since 2.3, use overloaded variant
public boolean hasValueDeserializerFor(JavaType type) {
return hasValueDeserializerFor(type, null);
}
/**
* Method for checking whether we could find a deserializer
* for given type.
*
* @param type
* @since 2.3
*/
public boolean hasValueDeserializerFor(JavaType type, AtomicReference cause) {
try {
return _cache.hasValueDeserializerFor(this, _factory, type);
} catch (JsonMappingException e) {
if (cause != null) {
cause.set(e);
}
} catch (RuntimeException e) {
if (cause == null) { // earlier behavior
throw e;
}
cause.set(e);
}
return false;
}
/**
* Method for finding a value deserializer, and creating a contextual
* version if necessary, for value reached via specified property.
*/
@SuppressWarnings("unchecked")
public final JsonDeserializer findContextualValueDeserializer(JavaType type,
BeanProperty prop) throws JsonMappingException
{
JsonDeserializer deser = _cache.findValueDeserializer(this, _factory, type);
if (deser != null) {
deser = (JsonDeserializer) handleSecondaryContextualization(deser, prop);
}
return deser;
}
/**
* Method for finding a deserializer for root-level value.
*/
@SuppressWarnings("unchecked")
public final JsonDeserializer findRootValueDeserializer(JavaType type)
throws JsonMappingException
{
JsonDeserializer deser = _cache.findValueDeserializer(this,
_factory, type);
if (deser == null) { // can this occur?
return null;
}
deser = (JsonDeserializer) handleSecondaryContextualization(deser, null);
TypeDeserializer typeDeser = _factory.findTypeDeserializer(_config, type);
if (typeDeser != null) {
// important: contextualize to indicate this is for root value
typeDeser = typeDeser.forProperty(null);
return new TypeWrappedDeserializer(typeDeser, deser);
}
return deser;
}
/**
* Convenience method, functionally same as:
*
* getDeserializerProvider().findKeyDeserializer(getConfig(), prop.getType(), prop);
*
*/
public final KeyDeserializer findKeyDeserializer(JavaType keyType,
BeanProperty prop) throws JsonMappingException {
KeyDeserializer kd = _cache.findKeyDeserializer(this,
_factory, keyType);
// Second: contextualize?
if (kd instanceof ContextualKeyDeserializer) {
kd = ((ContextualKeyDeserializer) kd).createContextual(this, prop);
}
return kd;
}
/*
/**********************************************************
/* Public API, ObjectId handling
/**********************************************************
*/
/**
* Method called to find and return entry corresponding to given
* Object Id: will add an entry if necessary, and never returns null
*/
public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator> generator, ObjectIdResolver resolver);
@Deprecated // since 2.4
public abstract ReadableObjectId findObjectId(Object id, ObjectIdGenerator> generator);
/**
* Method called to ensure that every object id encounter during processing
* are resolved.
*
* @throws UnresolvedForwardReference
*/
public abstract void checkUnresolvedObjectId()
throws UnresolvedForwardReference;
/*
/**********************************************************
/* Public API, type handling
/**********************************************************
*/
/**
* Convenience method, functionally equivalent to:
*
* getConfig().constructType(cls);
*
*/
public final JavaType constructType(Class> cls) {
return _config.constructType(cls);
}
/**
* Helper method to use for locating Class for given name. Should be used
* instead of basic Class.forName(className);
as it can
* try using contextual class loader, or use platform-specific workarounds
* (like on Android, GAE).
*/
public Class> findClass(String className) throws ClassNotFoundException
{
// By default, delegate to ClassUtil: can be overridden with custom handling
return ClassUtil.findClass(className);
}
/*
/**********************************************************
/* Public API, helper object recycling
/**********************************************************
*/
/**
* Method that can be used to get access to a reusable ObjectBuffer,
* useful for efficiently constructing Object arrays and Lists.
* Note that leased buffers should be returned once deserializer
* is done, to allow for reuse during same round of deserialization.
*/
public final ObjectBuffer leaseObjectBuffer()
{
ObjectBuffer buf = _objectBuffer;
if (buf == null) {
buf = new ObjectBuffer();
} else {
_objectBuffer = null;
}
return buf;
}
/**
* Method to call to return object buffer previously leased with
* {@link #leaseObjectBuffer}.
*
* @param buf Returned object buffer
*/
public final void returnObjectBuffer(ObjectBuffer buf)
{
/* Already have a reusable buffer? Let's retain bigger one
* (or if equal, favor newer one, shorter life-cycle)
*/
if (_objectBuffer == null
|| buf.initialCapacity() >= _objectBuffer.initialCapacity()) {
_objectBuffer = buf;
}
}
/**
* Method for accessing object useful for building arrays of
* primitive types (such as int[]).
*/
public final ArrayBuilders getArrayBuilders()
{
if (_arrayBuilders == null) {
_arrayBuilders = new ArrayBuilders();
}
return _arrayBuilders;
}
/*
/**********************************************************
/* Extended API: handler instantiation
/**********************************************************
*/
public abstract JsonDeserializer deserializerInstance(Annotated annotated,
Object deserDef)
throws JsonMappingException;
public abstract KeyDeserializer keyDeserializerInstance(Annotated annotated,
Object deserDef)
throws JsonMappingException;
/*
/**********************************************************
/* Extended API: resolving contextual deserializers; called
/* by structured deserializers for their value/component
/* deserializers
/**********************************************************
*/
/**
* Method called for primary property deserializers (ones
* directly created to deserialize values of a POJO property),
* to handle details of resolving
* {@link ContextualDeserializer} with given property context.
*
* @param prop Property for which the given primary deserializer is used; never null.
*
* @since 2.3
*/
public JsonDeserializer> handlePrimaryContextualization(JsonDeserializer> deser,
BeanProperty prop)
throws JsonMappingException
{
if (deser != null) {
if (deser instanceof ContextualDeserializer) {
deser = ((ContextualDeserializer) deser).createContextual(this, prop);
}
}
return deser;
}
/**
* Method called for secondary property deserializers (ones
* NOT directly created to deal with an annotatable POJO property,
* but instead created as a component -- such as value deserializers
* for structured types, or deserializers for root values)
* to handle details of resolving
* {@link ContextualDeserializer} with given property context.
* Given that these deserializers are not directly related to given property
* (or, in case of root value property, to any property), annotations
* accessible may or may not be relevant.
*
* @param prop Property for which deserializer is used, if any; null
* when deserializing root values
*
* @since 2.3
*/
public JsonDeserializer> handleSecondaryContextualization(JsonDeserializer> deser,
BeanProperty prop)
throws JsonMappingException {
if (deser != null && (deser instanceof ContextualDeserializer)) {
deser = ((ContextualDeserializer) deser).createContextual(this, prop);
}
return deser;
}
/*
/**********************************************************
/* Parsing methods that may use reusable/-cyclable objects
/**********************************************************
*/
/**
* Convenience method for parsing a Date from given String, using
* currently configured date format (accessed using
* {@link DeserializationConfig#getDateFormat()}).
*
* Implementation will handle thread-safety issues related to
* date formats such that first time this method is called,
* date format is cloned, and cloned instance will be retained
* for use during this deserialization round.
*/
public Date parseDate(String dateStr) throws IllegalArgumentException
{
try {
DateFormat df = getDateFormat();
return df.parse(dateStr);
} catch (ParseException e) {
throw new IllegalArgumentException("Failed to parse Date value '"+dateStr+"': "+e.getMessage());
}
}
/**
* Convenience method for constructing Calendar instance set
* to specified time, to be modified and used by caller.
*/
public Calendar constructCalendar(Date d) {
// 08-Jan-2008, tatu: not optimal, but should work for the most part; let's revise as needed.
Calendar c = Calendar.getInstance(getTimeZone());
c.setTime(d);
return c;
}
/*
/**********************************************************
/* Convenience methods for reading parsed values
/**********************************************************
*/
/**
* Convenience method that may be used by composite or container deserializers,
* for reading one-off values contained (for sequences, it is more efficient
* to actually fetch deserializer once for the whole collection).
*
* NOTE: when deserializing values of properties contained in composite types,
* rather use {@link #readPropertyValue(JsonParser, BeanProperty, Class)};
* this method does not allow use of contextual annotations.
*
* @since 2.4
*/
public T readValue(JsonParser p, Class type) throws IOException {
return readValue(p, getTypeFactory().constructType(type));
}
/**
* @since 2.4
*/
@SuppressWarnings("unchecked")
public T readValue(JsonParser p, JavaType type) throws IOException {
JsonDeserializer deser = findRootValueDeserializer(type);
if (deser == null) {
}
return (T) deser.deserialize(p, this);
}
/**
* Convenience method that may be used by composite or container deserializers,
* for reading one-off values for the composite type, taking into account
* annotations that the property (passed to this method -- usually property that
* has custom serializer that called this method) has.
*
* @since 2.4
*/
public T readPropertyValue(JsonParser p, BeanProperty prop, Class type) throws IOException {
return readPropertyValue(p, prop, getTypeFactory().constructType(type));
}
/**
* @since 2.4
*/
@SuppressWarnings("unchecked")
public T readPropertyValue(JsonParser p, BeanProperty prop, JavaType type) throws IOException {
JsonDeserializer deser = findContextualValueDeserializer(type, prop);
if (deser == null) {
}
return (T) deser.deserialize(p, this);
}
/*
/**********************************************************
/* Methods for problem handling, reporting
/**********************************************************
*/
/**
* Method deserializers can call to inform configured {@link DeserializationProblemHandler}s
* of an unrecognized property.
*
* @return True if there was a configured problem handler that was able to handle the
* problem
*/
/**
* Method deserializers can call to inform configured {@link DeserializationProblemHandler}s
* of an unrecognized property.
*/
public boolean handleUnknownProperty(JsonParser p, JsonDeserializer> deser,
Object instanceOrClass, String propName)
throws IOException, JsonProcessingException
{
LinkedNode h = _config.getProblemHandlers();
if (h != null) {
while (h != null) {
// Can bail out if it's handled
if (h.value().handleUnknownProperty(this, p, deser, instanceOrClass, propName)) {
return true;
}
h = h.next();
}
}
return false;
}
/**
* Helper method for reporting a problem with unhandled unknown exception
*
* @param instanceOrClass Either value being populated (if one has been
* instantiated), or Class that indicates type that would be (or
* have been) instantiated
* @param deser Deserializer that had the problem, if called by deserializer
* (or on behalf of one)
*/
public void reportUnknownProperty(Object instanceOrClass, String fieldName,
JsonDeserializer> deser)
throws JsonMappingException
{
if (!isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)) {
return;
}
// Do we know properties that are expected instead?
Collection propIds = (deser == null) ? null : deser.getKnownPropertyNames();
throw UnrecognizedPropertyException.from(_parser,
instanceOrClass, fieldName, propIds);
}
/*
/**********************************************************
/* Methods for constructing exceptions
/**********************************************************
*/
/**
* Helper method for constructing generic mapping exception for specified type
*/
public JsonMappingException mappingException(Class> targetClass) {
return mappingException(targetClass, _parser.getCurrentToken());
}
public JsonMappingException mappingException(Class> targetClass, JsonToken token) {
return JsonMappingException.from(_parser, "Can not deserialize instance of "+_calcName(targetClass)+" out of "+token+" token");
}
/**
* Helper method for constructing generic mapping exception with specified
* message and current location information
*/
public JsonMappingException mappingException(String message) {
return JsonMappingException.from(getParser(), message);
}
/**
* Helper method for constructing instantiation exception for specified type,
* to indicate problem with physically constructing instance of
* specified class (missing constructor, exception from constructor)
*/
public JsonMappingException instantiationException(Class> instClass, Throwable t) {
return JsonMappingException.from(_parser,
"Can not construct instance of "+instClass.getName()+", problem: "+t.getMessage(), t);
}
public JsonMappingException instantiationException(Class> instClass, String msg) {
return JsonMappingException.from(_parser, "Can not construct instance of "+instClass.getName()+", problem: "+msg);
}
/**
* Method that will construct an exception suitable for throwing when
* some String values are acceptable, but the one encountered is not.
*
*
* @deprecated Since 2.1 should use variant that takes value
*/
@Deprecated
public JsonMappingException weirdStringException(Class> instClass, String msg) {
return weirdStringException(null, instClass, msg);
}
/**
* Method that will construct an exception suitable for throwing when
* some String values are acceptable, but the one encountered is not.
*
* @param value String value from input being deserialized
* @param instClass Type that String should be deserialized into
* @param msg Message that describes specific problem
*
* @since 2.1
*/
public JsonMappingException weirdStringException(String value, Class> instClass, String msg) {
return InvalidFormatException.from(_parser,
"Can not construct instance of "+instClass.getName()+" from String value '"+_valueDesc()+"': "+msg,
value, instClass);
}
/**
* Helper method for constructing exception to indicate that input JSON
* Number was not suitable for deserializing into given type.
*/
@Deprecated
public JsonMappingException weirdNumberException(Class> instClass, String msg) {
return weirdStringException(null, instClass, msg);
}
/**
* Helper method for constructing exception to indicate that input JSON
* Number was not suitable for deserializing into given target type.
*/
public JsonMappingException weirdNumberException(Number value, Class> instClass, String msg) {
return InvalidFormatException.from(_parser,
"Can not construct instance of "+instClass.getName()+" from number value ("+_valueDesc()+"): "+msg,
null, instClass);
}
/**
* Helper method for constructing exception to indicate that given JSON
* Object field name was not in format to be able to deserialize specified
* key type.
*/
public JsonMappingException weirdKeyException(Class> keyClass, String keyValue, String msg) {
return InvalidFormatException.from(_parser,
"Can not construct Map key of type "+keyClass.getName()+" from String \""+_desc(keyValue)+"\": "+msg,
keyValue, keyClass);
}
/**
* Helper method for indicating that the current token was expected to be another
* token.
*/
public JsonMappingException wrongTokenException(JsonParser p, JsonToken expToken, String msg0) {
String msg = "Unexpected token ("+p.getCurrentToken()+"), expected "+expToken;
if (msg0 != null) {
msg = msg + ": "+msg0;
}
return JsonMappingException.from(p, msg);
}
/**
* Helper method for constructing exception to indicate that given
* type id (parsed from JSON) could not be converted to a Java type.
*/
public JsonMappingException unknownTypeException(JavaType type, String id) {
return JsonMappingException.from(_parser, "Could not resolve type id '"+id+"' into a subtype of "+type);
}
public JsonMappingException endOfInputException(Class> instClass) {
return JsonMappingException.from(_parser, "Unexpected end-of-input when trying to deserialize a "
+instClass.getName());
}
/*
/**********************************************************
/* Overridable internal methods
/**********************************************************
*/
protected DateFormat getDateFormat()
{
if (_dateFormat != null) {
return _dateFormat;
}
/* 24-Feb-2012, tatu: At this point, all timezone configuration
* should have occured, with respect to default dateformat
* and timezone configuration. But we still better clone
* an instance as formatters may be stateful.
*/
DateFormat df = _config.getDateFormat();
_dateFormat = df = (DateFormat) df.clone();
return df;
}
protected String determineClassName(Object instance) {
return ClassUtil.getClassDescription(instance);
}
/*
/**********************************************************
/* Other internal methods
/**********************************************************
*/
protected String _calcName(Class> cls) {
if (cls.isArray()) {
return _calcName(cls.getComponentType())+"[]";
}
return cls.getName();
}
protected String _valueDesc() {
try {
return _desc(_parser.getText());
} catch (Exception e) {
return "[N/A]";
}
}
protected String _desc(String desc) {
// !!! should we quote it? (in case there are control chars, linefeeds)
if (desc.length() > MAX_ERROR_STR_LEN) {
desc = desc.substring(0, MAX_ERROR_STR_LEN) + "]...[" + desc.substring(desc.length() - MAX_ERROR_STR_LEN);
}
return desc;
}
}
DeserializationFeature.java 0000664 0000000 0000000 00000032377 12373034547 0035774 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import com.fasterxml.jackson.databind.cfg.ConfigFeature;
/**
* Enumeration that defines simple on/off features that affect
* the way Java objects are deserialized from JSON
*
* Note that features can be set both through
* {@link ObjectMapper} (as sort of defaults) and through
* {@link ObjectReader}.
* In first case these defaults must follow "config-then-use" patterns
* (i.e. defined once, not changed afterwards); all per-call
* changes must be done using {@link ObjectReader}.
*
* Note that features that do not indicate version of inclusion
* were available in Jackson 2.0 (or earlier); only later additions
* indicate version of inclusion.
*/
public enum DeserializationFeature implements ConfigFeature
{
/*
/******************************************************
/* Type conversion features
/******************************************************
*/
/**
* 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),
// [JACKSON-652]
/**
* Feature that determines whether JSON Array is mapped to
* Object[]
or List<Object>
when binding
* "untyped" objects (ones with nominal type of java.lang.Object
).
* If true, binds as Object[]
; if false, as List<Object>
.
*
* Feature is disabled by default, meaning that JSON arrays are bound as
* {@link java.util.List}s.
*/
USE_JAVA_ARRAY_FOR_JSON_ARRAY(false),
/**
* Feature that determines standard deserialization mechanism used for
* Enum values: if enabled, Enums are assumed to have been serialized using
* return value of Enum.toString()
;
* if disabled, return value of Enum.name()
is assumed to have been used.
*
* Note: this feature should usually have same value
* as {@link SerializationFeature#WRITE_ENUMS_USING_TO_STRING}.
*
* Feature is disabled by default.
*/
READ_ENUMS_USING_TO_STRING(false),
/*
/******************************************************
* Error handling features
/******************************************************
*/
/**
* Feature that determines whether encountering of unknown
* properties (ones that do not map to a property, and there is
* no "any setter" or handler that can handle it)
* should result in a failure (by throwing a
* {@link JsonMappingException}) or not.
* This setting only takes effect after all other handling
* methods for unknown properties have been tried, and
* property remains unhandled.
*
* Feature is enabled by default (meaning that a
* {@link JsonMappingException} will be thrown if an unknown property
* is encountered).
*/
FAIL_ON_UNKNOWN_PROPERTIES(true),
/**
* Feature that determines whether encountering of JSON null
* is an error when deserializing into Java primitive types
* (like 'int' or 'double'). If it is, a JsonProcessingException
* is thrown to indicate this; if not, default value is used
* (0 for 'int', 0.0 for double, same defaulting as what JVM uses).
*
* Feature is disabled by default.
*/
FAIL_ON_NULL_FOR_PRIMITIVES(false),
/**
* Feature that determines whether JSON integer numbers are valid
* values to be used for deserializing Java enum values.
* If set to 'false' numbers are acceptable and are used to map to
* ordinal() of matching enumeration value; if 'true', numbers are
* not allowed and a {@link JsonMappingException} will be thrown.
* Latter behavior makes sense if there is concern that accidental
* mapping from integer values to enums might happen (and when enums
* are always serialized as JSON Strings)
*
* Feature is disabled by default.
*/
FAIL_ON_NUMBERS_FOR_ENUMS(false),
/**
* Feature that determines what happens when type of a polymorphic
* value (indicated for example by {@link com.fasterxml.jackson.annotation.JsonTypeInfo})
* can not be found (missing) or resolved (invalid class name, unmappable id);
* if enabled, an exception ir thrown; if false, null value is used instead.
*
* Feature is enabled by default so that exception is thrown for missing or invalid
* type information.
*
* @since 2.2
*/
FAIL_ON_INVALID_SUBTYPE(true),
/**
* Feature that determines what happens when reading JSON content into tree
* ({@link com.fasterxml.jackson.core.TreeNode}) and a duplicate key
* is encountered (property name that was already seen for the JSON Object).
* If enabled, {@link JsonMappingException} will be thrown; if disabled, no exception
* is thrown and the new (later) value overwrites the earlier value.
*
* Note that this property does NOT affect other aspects of data-binding; that is,
* no detection is done with respect to POJO properties or {@link java.util.Map}
* keys. New features may be added to control additional cases.
*
* Feature is disabled by default so that no exception is thrown.
*
* @since 2.3
*/
FAIL_ON_READING_DUP_TREE_KEY(false),
/**
* Feature that determines what happens when a property that has been explicitly
* marked as ignorable is encountered in input: if feature is enabled,
* {@link JsonMappingException} is thrown; if false, property is quietly skipped.
*
* Feature is disabled by default so that no exception is thrown.
*
* @since 2.3
*/
FAIL_ON_IGNORED_PROPERTIES(false),
/**
* Feature that determines whether Jackson code should catch
* and wrap {@link Exception}s (but never {@link Error}s!)
* to add additional information about
* location (within input) of problem or not. If enabled,
* most exceptions will be caught and re-thrown (exception
* specifically being that {@link java.io.IOException}s may be passed
* as is, since they are declared as throwable); this can be
* convenient both in that all exceptions will be checked and
* declared, and so there is more contextual information.
* However, sometimes calling application may just want "raw"
* unchecked exceptions passed as is.
*
* Feature is enabled by default.
*/
WRAP_EXCEPTIONS(true),
/*
/******************************************************
/* Structural conversion features
/******************************************************
*/
/**
* Feature that determines whether it is acceptable to coerce non-array
* (in JSON) values to work with Java collection (arrays, java.util.Collection)
* types. If enabled, collection deserializers will try to handle non-array
* values as if they had "implicit" surrounding JSON array.
* This feature is meant to be used for compatibility/interoperability reasons,
* to work with packages (such as XML-to-JSON converters) that leave out JSON
* array in cases where there is just a single element in array.
*
* Feature is disabled by default.
*/
ACCEPT_SINGLE_VALUE_AS_ARRAY(false),
/**
* Feature that determines whether it is acceptable to coerce single value array (in JSON)
* values to the corresponding value type. This is basically the opposite of the {@link #ACCEPT_SINGLE_VALUE_AS_ARRAY}
* feature. If more than one value is found in the array, a JsonMappingException is thrown.
*
*
* Feature is disabled by default
* @since 2.4
*/
UNWRAP_SINGLE_VALUE_ARRAYS(false),
/**
* Feature to allow "unwrapping" root-level JSON value, to match setting of
* {@link SerializationFeature#WRAP_ROOT_VALUE} used for serialization.
* Will verify that the root JSON value is a JSON Object, and that it has
* a single property with expected root name. If not, a
* {@link JsonMappingException} is thrown; otherwise value of the wrapped property
* will be deserialized as if it was the root value.
*
* Feature is disabled by default.
*/
UNWRAP_ROOT_VALUE(false),
/*
/******************************************************
/* Value conversion features
/******************************************************
*/
/**
* Feature that can be enabled to allow JSON empty String
* value ("") to be bound to POJOs as null.
* If disabled, standard POJOs can only be bound from JSON null or
* JSON Object (standard meaning that no custom deserializers or
* constructors are defined; both of which can add support for other
* kinds of JSON values); if enable, empty JSON String can be taken
* to be equivalent of JSON null.
*
* Feature is enabled by default.
*/
ACCEPT_EMPTY_STRING_AS_NULL_OBJECT(false),
/**
* Feature that allows unknown Enum values to be parsed as null values.
* If disabled, unknown Enum values will throw exceptions.
*
* Note that in some cases this will basically ignore unknown Enum values;
* this is the keys for keys of {@link java.util.EnumMap} and values
* of {@link java.util.EnumSet} (because nulls are not accepted in these
* cases).
*
* Feature is disabled by default.
*
* @since 2.0
*/
READ_UNKNOWN_ENUM_VALUES_AS_NULL(false),
/**
* Feature that controls whether numeric timestamp values are expected
* to be written using nanosecond timestamps (enabled) or not (disabled),
* if and only if datatype supports such resolution.
* Only newer datatypes (such as Java8 Date/Time) support such resolution --
* older types (pre-Java8 java.util.Date etc) and Joda do not --
* and this setting has no effect on such types.
*
* If disabled, standard millisecond timestamps are assumed.
* This is the counterpart to {@link SerializationFeature#WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS}.
*
* Feature is enabled by default, to support most accurate time values possible.
*
* @since 2.2
*/
READ_DATE_TIMESTAMPS_AS_NANOSECONDS(true),
/**
* Feature that specifies whether context provided {@link java.util.TimeZone}
* ({@link DeserializationContext#getTimeZone()} should be used to adjust Date/Time
* values on deserialization, even if value itself contains timezone information.
* If enabled, contextual TimeZone
will essentially override any other
* TimeZone information; if disabled, it will only be used if value itself does not
* contain any TimeZone information.
*
* @since 2.2
*/
ADJUST_DATES_TO_CONTEXT_TIME_ZONE(true),
/*
/******************************************************
/* Other
/******************************************************
*/
/**
* Feature that determines whether {@link ObjectReader} should
* try to eagerly fetch necessary {@link JsonDeserializer} when
* possible. This improves performance in cases where similarly
* configured {@link ObjectReader} instance is used multiple
* times; and should not significantly affect single-use cases.
*
* Note that there should not be any need to normally disable this
* feature: only consider that if there are actual perceived problems.
*
* Feature is enabled by default.
*
* @since 2.1
*/
EAGER_DESERIALIZER_FETCH(true)
;
private final boolean _defaultState;
private DeserializationFeature(boolean defaultState) {
_defaultState = defaultState;
}
@Override
public boolean enabledByDefault() { return _defaultState; }
@Override
public int getMask() { return (1 << ordinal()); }
} InjectableValues.java 0000664 0000000 0000000 00000005657 12373034547 0034553 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.util.*;
/**
* Abstract class that defines API for objects that provide value to
* "inject" during deserialization. An instance of this object
*/
public abstract class InjectableValues
{
/**
* Method called to find value identified by id valueId
to
* inject as value of specified property during deserialization, passing
* POJO instance in which value will be injected if it is available
* (will be available when injected via field or setter; not available
* when injected via constructor or factory method argument).
*
* @param valueId Object that identifies value to inject; may be a simple
* name or more complex identifier object, whatever provider needs
* @param ctxt Deserialization context
* @param forProperty Bean property in which value is to be injected
* @param beanInstance Bean instance that contains property to inject,
* if available; null if bean has not yet been constructed.
*/
public abstract Object findInjectableValue(Object valueId, DeserializationContext ctxt,
BeanProperty forProperty, Object beanInstance);
/*
/**********************************************************
/* Standard implementation
/**********************************************************
*/
/**
* Simple standard implementation which uses a simple Map to
* store values to inject, identified by simple String keys.
*/
public static class Std
extends InjectableValues
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
protected final Map _values;
public Std() {
this(new HashMap());
}
public Std(Map values) {
_values = values;
}
public Std addValue(String key, Object value) {
_values.put(key, value);
return this;
}
public Std addValue(Class> classKey, Object value) {
_values.put(classKey.getName(), value);
return this;
}
@Override
public Object findInjectableValue(Object valueId, DeserializationContext ctxt,
BeanProperty forProperty, Object beanInstance)
{
if (!(valueId instanceof String)) {
String type = (valueId == null) ? "[null]" : valueId.getClass().getName();
throw new IllegalArgumentException("Unrecognized inject value id type ("+type+"), expecting String");
}
String key = (String) valueId;
Object ob = _values.get(key);
if (ob == null && !_values.containsKey(key)) {
throw new IllegalArgumentException("No injectable id with value '"+key+"' found (for property '"+forProperty.getName()+"')");
}
return ob;
}
}
}
jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind/JavaType.java 0000664 0000000 0000000 00000033767 12373034547 0033140 0 ustar 00root root 0000000 0000000 package com.fasterxml.jackson.databind;
import java.lang.reflect.Modifier;
import com.fasterxml.jackson.core.type.ResolvedType;
/**
* Base class for type token classes used both to contain information
* and as keys for deserializers.
*
* Instances can (only) be constructed by
* com.fasterxml.jackson.databind.TypeFactory
.
*
* Since 2.2 this implements {@link java.lang.reflect.Type} to allow
* it to be pushed through interfaces that only expose that type.
*/
public abstract class JavaType
extends ResolvedType
implements java.io.Serializable, // 2.1
java.lang.reflect.Type // 2.2
{
private static final long serialVersionUID = 1;
/**
* This is the nominal type-erased Class that would be close to the
* type represented (but not exactly type, due to type erasure: type
* instance may have more information on this).
* May be an interface or abstract class, so instantiation
* may not be possible.
*/
protected final Class> _class;
protected final int _hash;
/**
* Optional handler (codec) that can be attached to indicate
* what to use for handling (serializing, deserializing) values of
* this specific type.
*
* Note: untyped (i.e. caller has to cast) because it is used for
* different kinds of handlers, with unrelated types.
*/
protected final Object _valueHandler;
/**
* Optional handler that can be attached to indicate how to handle
* additional type metadata associated with this type.
*
* Note: untyped (i.e. caller has to cast) because it is used for
* different kinds of handlers, with unrelated types.
*/
protected final Object _typeHandler;
/**
* Whether entities defined with this type should be handled using
* static typing (as opposed to dynamic runtime type) or not.
*
* @since 2.2
*/
protected final boolean _asStatic;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
/**
* @param raw "Raw" (type-erased) class for this type
* @param additionalHash Additional hash code to use, in addition
* to hash code of the class name
*/
protected JavaType(Class> raw, int additionalHash,
Object valueHandler, Object typeHandler, boolean asStatic)
{
_class = raw;
_hash = raw.getName().hashCode() + additionalHash;
_valueHandler = valueHandler;
_typeHandler = typeHandler;
_asStatic = asStatic;
}
/**
* "Copy method" that will construct a new instance that is identical to
* this instance, except that it will have specified type handler assigned.
*
* @return Newly created type instance
*/
public abstract JavaType withTypeHandler(Object h);
/**
* "Copy method" that will construct a new instance that is identical to
* this instance, except that its content type will have specified
* type handler assigned.
*
* @return Newly created type instance
*/
public abstract JavaType withContentTypeHandler(Object h);
/**
* "Copy method" that will construct a new instance that is identical to
* this instance, except that it will have specified value handler assigned.
*
* @return Newly created type instance
*/
public abstract JavaType withValueHandler(Object h);
/**
* "Copy method" that will construct a new instance that is identical to
* this instance, except that it will have specified content value handler assigned.
*
* @return Newly created type instance
*/
public abstract JavaType withContentValueHandler(Object h);
/**
* Method that can be called to get a type instance that indicates
* that values of the type should be handled using "static typing" for purposes
* of serialization (as opposed to "dynamic" aka runtime typing):
* meaning that no runtime information is needed for determining serializers to use.
* The main use case is to allow forcing of specific root value serialization type,
* and specifically in resolving serializers for contained types (element types
* for arrays, Collections and Maps).
*
* @since 2.2
*/
public abstract JavaType withStaticTyping();
/*
/**********************************************************
/* Type coercion fluent factory methods
/**********************************************************
*/
/**
* Method that can be called to do a "narrowing" conversions; that is,
* to return a type with a raw class that is assignable to the raw
* class of this type. If this is not possible, an
* {@link IllegalArgumentException} is thrown.
* If class is same as the current raw class, instance itself is
* returned.
*/
public JavaType narrowBy(Class> subclass)
{
// First: if same raw class, just return this instance
if (subclass == _class) { return this; }
// Otherwise, ensure compatibility
_assertSubclass(subclass, _class);
JavaType result = _narrow(subclass);
// TODO: these checks should NOT actually be needed; above should suffice:
if (_valueHandler != result.getValueHandler()) {
result = result.withValueHandler(_valueHandler);
}
if (_typeHandler != result.getTypeHandler()) {
result = result.withTypeHandler(_typeHandler);
}
return result;
}
/**
* More efficient version of {@link #narrowBy}, called by
* internal framework in cases where compatibility checks
* are to be skipped.
*/
public JavaType forcedNarrowBy(Class> subclass)
{
if (subclass == _class) { // can still optimize for simple case
return this;
}
JavaType result = _narrow(subclass);
// TODO: these checks should NOT actually be needed; above should suffice:
if (_valueHandler != result.getValueHandler()) {
result = result.withValueHandler(_valueHandler);
}
if (_typeHandler != result.getTypeHandler()) {
result = result.withTypeHandler(_typeHandler);
}
return result;
}
/**
* Method that can be called to do a "widening" conversions; that is,
* to return a type with a raw class that could be assigned from this
* type.
* If such conversion is not possible, an
* {@link IllegalArgumentException} is thrown.
* If class is same as the current raw class, instance itself is
* returned.
*/
public JavaType widenBy(Class> superclass) {
// First: if same raw class, just return this instance
if (superclass == _class) { return this; }
// Otherwise, ensure compatibility
_assertSubclass(_class, superclass);
return _widen(superclass);
}
protected abstract JavaType _narrow(Class> subclass);
/**
*
* Default implementation is just to call {@link #_narrow}, since
* underlying type construction is usually identical
*/
protected JavaType _widen(Class> superclass) { return _narrow(superclass); }
public abstract JavaType narrowContentsBy(Class> contentClass);
public abstract JavaType widenContentsBy(Class> contentClass);
/*
/**********************************************************
/* Implementation of ResolvedType API
/**********************************************************
*/
@Override
public final Class> getRawClass() { return _class; }
/**
* Method that can be used to check whether this type has
* specified Class as its type erasure. Put another way, returns
* true if instantiation of this Type is given (type-erased) Class.
*/
@Override
public final boolean hasRawClass(Class> clz) { return _class == clz; }
@Override
public boolean isAbstract() {
return Modifier.isAbstract(_class.getModifiers());
}
/**
* Convenience method for checking whether underlying Java type
* is a concrete class or not: abstract classes and interfaces
* are not.
*/
@Override
public boolean isConcrete() {
int mod = _class.getModifiers();
if ((mod & (Modifier.INTERFACE | Modifier.ABSTRACT)) == 0) {
return true;
}
/* 19-Feb-2010, tatus: Holy mackarel; primitive types
* have 'abstract' flag set...
*/
return _class.isPrimitive();
}
@Override
public boolean isThrowable() { return Throwable.class.isAssignableFrom(_class); }
@Override
public boolean isArrayType() { return false; }
@Override
public final boolean isEnumType() { return _class.isEnum(); }
@Override
public final boolean isInterface() { return _class.isInterface(); }
@Override
public final boolean isPrimitive() { return _class.isPrimitive(); }
@Override
public final boolean isFinal() { return Modifier.isFinal(_class.getModifiers()); }
/**
* @return True if type represented is a container type; this includes
* array, Map and Collection types.
*/
@Override
public abstract boolean isContainerType();
/**
* @return True if type is either true {@link java.util.Collection} type,
* or something similar (meaning it has at least one type parameter,
* which describes type of contents)
*/
@Override
public boolean isCollectionLikeType() { return false; }
/**
* @return True if type is either true {@link java.util.Map} type,
* or something similar (meaning it has at least two type parameter;
* first one describing key type, second value type)
*/
@Override
public boolean isMapLikeType() { return false; }
/**
* Accessor for checking whether handlers for dealing with values of
* this type should use static typing (as opposed to dynamic typing).
* Note that while value of 'true' does mean that static typing is to
* be used, value of 'false' may still be overridden by other settings.
*
* @since 2.2
*/
public final boolean useStaticType() { return _asStatic; }
/*
/**********************************************************
/* Public API, type parameter access; pass-through
/**********************************************************
*/
@Override
public boolean hasGenericTypes() { return containedTypeCount() > 0; }
@Override
public JavaType getKeyType() { return null; }
@Override
public JavaType getContentType() { return null; }
@Override
public int containedTypeCount() { return 0; }
@Override
public JavaType containedType(int index) { return null; }
@Override
public String containedTypeName(int index) { return null; }
/*
/**********************************************************
/* Semi-public API, accessing handlers
/**********************************************************
*/
/**
* Method for accessing value handler associated with this type, if any
*/
@SuppressWarnings("unchecked")
public T getValueHandler() { return (T) _valueHandler; }
/**
* Method for accessing type handler associated with this type, if any
*/
@SuppressWarnings("unchecked")
public T getTypeHandler() { return (T) _typeHandler; }
/*
/**********************************************************
/* Support for producing signatures
/**********************************************************
*/
//public abstract String toCanonical();
/**
* Method for accessing signature that contains generic
* type information, in form compatible with JVM 1.5
* as per JLS. It is a superset of {@link #getErasedSignature},
* in that generic information can be automatically removed
* if necessary (just remove outermost
* angle brackets along with content inside)
*/
public String getGenericSignature() {
StringBuilder sb = new StringBuilder(40);
getGenericSignature(sb);
return sb.toString();
}
/**
*
* @param sb StringBuilder to append signature to
*
* @return StringBuilder that was passed in; returned to allow
* call chaining
*/
public abstract StringBuilder getGenericSignature(StringBuilder sb);
/**
* Method for accessing signature without generic
* type information, in form compatible with all versions
* of JVM, and specifically used for type descriptions
* when generating byte code.
*/
public String getErasedSignature() {
StringBuilder sb = new StringBuilder(40);
getErasedSignature(sb);
return sb.toString();
}
/**
* Method for accessing signature without generic
* type information, in form compatible with all versions
* of JVM, and specifically used for type descriptions
* when generating byte code.
*
* @param sb StringBuilder to append signature to
*
* @return StringBuilder that was passed in; returned to allow
* call chaining
*/
public abstract StringBuilder getErasedSignature(StringBuilder sb);
/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
protected void _assertSubclass(Class> subclass, Class> superClass) {
if (!_class.isAssignableFrom(subclass)) {
throw new IllegalArgumentException("Class "+subclass.getName()+" is not assignable to "+_class.getName());
}
}
/*
/**********************************************************
/* Standard methods; let's make them abstract to force override
/**********************************************************
*/
@Override
public abstract String toString();
@Override
public abstract boolean equals(Object o);
@Override
public final int hashCode() { return _hash; }
}
JsonDeserializer.java 0000664 0000000 0000000 00000033343 12373034547 0034600 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import java.util.Collection;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.deser.BeanDeserializerFactory;
import com.fasterxml.jackson.databind.deser.SettableBeanProperty;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
* Abstract class that defines API used by {@link ObjectMapper} (and
* other chained {@link JsonDeserializer}s too) to deserialize Objects of
* arbitrary types from JSON, using provided {@link JsonParser}.
*
* Custom deserializers should usually not directly extend this class,
* but instead extend {@link com.fasterxml.jackson.databind.deser.std.StdDeserializer}
* (or its subtypes like {@link com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer}).
*
* If deserializer is an aggregate one -- meaning it delegates handling of some
* of its contents by using other deserializer(s) -- it typically also needs
* to implement {@link com.fasterxml.jackson.databind.deser.ResolvableDeserializer},
* which can locate dependant deserializers. This is important to allow dynamic
* overrides of deserializers; separate call interface is needed to separate
* resolution of dependant deserializers (which may have cyclic link back
* to deserializer itself, directly or indirectly).
*
* In addition, to support per-property annotations (to configure aspects
* of deserialization on per-property basis), deserializers may want
* to implement
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer},
* which allows specialization of deserializers: call to
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer#createContextual}
* is passed information on property, and can create a newly configured
* deserializer for handling that particular property.
*
* If both
* {@link com.fasterxml.jackson.databind.deser.ResolvableDeserializer} and
* {@link com.fasterxml.jackson.databind.deser.ContextualDeserializer}
* are implemented, resolution of deserializers occurs before
* contextualization.
*/
public abstract class JsonDeserializer
{
/*
/**********************************************************
/* Main deserialization methods
/**********************************************************
*/
/**
* Method that can be called to ask implementation to deserialize
* JSON content into the value type this serializer handles.
* Returned instance is to be constructed by method itself.
*
* Pre-condition for this method is that the parser points to the
* first event that is part of value to deserializer (and which
* is never JSON 'null' literal, more on this below): for simple
* types it may be the only value; and for structured types the
* Object start marker or a FIELD_NAME.
*
*
* The two possible input conditions for structured types result
* from polymorphism via fields. In the ordinary case, Jackson
* calls this method when it has encountered an OBJECT_START,
* and the method implementation must advance to the next token to
* see the first field name. If the application configures
* polymorphism via a field, then the object looks like the following.
*
* {
* "@class": "class name",
* ...
* }
*
* Jackson consumes the two tokens (the @class field name
* and its value) in order to learn the class and select the deserializer.
* Thus, the stream is pointing to the FIELD_NAME for the first field
* after the @class. Thus, if you want your method to work correctly
* both with and without polymorphism, you must begin your method with:
*
* if (jp.getCurrentToken() == JsonToken.START_OBJECT) {
* jp.nextToken();
* }
*
* This results in the stream pointing to the field name, so that
* the two conditions align.
*
*
* Post-condition is that the parser will point to the last
* event that is part of deserialized value (or in case deserialization
* fails, event that was not recognized or usable, which may be
* the same event as the one it pointed to upon call).
*
* Note that this method is never called for JSON null literal,
* and thus deserializers need (and should) not check for it.
*
* @param jp Parsed used for reading JSON content
* @param ctxt Context that can be used to access information about
* this deserialization activity.
*
* @return Deserialized value
*/
public abstract T deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException;
/**
* Alternate deserialization method (compared to the most commonly
* used, {@link #deserialize(JsonParser, DeserializationContext)}),
* which takes in initialized value instance, to be
* configured and/or populated by deserializer.
* Method is not necessarily used (or supported) by all types
* (it will not work for immutable types, for obvious reasons):
* most commonly it is used for Collections and Maps.
* It may be used both with "updating readers" (for POJOs) and
* when Collections and Maps use "getter as setter".
*
* Default implementation just throws
* {@link UnsupportedOperationException}, to indicate that types
* that do not explicitly add support do not necessarily support
* update-existing-value operation (esp. immutable types)
*/
public T deserialize(JsonParser jp, DeserializationContext ctxt,
T intoValue)
throws IOException, JsonProcessingException
{
throw new UnsupportedOperationException("Can not update object of type "
+intoValue.getClass().getName()+" (by deserializer of type "+getClass().getName()+")");
}
/**
* Deserialization called when type being deserialized is defined to
* contain additional type identifier, to allow for correctly
* instantiating correct subtype. This can be due to annotation on
* type (or its supertype), or due to global settings without
* annotations.
*
* Default implementation may work for some types, but ideally subclasses
* should not rely on current default implementation.
* Implementation is mostly provided to avoid compilation errors with older
* code.
*
* @param typeDeserializer Deserializer to use for handling type information
*/
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException, JsonProcessingException
{
// We could try calling
return typeDeserializer.deserializeTypedFromAny(jp, ctxt);
}
/*
/**********************************************************
/* Fluent factory methods for constructing decorated versions
/**********************************************************
*/
/**
* Method that will return deserializer instance that is able
* to handle "unwrapped" value instances
* If no unwrapped instance can be constructed, will simply
* return this object as-is.
*
* Default implementation just returns 'this'
* indicating that no unwrapped variant exists
*/
public JsonDeserializer unwrappingDeserializer(NameTransformer unwrapper) {
return this;
}
/**
* Method that can be called to try to replace deserializer this deserializer
* delegates calls to. If not supported (either this deserializer does not
* delegate anything; or it does not want any changes), should either
* throw {@link UnsupportedOperationException} (if operation does not
* make sense or is not allowed); or return this deserializer as is.
*
* @since 2.1
*/
public JsonDeserializer> replaceDelegatee(JsonDeserializer> delegatee) {
throw new UnsupportedOperationException();
}
/*
/**********************************************************
/* Other accessors
/**********************************************************
*/
/**
* Method for accessing type of values this deserializer produces.
* Note that this information is not guaranteed to be exact -- it
* may be a more generic (super-type) -- but it should not be
* incorrect (return a non-related type).
*
* Default implementation will return null, which means almost same
* same as returning Object.class
would; that is, that
* nothing is known about handled type.
*
* @since 2.3
*/
public Class> handledType() { return null; }
/**
* Method that can be called to determine value to be used for
* representing null values (values deserialized when JSON token
* is {@link JsonToken#VALUE_NULL}). Usually this is simply
* Java null, but for some types (especially primitives) it may be
* necessary to use non-null values.
*
* Note that deserializers are allowed to call this just once and
* then reuse returned value; that is, method is not guaranteed to
* be called once for each conversion.
*
* Default implementation simply returns null.
*/
public T getNullValue() { return null; }
/**
* Method called to determine value to be used for "empty" values
* (most commonly when deserializing from empty JSON Strings).
* Usually this is same as {@link #getNullValue} (which in turn
* is usually simply Java null), but it can be overridden
* for types. Or, if type should never be converted from empty
* String, method can also throw an exception.
*
* Default implementation simple calls {@link #getNullValue} and
* returns value.
*/
public T getEmptyValue() { return getNullValue(); }
/**
* Method that will
* either return null to indicate that type being deserializers
* has no concept of properties; or a collection of identifiers
* for which toString
will give external property
* name.
* This is only to be used for error reporting and diagnostics
* purposes (most commonly, to accompany "unknown property"
* exception).
*
* @since 2.0
*/
public Collection getKnownPropertyNames() {
return null;
}
/**
* Method called to see if deserializer instance is cachable and
* usable for other properties of same type (type for which instance
* was created).
*
* Note that cached instances are still resolved on per-property basis,
* if instance implements {@link com.fasterxml.jackson.databind.deser.ResolvableDeserializer}:
* cached instance is just as the base. This means that in most cases it is safe to
* cache instances; however, it only makes sense to cache instances
* if instantiation is expensive, or if instances are heavy-weight.
*
* Default implementation returns false, to indicate that no caching
* is done.
*/
public boolean isCachable() { return false; }
/**
* Accessor that can be used to check whether this deserializer
* is expecting to possibly get an Object Identifier value instead of full value
* serialization, and if so, should be able to resolve it to actual
* Object instance to return as deserialized value.
*
* Default implementation returns null, as support can not be implemented
* generically. Some standard deserializers (most notably
* {@link com.fasterxml.jackson.databind.deser.BeanDeserializer})
* do implement this feature, and may return reader instance, depending on exact
* configuration of instance (which is based on type, and referring property).
*
* @return ObjectIdReader used for resolving possible Object Identifier
* value, instead of full value serialization, if deserializer can do that;
* null if no Object Id is expected.
*
* @since 2.0
*/
public ObjectIdReader getObjectIdReader() { return null; }
/**
* Accessor that can be used to determine if this deserializer uses
* another deserializer for actual deserialization, by delegating
* calls. If so, will return immediate delegate (which itself may
* delegate to further deserializers); otherwise will return null.
*
* @return Deserializer this deserializer delegates calls to, if null;
* null otherwise.
*
* @since 2.1
*/
public JsonDeserializer> getDelegatee() {
return null;
}
/**
* Method needed by {@link BeanDeserializerFactory} to properly link
* managed- and back-reference pairs.
*
* @since 2.2 (was moved out of BeanDeserializerBase
)
*/
public SettableBeanProperty findBackReference(String refName)
{
throw new IllegalArgumentException("Can not handle managed/back reference '"+refName
+"': type: value deserializer of type "+getClass().getName()+" does not support them");
}
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/
/**
* This marker class is only to be used with annotations, to
* indicate that no deserializer is configured .
*
* Specifically, this class is to be used as the marker for
* annotation {@link com.fasterxml.jackson.databind.annotation.JsonDeserialize}
*/
public abstract static class None extends JsonDeserializer {
private None() { } // not to be instantiated
}
}
JsonMappingException.java 0000664 0000000 0000000 00000026122 12373034547 0035425 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import java.io.Serializable;
import java.util.*;
import com.fasterxml.jackson.core.*;
/**
* Checked exception used to signal fatal problems with mapping of
* content.
*
* One additional feature is the ability to denote relevant path
* of references (during serialization/deserialization) to help in
* troubleshooting.
*/
public class JsonMappingException
extends JsonProcessingException
{
private static final long serialVersionUID = 1L;
/**
* Let's limit length of reference chain, to limit damage in cases
* of infinite recursion.
*/
final static int MAX_REFS_TO_LIST = 1000;
/*
/**********************************************************
/* Helper classes
/**********************************************************
*/
/**
* Simple bean class used to contain references. References
* can be added to indicate execution/reference path that
* lead to the problem that caused this exception to be
* thrown.
*/
public static class Reference implements Serializable
{
private static final long serialVersionUID = 1L;
/**
* Object through which reference was resolved. Can be either
* actual instance (usually the case for serialization), or
* Class (usually the case for deserialization).
*/
protected Object _from;
/**
* Name of field (for beans) or key (for Maps) that is part
* of the reference. May be null for Collection types (which
* generally have {@link #_index} defined), or when resolving
* Map classes without (yet) having an instance to operate on.
*/
protected String _fieldName;
/**
* Index within a {@link Collection} instance that contained
* the reference; used if index is relevant and available.
* If either not applicable, or not available, -1 is used to
* denote "not known".
*/
protected int _index = -1;
/**
* Default constructor for deserialization/sub-classing purposes
*/
protected Reference() { }
public Reference(Object from) { _from = from; }
public Reference(Object from, String fieldName) {
_from = from;
if (fieldName == null) {
throw new NullPointerException("Can not pass null fieldName");
}
_fieldName = fieldName;
}
public Reference(Object from, int index) {
_from = from;
_index = index;
}
public void setFrom(Object o) { _from = o; }
public void setFieldName(String n) { _fieldName = n; }
public void setIndex(int ix) { _index = ix; }
public Object getFrom() { return _from; }
public String getFieldName() { return _fieldName; }
public int getIndex() { return _index; }
@Override public String toString() {
StringBuilder sb = new StringBuilder();
Class> cls = (_from instanceof Class>) ?
((Class>)_from) : _from.getClass();
/* Hmmh. Although Class.getName() is mostly ok, it does look
* butt-ugly for arrays. So let's use getSimpleName() instead;
* but have to prepend package name too.
*/
Package pkg = cls.getPackage();
if (pkg != null) {
sb.append(pkg.getName());
sb.append('.');
}
sb.append(cls.getSimpleName());
sb.append('[');
if (_fieldName != null) {
sb.append('"');
sb.append(_fieldName);
sb.append('"');
} else if (_index >= 0) {
sb.append(_index);
} else {
sb.append('?');
}
sb.append(']');
return sb.toString();
}
}
/*
/**********************************************************
/* State/configuration
/**********************************************************
*/
/**
* Path through which problem that triggering throwing of
* this exception was reached.
*/
protected LinkedList _path;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public JsonMappingException(String msg) { super(msg); }
public JsonMappingException(String msg, Throwable rootCause) { super(msg, rootCause); }
public JsonMappingException(String msg, JsonLocation loc) { super(msg, loc); }
public JsonMappingException(String msg, JsonLocation loc, Throwable rootCause) { super(msg, loc, rootCause); }
public static JsonMappingException from(JsonParser jp, String msg) {
return new JsonMappingException(msg, ((jp == null) ? null : jp.getTokenLocation()));
}
public static JsonMappingException from(JsonParser jp, String msg, Throwable problem) {
return new JsonMappingException(msg, ((jp == null) ? null : jp.getTokenLocation()), problem);
}
/**
* Factory method used when "upgrading" an {@link IOException} into
* {@link JsonMappingException}: usually only needed to comply with
* a signature.
*
* @since 2.1
*/
public static JsonMappingException fromUnexpectedIOE(IOException src) {
return new JsonMappingException("Unexpected IOException (of type "
+src.getClass().getName()+"): "+src.getMessage(), (JsonLocation)null, src);
}
/**
* Method that can be called to either create a new JsonMappingException
* (if underlying exception is not a JsonMappingException), or augment
* given exception with given path/reference information.
*
* This version of method is called when the reference is through a
* non-indexed object, such as a Map or POJO/bean.
*/
public static JsonMappingException wrapWithPath(Throwable src, Object refFrom,
String refFieldName) {
return wrapWithPath(src, new Reference(refFrom, refFieldName));
}
/**
* Method that can be called to either create a new JsonMappingException
* (if underlying exception is not a JsonMappingException), or augment
* given exception with given path/reference information.
*
* This version of method is called when the reference is through an
* index, which happens with arrays and Collections.
*/
public static JsonMappingException wrapWithPath(Throwable src, Object refFrom, int index) {
return wrapWithPath(src, new Reference(refFrom, index));
}
/**
* Method that can be called to either create a new JsonMappingException
* (if underlying exception is not a JsonMappingException), or augment
* given exception with given path/reference information.
*/
public static JsonMappingException wrapWithPath(Throwable src, Reference ref)
{
JsonMappingException jme;
if (src instanceof JsonMappingException) {
jme = (JsonMappingException) src;
} else {
String msg = src.getMessage();
/* Related to [JACKSON-62], let's use a more meaningful placeholder
* if all we have is null
*/
if (msg == null || msg.length() == 0) {
msg = "(was "+src.getClass().getName()+")";
}
jme = new JsonMappingException(msg, null, src);
}
jme.prependPath(ref);
return jme;
}
/*
/**********************************************************
/* Accessors/mutators
/**********************************************************
*/
/**
* Method for accessing full structural path within type hierarchy
* down to problematic property.
*/
public List getPath()
{
if (_path == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(_path);
}
/**
* Method for accesing description of path that lead to the
* problem that triggered this exception
*/
public String getPathReference()
{
return getPathReference(new StringBuilder()).toString();
}
public StringBuilder getPathReference(StringBuilder sb)
{
_appendPathDesc(sb);
return sb;
}
/**
* Method called to prepend a reference information in front of
* current path
*/
public void prependPath(Object referrer, String fieldName)
{
Reference ref = new Reference(referrer, fieldName);
prependPath(ref);
}
/**
* Method called to prepend a reference information in front of
* current path
*/
public void prependPath(Object referrer, int index)
{
Reference ref = new Reference(referrer, index);
prependPath(ref);
}
public void prependPath(Reference r)
{
if (_path == null) {
_path = new LinkedList();
}
/* Also: let's not increase without bounds. Could choose either
* head or tail; tail is easier (no need to ever remove), as
* well as potentially more useful so let's use it:
*/
if (_path.size() < MAX_REFS_TO_LIST) {
_path.addFirst(r);
}
}
/*
/**********************************************************
/* Overridden methods
/**********************************************************
*/
@Override
public String getLocalizedMessage() {
return _buildMessage();
}
/**
* Method is overridden so that we can properly inject description
* of problem path, if such is defined.
*/
@Override
public String getMessage() {
return _buildMessage();
}
protected String _buildMessage()
{
/* First: if we have no path info, let's just use parent's
* definition as is
*/
String msg = super.getMessage();
if (_path == null) {
return msg;
}
StringBuilder sb = (msg == null) ? new StringBuilder() : new StringBuilder(msg);
/* 18-Feb-2009, tatu: initially there was a linefeed between
* message and path reference; but unfortunately many systems
* (loggers, junit) seem to assume linefeeds are only added to
* separate stack trace.
*/
sb.append(" (through reference chain: ");
sb = getPathReference(sb);
sb.append(')');
return sb.toString();
}
@Override
public String toString()
{
return getClass().getName()+": "+getMessage();
}
/*
/**********************************************************
/* Internal methods
/**********************************************************
*/
protected void _appendPathDesc(StringBuilder sb)
{
if (_path == null) {
return;
}
Iterator it = _path.iterator();
while (it.hasNext()) {
sb.append(it.next().toString());
if (it.hasNext()) {
sb.append("->");
}
}
}
}
jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind/JsonNode.java 0000664 0000000 0000000 00000100165 12373034547 0033117 0 ustar 00root root 0000000 0000000 package com.fasterxml.jackson.databind;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import com.fasterxml.jackson.core.JsonPointer;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.MissingNode;
import com.fasterxml.jackson.databind.util.EmptyIterator;
/**
* Base class for all JSON nodes, which form the basis of JSON
* Tree Model that Jackson implements.
* One way to think of these nodes is to consider them
* similar to DOM nodes in XML DOM trees.
*
* As a general design rule, most accessors ("getters") are included
* in this base class, to allow for traversing structure without
* type casts. Most mutators, however, need to be accessed through
* specific sub-classes (such as ObjectNode
* and ArrayNode
).
* This seems sensible because proper type
* information is generally available when building or modifying
* trees, but less often when reading a tree (newly built from
* parsed JSON content).
*
* Actual concrete sub-classes can be found from package
* {@link com.fasterxml.jackson.databind.node}.
*/
public abstract class JsonNode
implements TreeNode, Iterable
{
/*
/**********************************************************
/* Construction, related
/**********************************************************
*/
protected JsonNode() { }
/**
* Method that can be called to get a node that is guaranteed
* not to allow changing of this node through mutators on
* this node or any of its children.
* This means it can either make a copy of this node (and all
* mutable children and grand children nodes), or node itself
* if it is immutable.
*
* Note: return type is guaranteed to have same type as the
* node method is called on; which is why method is declared
* with local generic type.
*
* @since 2.0
*
* @return Node that is either a copy of this node (and all non-leaf
* children); or, for immutable leaf nodes, node itself.
*/
public abstract T deepCopy();
/*
/**********************************************************
/* TreeNode implementation
/**********************************************************
*/
// public abstract JsonToken asToken();
// public abstract JsonParser.NumberType numberType();
// public abstract JsonParser traverse();
@Override
public int size() { return 0; }
@Override
public final boolean isValueNode()
{
switch (getNodeType()) {
case ARRAY: case OBJECT: case MISSING:
return false;
default:
return true;
}
}
@Override
public final boolean isContainerNode() {
final JsonNodeType type = getNodeType();
return type == JsonNodeType.OBJECT || type == JsonNodeType.ARRAY;
}
@Override
public final boolean isMissingNode() {
return getNodeType() == JsonNodeType.MISSING;
}
@Override
public final boolean isArray() {
return getNodeType() == JsonNodeType.ARRAY;
}
@Override
public final boolean isObject() {
return getNodeType() == JsonNodeType.OBJECT;
}
/**
* Method for accessing value of the specified element of
* an array node. For other nodes, null is always returned.
*
* For array nodes, index specifies
* exact location within array and allows for efficient iteration
* over child elements (underlying storage is guaranteed to
* be efficiently indexable, i.e. has random-access to elements).
* If index is less than 0, or equal-or-greater than
* node.size()
, null is returned; no exception is
* thrown for any index.
*
* NOTE: if the element value has been explicitly set as null
* (which is different from removal!),
* a {@link com.fasterxml.jackson.databind.node.NullNode} will be returned,
* not null.
*
* @return Node that represent value of the specified element,
* if this node is an array and has specified element.
* Null otherwise.
*/
@Override
public abstract JsonNode get(int index);
/**
* Method for accessing value of the specified field of
* an object node. If this node is not an object (or it
* does not have a value for specified field name), or
* if there is no field with such name, null is returned.
*
* NOTE: if the property value has been explicitly set as null
* (which is different from removal!),
* a {@link com.fasterxml.jackson.databind.node.NullNode} will be returned,
* not null.
*
* @return Node that represent value of the specified field,
* if this node is an object and has value for the specified
* field. Null otherwise.
*/
@Override
public JsonNode get(String fieldName) { return null; }
/**
* This method is similar to {@link #get(String)}, except
* that instead of returning null if no such value exists (due
* to this node not being an object, or object not having value
* for the specified field),
* a "missing node" (node that returns true for
* {@link #isMissingNode}) will be returned. This allows for
* convenient and safe chained access via path calls.
*/
@Override
public abstract JsonNode path(String fieldName);
/**
* This method is similar to {@link #get(int)}, except
* that instead of returning null if no such element exists (due
* to index being out of range, or this node not being an array),
* a "missing node" (node that returns true for
* {@link #isMissingNode}) will be returned. This allows for
* convenient and safe chained access via path calls.
*/
@Override
public abstract JsonNode path(int index);
@Override
public Iterator fieldNames() {
return EmptyIterator.instance();
}
/**
* Method for locating node specified by given JSON pointer instances.
* Method will never return null; if no matching node exists,
* will return a node for which {@link #isMissingNode()} returns true.
*
* @return Node that matches given JSON Pointer: if no match exists,
* will return a node for which {@link #isMissingNode()} returns true.
*
* @since 2.3
*/
@Override
public final JsonNode at(JsonPointer ptr)
{
// Basically: value nodes only match if we have "empty" path left
if (ptr.matches()) {
return this;
}
JsonNode n = _at(ptr);
if (n == null) {
return MissingNode.getInstance();
}
return n.at(ptr.tail());
}
/**
* Convenience method that is functionally equivalent to:
*
* return at(JsonPointer.valueOf(jsonPointerExpression));
*
*
* Note that if the same expression is used often, it is preferable to construct
* {@link JsonPointer} instance once and reuse it: this method will not perform
* any caching of compiled expressions.
*
* @param jsonPtrExpr Expression to compile as a {@link JsonPointer}
* instance
*
* @return Node that matches given JSON Pointer: if no match exists,
* will return a node for which {@link TreeNode#isMissingNode()} returns true.
*
* @since 2.3
*/
@Override
public final JsonNode at(String jsonPtrExpr) {
return at(JsonPointer.compile(jsonPtrExpr));
}
protected abstract JsonNode _at(JsonPointer ptr);
/*
/**********************************************************
/* Public API, type introspection
/**********************************************************
*/
// // First high-level division between values, containers and "missing"
/**
* Return the type of this node
*
* @return the node type as a {@link JsonNodeType} enum value
*
* @since 2.2
*/
public abstract JsonNodeType getNodeType();
/**
* Method that can be used to check if the node is a wrapper
* for a POJO ("Plain Old Java Object" aka "bean".
* Returns true only for
* instances of POJONode
.
*
* @return True if this node wraps a POJO
*/
public final boolean isPojo() {
return getNodeType() == JsonNodeType.POJO;
}
/**
* @return True if this node represents a numeric JSON value
*/
public final boolean isNumber() {
return getNodeType() == JsonNodeType.NUMBER;
}
/**
*
* @return True if this node represents an integral (integer)
* numeric JSON value
*/
public boolean isIntegralNumber() { return false; }
/**
* @return True if this node represents a non-integral
* numeric JSON value
*/
public boolean isFloatingPointNumber() { return false; }
/**
* Method that can be used to check whether contained value
* is a number represented as Java short
.
* Note, however, that even if this method returns false, it
* is possible that conversion would be possible from other numeric
* types -- to check if this is possible, use
* {@link #canConvertToInt()} instead.
*
* @return True if the value contained by this node is stored as Java short
*/
public boolean isShort() { return false; }
/**
* Method that can be used to check whether contained value
* is a number represented as Java int
.
* Note, however, that even if this method returns false, it
* is possible that conversion would be possible from other numeric
* types -- to check if this is possible, use
* {@link #canConvertToInt()} instead.
*
* @return True if the value contained by this node is stored as Java int
*/
public boolean isInt() { return false; }
/**
* Method that can be used to check whether contained value
* is a number represented as Java long
.
* Note, however, that even if this method returns false, it
* is possible that conversion would be possible from other numeric
* types -- to check if this is possible, use
* {@link #canConvertToInt()} instead.
*
* @return True if the value contained by this node is stored as Java long
*/
public boolean isLong() { return false; }
/**
* @since 2.2
*/
public boolean isFloat() { return false; }
public boolean isDouble() { return false; }
public boolean isBigDecimal() { return false; }
public boolean isBigInteger() { return false; }
/**
* Method that checks whether this node represents basic JSON String
* value.
*/
public final boolean isTextual() {
return getNodeType() == JsonNodeType.STRING;
}
/**
* Method that can be used to check if this node was created from
* JSON boolean value (literals "true" and "false").
*/
public final boolean isBoolean() {
return getNodeType() == JsonNodeType.BOOLEAN;
}
/**
* Method that can be used to check if this node was created from
* JSON literal null value.
*/
public final boolean isNull() {
return getNodeType() == JsonNodeType.NULL;
}
/**
* Method that can be used to check if this node represents
* binary data (Base64 encoded). Although this will be externally
* written as JSON String value, {@link #isTextual} will
* return false if this method returns true.
*
* @return True if this node represents base64 encoded binary data
*/
public final boolean isBinary() {
return getNodeType() == JsonNodeType.BINARY;
}
/**
* Method that can be used to check whether this node is a numeric
* node ({@link #isNumber} would return true) AND its value fits
* within Java's 32-bit signed integer type, int
.
* Note that floating-point numbers are convertible if the integral
* part fits without overflow (as per standard Java coercion rules)
*
* NOTE: this method does not consider possible value type conversion
* from JSON String into Number; so even if this method returns false,
* it is possible that {@link #asInt} could still succeed
* if node is a JSON String representing integral number, or boolean.
*
* @since 2.0
*/
public boolean canConvertToInt() { return false; }
/**
* Method that can be used to check whether this node is a numeric
* node ({@link #isNumber} would return true) AND its value fits
* within Java's 64-bit signed integer type, long
.
* Note that floating-point numbers are convertible if the integral
* part fits without overflow (as per standard Java coercion rules)
*
* NOTE: this method does not consider possible value type conversion
* from JSON String into Number; so even if this method returns false,
* it is possible that {@link #asLong} could still succeed
* if node is a JSON String representing integral number, or boolean.
*
* @since 2.0
*/
public boolean canConvertToLong() { return false; }
/*
/**********************************************************
/* Public API, straight value access
/**********************************************************
*/
/**
* Method to use for accessing String values.
* Does NOT do any conversions for non-String value nodes;
* for non-String values (ones for which {@link #isTextual} returns
* false) null will be returned.
* For String values, null is never returned (but empty Strings may be)
*
* @return Textual value this node contains, iff it is a textual
* JSON node (comes from JSON String value entry)
*/
public String textValue() { return null; }
/**
* Method to use for accessing binary content of binary nodes (nodes
* for which {@link #isBinary} returns true); or for Text Nodes
* (ones for which {@link #textValue} returns non-null value),
* to read decoded base64 data.
* For other types of nodes, returns null.
*
* @return Binary data this node contains, iff it is a binary
* node; null otherwise
*/
public byte[] binaryValue() throws IOException {
return null;
}
/**
* Method to use for accessing JSON boolean values (value
* literals 'true' and 'false').
* For other types, always returns false.
*
* @return Textual value this node contains, iff it is a textual
* json node (comes from JSON String value entry)
*/
public boolean booleanValue() { return false; }
/**
* Returns numeric value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true); otherwise
* returns null
*
* @return Number value this node contains, if any (null for non-number
* nodes).
*/
public Number numberValue() { return null; }
/**
* Returns 16-bit short value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true). For other
* types returns 0.
* For floating-point numbers, value is truncated using default
* Java coercion, similar to how cast from double to short operates.
*
* @return Short value this node contains, if any; 0 for non-number
* nodes.
*/
public short shortValue() { return 0; }
/**
* Returns integer value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true). For other
* types returns 0.
* For floating-point numbers, value is truncated using default
* Java coercion, similar to how cast from double to int operates.
*
* @return Integer value this node contains, if any; 0 for non-number
* nodes.
*/
public int intValue() { return 0; }
/**
* Returns 64-bit long value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true). For other
* types returns 0.
* For floating-point numbers, value is truncated using default
* Java coercion, similar to how cast from double to long operates.
*
* @return Long value this node contains, if any; 0 for non-number
* nodes.
*/
public long longValue() { return 0L; }
/**
* Returns 32-bit floating value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true). For other
* types returns 0.0.
* For integer values, conversion is done using coercion; this means
* that an overflow is possible for `long` values
*
* @return 32-bit float value this node contains, if any; 0.0 for non-number nodes.
*
* @since 2.2
*/
public float floatValue() { return 0.0f; }
/**
* Returns 64-bit floating point (double) value for this node, if and only if
* this node is numeric ({@link #isNumber} returns true). For other
* types returns 0.0.
* For integer values, conversion is done using coercion; this may result
* in overflows with {@link BigInteger} values.
*
* @return 64-bit double value this node contains, if any; 0.0 for non-number nodes.
*
* @since 2.2
*/
public double doubleValue() { return 0.0; }
public BigDecimal decimalValue() { return BigDecimal.ZERO; }
public BigInteger bigIntegerValue() { return BigInteger.ZERO; }
/*
/**********************************************************
/* Public API, value access with conversion(s)/coercion(s)
/**********************************************************
*/
/**
* Method that will return a valid String representation of
* the container value, if the node is a value node
* (method {@link #isValueNode} returns true),
* otherwise empty String.
*/
public abstract String asText();
/**
* Method similar to {@link #asText()}, except that it will return
* defaultValue
in cases where null value would be returned;
* either for missing nodes (trying to access missing property, or element
* at invalid item for array) or explicit nulls.
*
* @since 2.4
*/
public String asText(String defaultValue) {
String str = asText();
return (str == null) ? defaultValue : str;
}
/**
* Method that will try to convert value of this node to a Java int .
* Numbers are coerced using default Java rules; booleans convert to 0 (false)
* and 1 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an int (including structured types
* like Objects and Arrays),
* default value of 0 will be returned; no exceptions are thrown.
*/
public int asInt() {
return asInt(0);
}
/**
* Method that will try to convert value of this node to a Java int .
* Numbers are coerced using default Java rules; booleans convert to 0 (false)
* and 1 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an int (including structured types
* like Objects and Arrays),
* specified defaultValue will be returned; no exceptions are thrown.
*/
public int asInt(int defaultValue) {
return defaultValue;
}
/**
* Method that will try to convert value of this node to a Java long .
* Numbers are coerced using default Java rules; booleans convert to 0 (false)
* and 1 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an long (including structured types
* like Objects and Arrays),
* default value of 0 will be returned; no exceptions are thrown.
*/
public long asLong() {
return asLong(0L);
}
/**
* Method that will try to convert value of this node to a Java long .
* Numbers are coerced using default Java rules; booleans convert to 0 (false)
* and 1 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an long (including structured types
* like Objects and Arrays),
* specified defaultValue will be returned; no exceptions are thrown.
*/
public long asLong(long defaultValue) {
return defaultValue;
}
/**
* Method that will try to convert value of this node to a Java double .
* Numbers are coerced using default Java rules; booleans convert to 0.0 (false)
* and 1.0 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an int (including structured types
* like Objects and Arrays),
* default value of 0.0 will be returned; no exceptions are thrown.
*/
public double asDouble() {
return asDouble(0.0);
}
/**
* Method that will try to convert value of this node to a Java double .
* Numbers are coerced using default Java rules; booleans convert to 0.0 (false)
* and 1.0 (true), and Strings are parsed using default Java language integer
* parsing rules.
*
* If representation can not be converted to an int (including structured types
* like Objects and Arrays),
* specified defaultValue will be returned; no exceptions are thrown.
*/
public double asDouble(double defaultValue) {
return defaultValue;
}
/**
* Method that will try to convert value of this node to a Java boolean .
* JSON booleans map naturally; integer numbers other than 0 map to true, and
* 0 maps to false
* and Strings 'true' and 'false' map to corresponding values.
*
* If representation can not be converted to a boolean value (including structured types
* like Objects and Arrays),
* default value of false will be returned; no exceptions are thrown.
*/
public boolean asBoolean() {
return asBoolean(false);
}
/**
* Method that will try to convert value of this node to a Java boolean .
* JSON booleans map naturally; integer numbers other than 0 map to true, and
* 0 maps to false
* and Strings 'true' and 'false' map to corresponding values.
*
* If representation can not be converted to a boolean value (including structured types
* like Objects and Arrays),
* specified defaultValue will be returned; no exceptions are thrown.
*/
public boolean asBoolean(boolean defaultValue) {
return defaultValue;
}
/*
/**********************************************************
/* Public API, value find / existence check methods
/**********************************************************
*/
/**
* Method that allows checking whether this node is JSON Object node
* and contains value for specified property. If this is the case
* (including properties with explicit null values), returns true;
* otherwise returns false.
*
* This method is equivalent to:
*
* node.get(fieldName) != null
*
* (since return value of get() is node, not value node contains)
*
* NOTE: when explicit null
values are added, this
* method will return true
for such properties.
*
* @param fieldName Name of element to check
*
* @return True if this node is a JSON Object node, and has a property
* entry with specified name (with any value, including null value)
*/
public boolean has(String fieldName) {
return get(fieldName) != null;
}
/**
* Method that allows checking whether this node is JSON Array node
* and contains a value for specified index
* If this is the case
* (including case of specified indexing having null as value), returns true;
* otherwise returns false.
*
* Note: array element indexes are 0-based.
*
* This method is equivalent to:
*
* node.get(index) != null
*
*
* NOTE: this method will return true
for explicitly added
* null values.
*
* @param index Index to check
*
* @return True if this node is a JSON Object node, and has a property
* entry with specified name (with any value, including null value)
*/
public boolean has(int index) {
return get(index) != null;
}
/**
* Method that is similar to {@link #has(String)}, but that will
* return false
for explicitly added nulls.
*
* This method is functionally equivalent to:
*
* node.get(fieldName) != null && !node.get(fieldName).isNull()
*
*
* @since 2.1
*/
public boolean hasNonNull(String fieldName) {
JsonNode n = get(fieldName);
return (n != null) && !n.isNull();
}
/**
* Method that is similar to {@link #has(int)}, but that will
* return false
for explicitly added nulls.
*
* This method is equivalent to:
*
* node.get(index) != null && !node.get(index).isNull()
*
*
* @since 2.1
*/
public boolean hasNonNull(int index) {
JsonNode n = get(index);
return (n != null) && !n.isNull();
}
/*
/**********************************************************
/* Public API, container access
/**********************************************************
*/
/**
* Same as calling {@link #elements}; implemented so that
* convenience "for-each" loop can be used for looping over elements
* of JSON Array constructs.
*/
@Override
public final Iterator iterator() { return elements(); }
/**
* Method for accessing all value nodes of this Node, iff
* this node is a JSON Array or Object node. In case of Object node,
* field names (keys) are not included, only values.
* For other types of nodes, returns empty iterator.
*/
public Iterator elements() {
return EmptyIterator.instance();
}
/**
* @return Iterator that can be used to traverse all key/value pairs for
* object nodes; empty iterator (no contents) for other types
*/
public Iterator> fields() {
return EmptyIterator.instance();
}
/*
/**********************************************************
/* Public API, find methods
/**********************************************************
*/
/**
* Method for finding a JSON Object field with specified name in this
* node or its child nodes, and returning value it has.
* If no matching field is found in this node or its descendants, returns null.
*
* @param fieldName Name of field to look for
*
* @return Value of first matching node found, if any; null if none
*/
public abstract JsonNode findValue(String fieldName);
/**
* Method for finding JSON Object fields with specified name, and returning
* found ones as a List. Note that sub-tree search ends if a field is found,
* so possible children of result nodes are not included.
* If no matching fields are found in this node or its descendants, returns
* an empty List.
*
* @param fieldName Name of field to look for
*/
public final List findValues(String fieldName)
{
List result = findValues(fieldName, null);
if (result == null) {
return Collections.emptyList();
}
return result;
}
/**
* Similar to {@link #findValues}, but will additionally convert
* values into Strings, calling {@link #asText}.
*/
public final List findValuesAsText(String fieldName)
{
List result = findValuesAsText(fieldName, null);
if (result == null) {
return Collections.emptyList();
}
return result;
}
/**
* Method similar to {@link #findValue}, but that will return a
* "missing node" instead of null if no field is found. Missing node
* is a specific kind of node for which {@link #isMissingNode}
* returns true; and all value access methods return empty or
* missing value.
*
* @param fieldName Name of field to look for
*
* @return Value of first matching node found; or if not found, a
* "missing node" (non-null instance that has no value)
*/
public abstract JsonNode findPath(String fieldName);
/**
* Method for finding a JSON Object that contains specified field,
* within this node or its descendants.
* If no matching field is found in this node or its descendants, returns null.
*
* @param fieldName Name of field to look for
*
* @return Value of first matching node found, if any; null if none
*/
public abstract JsonNode findParent(String fieldName);
/**
* Method for finding a JSON Object that contains specified field,
* within this node or its descendants.
* If no matching field is found in this node or its descendants, returns null.
*
* @param fieldName Name of field to look for
*
* @return Value of first matching node found, if any; null if none
*/
public final List findParents(String fieldName)
{
List result = findParents(fieldName, null);
if (result == null) {
return Collections.emptyList();
}
return result;
}
public abstract List findValues(String fieldName, List foundSoFar);
public abstract List findValuesAsText(String fieldName, List foundSoFar);
public abstract List findParents(String fieldName, List foundSoFar);
/*
/**********************************************************
/* Public API, path handling
/**********************************************************
*/
/**
* Method that can be called on Object nodes, to access a property
* that has Object value; or if no such property exists, to create,
* add and return such Object node.
* If the node method is called on is not Object node,
* or if property exists and has value that is not Object node,
* {@link UnsupportedOperationException} is thrown
*/
public JsonNode with(String propertyName) {
throw new UnsupportedOperationException("JsonNode not of type ObjectNode (but "
+getClass().getName()+"), can not call with() on it");
}
/**
* Method that can be called on Object nodes, to access a property
* that has Array
value; or if no such property exists, to create,
* add and return such Array node.
* If the node method is called on is not Object node,
* or if property exists and has value that is not Array node,
* {@link UnsupportedOperationException} is thrown
*/
public JsonNode withArray(String propertyName) {
throw new UnsupportedOperationException("JsonNode not of type ObjectNode (but "
+getClass().getName()+"), can not call withArray() on it");
}
/*
/**********************************************************
/* Overridden standard methods
/**********************************************************
*/
/**
*
* Note: marked as abstract to ensure all implementation
* classes define it properly.
*/
@Override
public abstract String toString();
/**
* Equality for node objects is defined as full (deep) value
* equality. This means that it is possible to compare complete
* JSON trees for equality by comparing equality of root nodes.
*
* Note: marked as abstract to ensure all implementation
* classes define it properly and not rely on definition
* from {@link java.lang.Object}.
*/
@Override
public abstract boolean equals(Object o);
}
JsonSerializable.java 0000664 0000000 0000000 00000004050 12373034547 0034555 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
/**
* Interface that can be implemented by objects that know how to
* serialize themselves to JSON, using {@link JsonGenerator}
* (and {@link SerializerProvider} if necessary).
*
* Note that implementing this interface binds implementing object
* closely to Jackson API, and that it is often not necessary to do
* so -- if class is a bean, it can be serialized without
* implementing this interface.
*
* NOTE: Jackson 2.0 added another method (from former "JsonSerializableWithType"),
* which is required for proper handling of case where additional type information
* is needed.
*/
public interface JsonSerializable
{
/**
* Serialization method called when no additional type information is
* to be included in serialization.
*/
public void serialize(JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException;
/**
* Serialization method called when additional type information is
* expected to be included in serialization, for deserialization to use.
*
* Usually implementation consists of a call to one of methods
* in {@link TypeSerializer} (such as {@link TypeSerializer#writeTypePrefixForObject(Object, JsonGenerator)})
* followed by serialization of contents,
* followed by another call to {@link TypeSerializer}
* (such as {@link TypeSerializer#writeTypeSuffixForObject(Object, JsonGenerator)}).
* Exact methods to call in {@link TypeSerializer} depend on shape of JSON Object used
* (Array, Object or scalar like String/Number/Boolean).
*
* Note that some types (most notably, "natural" types: String, Integer,
* Double and Boolean) never include type information.
*/
public void serializeWithType(JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException, JsonProcessingException;
}
JsonSerializer.java 0000664 0000000 0000000 00000022707 12373034547 0034271 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.util.NameTransformer;
/**
* Abstract class that defines API used by {@link ObjectMapper} (and
* other chained {@link JsonSerializer}s too) to serialize Objects of
* arbitrary types into JSON, using provided {@link JsonGenerator}.
* {@link com.fasterxml.jackson.databind.ser.std.StdSerializer} instead
* of this class, since it will implement many of optional
* methods of this class.
*
* NOTE: various serialize
methods are never (to be) called
* with null values -- caller must handle null values, usually
* by calling {@link SerializerProvider#findNullValueSerializer} to obtain
* serializer to use.
* This also means that custom serializers can not be directly used to change
* the output to produce when serializing null values.
*
* If serializer is an aggregate one -- meaning it delegates handling of some
* of its contents by using other serializer(s) -- it typically also needs
* to implement {@link com.fasterxml.jackson.databind.ser.ResolvableSerializer},
* which can locate secondary serializers needed. This is important to allow dynamic
* overrides of serializers; separate call interface is needed to separate
* resolution of secondary serializers (which may have cyclic link back
* to serializer itself, directly or indirectly).
*
* In addition, to support per-property annotations (to configure aspects
* of serialization on per-property basis), serializers may want
* to implement
* {@link com.fasterxml.jackson.databind.ser.ContextualSerializer},
* which allows specialization of serializers: call to
* {@link com.fasterxml.jackson.databind.ser.ContextualSerializer#createContextual}
* is passed information on property, and can create a newly configured
* serializer for handling that particular property.
*
* If both
* {@link com.fasterxml.jackson.databind.ser.ResolvableSerializer} and
* {@link com.fasterxml.jackson.databind.ser.ContextualSerializer}
* are implemented, resolution of serializers occurs before
* contextualization.
*/
public abstract class JsonSerializer
implements JsonFormatVisitable // since 2.1
{
/*
/**********************************************************
/* Fluent factory methods for constructing decorated versions
/**********************************************************
*/
/**
* Method that will return serializer instance that produces
* "unwrapped" serialization, if applicable for type being
* serialized (which is the case for some serializers
* that produce JSON Objects as output).
* If no unwrapped serializer can be constructed, will simply
* return serializer as-is.
*
* Default implementation just returns serializer as-is,
* indicating that no unwrapped variant exists
*
* @param unwrapper Name transformation to use to convert between names
* of unwrapper properties
*/
public JsonSerializer unwrappingSerializer(NameTransformer unwrapper) {
return this;
}
/**
* Method that can be called to try to replace serializer this serializer
* delegates calls to. If not supported (either this serializer does not
* delegate anything; or it does not want any changes), should either
* throw {@link UnsupportedOperationException} (if operation does not
* make sense or is not allowed); or return this serializer as is.
*
* @since 2.1
*/
public JsonSerializer replaceDelegatee(JsonSerializer> delegatee) {
throw new UnsupportedOperationException();
}
/*
/**********************************************************
/* Serialization methods
/**********************************************************
*/
/**
* Method that can be called to ask implementation to serialize
* values of type this serializer handles.
*
* @param value Value to serialize; can not be null.
* @param jgen Generator used to output resulting Json content
* @param provider Provider that can be used to get serializers for
* serializing Objects value contains, if any.
*/
public abstract void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException;
/**
* Method that can be called to ask implementation to serialize
* values of type this serializer handles, using specified type serializer
* for embedding necessary type information.
*
* Default implementation will throw {@link UnsupportedOperationException}
* to indicate that proper type handling needs to be implemented.
*
* For simple datatypes written as a single scalar value (JSON String, Number, Boolean),
* implementation would look like:
*
* // note: method to call depends on whether this type is serialized as JSON scalar, object or Array!
* typeSer.writeTypePrefixForScalar(value, jgen);
* serialize(value, jgen, provider);
* typeSer.writeTypeSuffixForScalar(value, jgen);
*
* and implementations for type serialized as JSON Arrays or Objects would differ slightly,
* as START-ARRAY>/END-ARRAY
and
* START-OBJECT>/END-OBJECT
pairs
* need to be properly handled with respect to serializing of contents.
*
* @param value Value to serialize; can not be null.
* @param jgen Generator used to output resulting Json content
* @param provider Provider that can be used to get serializers for
* serializing Objects value contains, if any.
* @param typeSer Type serializer to use for including type information
*/
public void serializeWithType(T value, JsonGenerator jgen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException, JsonProcessingException
{
Class> clz = handledType();
if (clz == null) {
clz = value.getClass();
}
throw new UnsupportedOperationException("Type id handling not implemented for type "+clz.getName());
}
/*
/**********************************************************
/* Other accessors
/**********************************************************
*/
/**
* Method for accessing type of Objects this serializer can handle.
* Note that this information is not guaranteed to be exact -- it
* may be a more generic (super-type) -- but it should not be
* incorrect (return a non-related type).
*
* Default implementation will return null, which essentially means
* same as returning Object.class
would; that is, that
* nothing is known about handled type.
*
*/
public Class handledType() { return null; }
/**
* Method called to check whether given serializable value is
* considered "empty" value (for purposes of suppressing serialization
* of empty values).
*
* Default implementation will consider only null values to be empty.
*
* @since 2.0
*/
public boolean isEmpty(T value) {
return (value == null);
}
/**
* Method that can be called to see whether this serializer instance
* will use Object Id to handle cyclic references.
*/
public boolean usesObjectId() {
return false;
}
/**
* Accessor for checking whether this serializer is an
* "unwrapping" serializer; this is necessary to know since
* it may also require caller to suppress writing of the
* leading property name.
*/
public boolean isUnwrappingSerializer() {
return false;
}
/**
* Accessor that can be used to determine if this serializer uses
* another serializer for actual serialization, by delegating
* calls. If so, will return immediate delegate (which itself may
* delegate to further serializers); otherwise will return null.
*
* @return Serializer this serializer delegates calls to, if null;
* null otherwise.
*
* @since 2.1
*/
public JsonSerializer> getDelegatee() {
return null;
}
/*
/**********************************************************
/* Default JsonFormatVisitable implementation
/**********************************************************
*/
/**
* Default implementation simply calls {@link JsonFormatVisitorWrapper#expectAnyFormat(JavaType)}.
*
* @since 2.1
*/
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type)
throws JsonMappingException
{
if (visitor != null) visitor.expectAnyFormat(type);
}
/*
/**********************************************************
/* Helper class(es)
/**********************************************************
*/
/**
* This marker class is only to be used with annotations, to
* indicate that no serializer is configured .
*
* Specifically, this class is to be used as the marker for
* annotation {@link com.fasterxml.jackson.databind.annotation.JsonSerialize}.
*/
public abstract static class None
extends JsonSerializer { }
}
KeyDeserializer.java 0000664 0000000 0000000 00000001734 12373034547 0034416 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
/**
* Abstract class that defines API used for deserializing JSON content
* field names into Java Map keys. These deserializers are only used
* if the Map key class is not String
or Object
.
*/
public abstract class KeyDeserializer
{
/**
* Method called to deserialize a {@link java.util.Map} key from JSON property name.
*/
public abstract Object deserializeKey(String key, DeserializationContext ctxt)
throws IOException, JsonProcessingException;
/**
* This marker class is only to be used with annotations, to
* indicate that no deserializer is configured .
*
* Specifically, this class is to be used as the marker for
* annotation {@link com.fasterxml.jackson.databind.annotation.JsonDeserialize}.
*/
public abstract static class None
extends KeyDeserializer { }
}
MapperFeature.java 0000664 0000000 0000000 00000025346 12373034547 0034070 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.cfg.ConfigFeature;
/**
* Enumeration that defines simple on/off features to set
* for {@link ObjectMapper}, and accessible (but not changeable)
* via {@link ObjectReader} and {@link ObjectWriter} (as well as
* through various convenience methods through context objects).
*
* Note that in addition to being only mutable via {@link ObjectMapper},
* changes only take effect when done before any serialization or
* deserialization calls -- that is, caller must follow
* "configure-then-use" pattern.
*/
public enum MapperFeature implements ConfigFeature
{
/*
/******************************************************
/* Introspection features
/******************************************************
*/
/**
* Feature that determines whether annotation introspection
* is used for configuration; if enabled, configured
* {@link AnnotationIntrospector} will be used: if disabled,
* no annotations are considered.
*
* Feature is enabled by default.
*/
USE_ANNOTATIONS(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.
*/
AUTO_DETECT_FIELDS(true),
/**
* Feature that determines whether regualr "getter" methods are
* automatically detected based on standard Bean naming convention
* or not. If yes, then all public zero-argument methods that
* start with prefix "get"
* are considered as getters.
* If disabled, only methods explicitly annotated are considered getters.
*
* Note that since version 1.3, this does NOT include
* "is getters" (see {@link #AUTO_DETECT_IS_GETTERS} for details)
*
* 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_GETTERS(true),
/**
* Feature that determines whether "is getter" methods are
* automatically detected based on standard Bean naming convention
* or not. If yes, then all public zero-argument methods that
* start with prefix "is", and whose return type is boolean
* are considered as "is getters".
* If disabled, only methods explicitly annotated are considered getters.
*
* 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_IS_GETTERS(true),
/**
* 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 getters (getter methods)
* can be auto-detected if there is no matching mutator (setter,
* constructor parameter or field) or not: if set to true,
* only getters that match a mutator are auto-discovered; if
* false, all auto-detectable getters can be discovered.
*
* Feature is disabled by default.
*/
REQUIRE_SETTERS_FOR_GETTERS(false),
/**
* 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.
*
* Feature is enabled by default.
*/
CAN_OVERRIDE_ACCESS_MODIFIERS(true),
/**
* Feature that determines whether member mutators (fields and
* setters) may be "pulled in" even if they are not visible,
* as long as there is a visible accessor (getter or field) with same name.
* For example: field "value" may be inferred as mutator,
* if there is visible or explicitly marked getter "getValue()".
* If enabled, inferring is enabled; otherwise (disabled) only visible and
* explicitly annotated accessors are ever used.
*
* Note that 'getters' are never inferred and need to be either visible (including
* bean-style naming) or explicitly annotated.
*
* Feature is enabled by default.
*
* @since 2.2
*/
INFER_PROPERTY_MUTATORS(true),
/**
* Feature that determines whether member fields declared as 'final' may
* be auto-detected to be used mutators (used to change value of the logical
* property) or not. If enabled, 'final' access modifier has no effect, and
* such fields may be detected according to usual visibility and inference
* rules; if disabled, such fields are NOT used as mutators except if
* explicitly annotated for such use.
*
* Feature is enabled by default, for backwards compatibility reasons.
*/
ALLOW_FINAL_FIELDS_AS_MUTATORS(true),
/*
/******************************************************
/* Type-handling features
/******************************************************
*/
/**
* Feature that determines whether the type detection for
* serialization should be using actual dynamic runtime type,
* or declared static type.
* Note that deserialization always uses declared static types
* since no runtime types are available (as we are creating
* instances after using type information).
*
* This global default value can be overridden at class, method
* or field level by using {@link JsonSerialize#typing} annotation
* property.
*
* Feature is disabled by default which means that dynamic runtime types
* are used (instead of declared static types) for serialization.
*/
USE_STATIC_TYPING(false),
/*
/******************************************************
/* View-related features
/******************************************************
*/
/**
* Feature that determines whether properties that have no view
* annotations are included in JSON serialization views (see
* {@link com.fasterxml.jackson.annotation.JsonView} for more
* details on JSON Views).
* If enabled, non-annotated properties will be included;
* when disabled, they will be excluded. So this feature
* changes between "opt-in" (feature disabled) and
* "opt-out" (feature enabled) modes.
*
* Default value is enabled, meaning that non-annotated
* properties are included in all views if there is no
* {@link com.fasterxml.jackson.annotation.JsonView} annotation.
*
* Feature is enabled by default.
*/
DEFAULT_VIEW_INCLUSION(true),
/*
/******************************************************
/* Generic output features
/******************************************************
*/
/**
* Feature that defines default property serialization order used
* for POJO fields (note: does not apply to {@link java.util.Map}
* serialization!):
* if enabled, default ordering is alphabetic (similar to
* how {@link com.fasterxml.jackson.annotation.JsonPropertyOrder#alphabetic()}
* works); if disabled, order is unspecified (based on what JDK gives
* us, which may be declaration order, but is not guaranteed).
*
* Note that this is just the default behavior, and can be overridden by
* explicit overrides in classes.
*
* Feature is disabled by default.
*/
SORT_PROPERTIES_ALPHABETICALLY(false),
/*
/******************************************************
/* Name-related features
/******************************************************
*/
/**
* Feature that can be enabled to make property names be
* overridden by wrapper name (usually detected with annotations
* as defined by {@link AnnotationIntrospector#findWrapperName}.
* If enabled, all properties that have associated non-empty Wrapper
* name will use that wrapper name instead of property name.
* If disabled, wrapper name is only used for wrapping (if anything).
*
* Feature is disabled by default.
*
* @since 2.1
*/
USE_WRAPPER_NAME_AS_PROPERTY_NAME(false)
;
private final boolean _defaultState;
private MapperFeature(boolean defaultState) {
_defaultState = defaultState;
}
@Override
public boolean enabledByDefault() { return _defaultState; }
@Override
public int getMask() { return (1 << ordinal()); }
} MappingIterator.java 0000664 0000000 0000000 00000017500 12373034547 0034426 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.Closeable;
import java.io.IOException;
import java.util.*;
import com.fasterxml.jackson.core.*;
/**
* Iterator exposed by {@link ObjectMapper} when binding sequence of
* objects. Extension is done to allow more convenient exposing of
* {@link IOException} (which basic {@link Iterator} does not expose)
*/
public class MappingIterator implements Iterator, Closeable
{
protected final static MappingIterator> EMPTY_ITERATOR =
new MappingIterator(null, null, null, null, false, null);
protected final JavaType _type;
protected final DeserializationContext _context;
protected final JsonDeserializer _deserializer;
protected JsonParser _parser;
/**
* Flag that indicates whether input {@link JsonParser} should be closed
* when we are done or not; generally only called when caller did not
* pass JsonParser.
*/
protected final boolean _closeParser;
/**
* Flag that is set when we have determined what {@link #hasNextValue()}
* should value; reset when {@link #nextValue} is called
*/
protected boolean _hasNextChecked;
/**
* If not null, "value to update" instead of creating a new instance
* for each call.
*/
protected final T _updatedValue;
/**
* @deprecated Since 2.1, to be removed
*/
@Deprecated
protected MappingIterator(JavaType type, JsonParser jp, DeserializationContext ctxt,
JsonDeserializer> deser)
{
this(type, jp, ctxt, deser, true, null);
}
/**
* @param managedParser Whether we "own" the {@link JsonParser} passed or not:
* if true, it was created by {@link ObjectReader} and code here needs to
* close it; if false, it was passed by calling code and should not be
* closed by iterator.
*/
@SuppressWarnings("unchecked")
protected MappingIterator(JavaType type, JsonParser jp, DeserializationContext ctxt,
JsonDeserializer> deser,
boolean managedParser, Object valueToUpdate)
{
_type = type;
_parser = jp;
_context = ctxt;
_deserializer = (JsonDeserializer) deser;
_closeParser = managedParser;
if (valueToUpdate == null) {
_updatedValue = null;
} else {
_updatedValue = (T) valueToUpdate;
}
/* Ok: one more thing; we may have to skip START_ARRAY, assuming
* "wrapped" sequence; but this is ONLY done for 'managed' parsers
* and never if JsonParser was directly passed by caller (if it
* was, caller must have either positioned it over first token of
* the first element, or cleared the START_ARRAY token explicitly).
* Note, however, that we do not try to guess whether this could be
* an unwrapped sequence of arrays/Lists: we just assume it is wrapped;
* and if not, caller needs to hand us JsonParser instead, pointing to
* the first token of the first element.
*/
if (managedParser && jp != null && jp.getCurrentToken() == JsonToken.START_ARRAY) {
jp.clearCurrentToken();
}
}
@SuppressWarnings("unchecked")
protected static MappingIterator emptyIterator() {
return (MappingIterator) EMPTY_ITERATOR;
}
/*
/**********************************************************
/* Basic iterator impl
/**********************************************************
*/
@Override
public boolean hasNext()
{
try {
return hasNextValue();
} catch (JsonMappingException e) {
throw new RuntimeJsonMappingException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public T next()
{
try {
return nextValue();
} catch (JsonMappingException e) {
throw new RuntimeJsonMappingException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void close() throws IOException{
if(_parser != null) {
_parser.close();
}
}
/*
/**********************************************************
/* Extended API, iteration
/**********************************************************
*/
/**
* Equivalent of {@link #next} but one that may throw checked
* exceptions from Jackson due to invalid input.
*/
public boolean hasNextValue() throws IOException
{
if (_parser == null) {
return false;
}
if (!_hasNextChecked) {
JsonToken t = _parser.getCurrentToken();
_hasNextChecked = true;
if (t == null) { // un-initialized or cleared; find next
t = _parser.nextToken();
// If EOF, no more, or if we hit END_ARRAY (although we don't clear the token).
if (t == null || t == JsonToken.END_ARRAY) {
JsonParser jp = _parser;
_parser = null;
if (_closeParser) {
jp.close();
}
return false;
}
}
}
return true;
}
public T nextValue() throws IOException
{
// caller should always call 'hasNext[Value]' first; but let's ensure:
if (!_hasNextChecked) {
if (!hasNextValue()) {
throw new NoSuchElementException();
}
}
if (_parser == null) {
throw new NoSuchElementException();
}
_hasNextChecked = false;
T result;
if (_updatedValue == null) {
result = _deserializer.deserialize(_parser, _context);
} else{
_deserializer.deserialize(_parser, _context, _updatedValue);
result = _updatedValue;
}
// Need to consume the token too
_parser.clearCurrentToken();
return result;
}
/**
* Convenience method for reading all entries accessible via
* this iterator
*
* @return List of entries read
*
* @since 2.2
*/
public List readAll() throws IOException {
return readAll(new ArrayList());
}
/**
* Convenience method for reading all entries accessible via
* this iterator
*
* @return List of entries read (same as passed-in argument)
*
* @since 2.2
*/
public List readAll(List resultList) throws IOException
{
while (hasNextValue()) {
resultList.add(nextValue());
}
return resultList;
}
/*
/**********************************************************
/* Extended API, accessors
/**********************************************************
*/
/**
* Accessor for getting underlying parser this iterator uses.
*
* @since 2.2
*/
public JsonParser getParser() {
return _parser;
}
/**
* Accessor for accessing {@link FormatSchema} that the underlying parser
* (as per {@link #getParser}) is using, if any; only parser of schema-aware
* formats use schemas.
*
* @since 2.2
*/
public FormatSchema getParserSchema() {
return _parser.getSchema();
}
/**
* Convenience method, functionally equivalent to:
*
* iterator.getParser().getCurrentLocation()
*
*
* @return Location of the input stream of the underlying parser
*
* @since 2.2.1
*/
public JsonLocation getCurrentLocation() {
return _parser.getCurrentLocation();
}
}
MappingJsonFactory.java 0000664 0000000 0000000 00000005364 12373034547 0035103 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind /* Jackson JSON-processor.
*
* Copyright (c) 2007- Tatu Saloranta, tatu.saloranta@iki.fi
*
* Licensed under the License specified in file LICENSE, included with
* the source code and binary code bundles.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fasterxml.jackson.databind;
import java.io.IOException;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.format.InputAccessor;
import com.fasterxml.jackson.core.format.MatchStrength;
/**
* Sub-class of {@link JsonFactory} that will create a proper
* {@link ObjectCodec} to allow seam-less conversions between
* JSON content and Java objects (POJOs).
* The only addition to regular {@link JsonFactory} currently
* is that {@link ObjectMapper} is constructed and passed as
* the codec to use.
*/
public class MappingJsonFactory
extends JsonFactory
{
// generated for Jackson 2.1.0
private static final long serialVersionUID = -6744103724013275513L;
public MappingJsonFactory()
{
this(null);
}
public MappingJsonFactory(ObjectMapper mapper)
{
super(mapper);
if (mapper == null) {
setCodec(new ObjectMapper(this));
}
}
/**
* We'll override the method to return more specific type; co-variance
* helps here
*/
@Override
public final ObjectMapper getCodec() { return (ObjectMapper) _objectCodec; }
// @since 2.1
@Override
public JsonFactory copy()
{
_checkInvalidCopy(MappingJsonFactory.class);
// note: as with base class, must NOT copy mapper reference
return new MappingJsonFactory(null);
}
/*
/**********************************************************
/* Format detection functionality (since 1.8)
/**********************************************************
*/
/**
* Sub-classes need to override this method (as of 1.8)
*/
@Override
public String getFormatName()
{
/* since non-JSON factories typically should not extend this class,
* let's just always return JSON as name.
*/
return FORMAT_NAME_JSON;
}
/**
* Sub-classes need to override this method (as of 1.8)
*/
@Override
public MatchStrength hasFormat(InputAccessor acc) throws IOException
{
if (getClass() == MappingJsonFactory.class) {
return hasJSONFormat(acc);
}
return null;
}
}
jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind/Module.java 0000664 0000000 0000000 00000027104 12373034547 0032626 0 ustar 00root root 0000000 0000000 package com.fasterxml.jackson.databind;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.deser.KeyDeserializers;
import com.fasterxml.jackson.databind.deser.ValueInstantiators;
import com.fasterxml.jackson.databind.introspect.ClassIntrospector;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.databind.ser.Serializers;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.type.TypeModifier;
/**
* Simple interface for extensions that can be registered with {@link ObjectMapper}
* to provide a well-defined set of extensions to default functionality; such as
* support for new data types.
*/
public abstract class Module
implements Versioned
{
/*
/**********************************************************
/* Simple accessors
/**********************************************************
*/
/**
* Method that returns identifier for module; this can be used by Jackson
* for informational purposes, as well as in associating extensions with
* module that provides them.
*/
public abstract String getModuleName();
/**
* Method that returns version of this module. Can be used by Jackson for
* informational purposes.
*/
@Override
public abstract Version version();
/*
/**********************************************************
/* Life-cycle: registration
/**********************************************************
*/
/**
* Method called by {@link ObjectMapper} when module is registered.
* It is called to let module register functionality it provides,
* using callback methods passed-in context object exposes.
*/
public abstract void setupModule(SetupContext context);
/*
/**********************************************************
/* Helper types
/**********************************************************
*/
/**
* Interface Jackson exposes to modules for purpose of registering
* extended functionality.
* Usually implemented by {@link ObjectMapper}, but modules should
* NOT rely on this -- if they do require access to mapper instance,
* they need to call {@link SetupContext#getOwner} method.
*/
public static interface SetupContext
{
/*
/**********************************************************
/* Simple accessors
/**********************************************************
*/
/**
* Method that returns version information about {@link ObjectMapper}
* that implements this context. Modules can use this to choose
* different settings or initialization order; or even decide to fail
* set up completely if version is compatible with module.
*/
public Version getMapperVersion();
/**
* Fallback access method that allows modules to refer to the
* {@link ObjectMapper} that provided this context.
* It should NOT be needed by most modules; and ideally should
* not be used -- however, there may be cases where this may
* be necessary due to various design constraints.
*
* NOTE: use of this method is discouraged, as it allows access to
* things Modules typically should not modify. It is included, however,
* to allow access to new features in cases where Module API
* has not yet been extended, or there are oversights.
*
* Return value is chosen to not leak dependency to {@link ObjectMapper};
* however, instance will always be of that type.
* This is why return value is declared generic, to allow caller to
* specify context to often avoid casting.
*
* @since 2.0
*/
public C getOwner();
/**
* Accessor for finding {@link TypeFactory} that is currently configured
* by the context.
*
* NOTE: since it is possible that other modules might change or replace
* TypeFactory, use of this method adds order-dependency for registrations.
*
* @since 2.0
*/
public TypeFactory getTypeFactory();
public boolean isEnabled(MapperFeature f);
public boolean isEnabled(DeserializationFeature f);
public boolean isEnabled(SerializationFeature f);
public boolean isEnabled(JsonFactory.Feature f);
public boolean isEnabled(JsonParser.Feature f);
public boolean isEnabled(JsonGenerator.Feature f);
/*
/**********************************************************
/* Handler registration; serializers/deserializers
/**********************************************************
*/
/**
* Method that module can use to register additional deserializers to use for
* handling types.
*
* @param d Object that can be called to find deserializer for types supported
* by module (null returned for non-supported types)
*/
public void addDeserializers(Deserializers d);
/**
* Method that module can use to register additional deserializers to use for
* handling Map key values (which are separate from value deserializers because
* they are always serialized from String values)
*/
public void addKeyDeserializers(KeyDeserializers s);
/**
* Method that module can use to register additional serializers to use for
* handling types.
*
* @param s Object that can be called to find serializer for types supported
* by module (null returned for non-supported types)
*/
public void addSerializers(Serializers s);
/**
* Method that module can use to register additional serializers to use for
* handling Map key values (which are separate from value serializers because
* they must write JsonToken.FIELD_NAME
instead of String value).
*/
public void addKeySerializers(Serializers s);
/*
/**********************************************************
/* Handler registration; other
/**********************************************************
*/
/**
* Method that module can use to register additional modifier objects to
* customize configuration and construction of bean deserializers.
*
* @param mod Modifier to register
*/
public void addBeanDeserializerModifier(BeanDeserializerModifier mod);
/**
* Method that module can use to register additional modifier objects to
* customize configuration and construction of bean serializers.
*
* @param mod Modifier to register
*/
public void addBeanSerializerModifier(BeanSerializerModifier mod);
/**
* Method that module can use to register additional
* {@link AbstractTypeResolver} instance, to handle resolution of
* abstract to concrete types (either by defaulting, or by materializing).
*
* @param resolver Resolver to add.
*/
public void addAbstractTypeResolver(AbstractTypeResolver resolver);
/**
* Method that module can use to register additional
* {@link TypeModifier} instance, which can augment {@link com.fasterxml.jackson.databind.JavaType}
* instances constructed by {@link com.fasterxml.jackson.databind.type.TypeFactory}.
*
* @param modifier to add
*/
public void addTypeModifier(TypeModifier modifier);
/**
* Method that module can use to register additional {@link com.fasterxml.jackson.databind.deser.ValueInstantiator}s,
* by adding {@link ValueInstantiators} object that gets called when
* instantatiator is needed by a deserializer.
*
* @param instantiators Object that can provide {@link com.fasterxml.jackson.databind.deser.ValueInstantiator}s for
* constructing POJO values during deserialization
*/
public void addValueInstantiators(ValueInstantiators instantiators);
/**
* Method for replacing the default class introspector with a derived class that
* overrides specific behavior.
*
* @param ci Derived class of ClassIntrospector with overriden behavior
*
* @since 2.2
*/
public void setClassIntrospector(ClassIntrospector ci);
/**
* Method for registering specified {@link AnnotationIntrospector} as the highest
* priority introspector (will be chained with existing introspector(s) which
* will be used as fallbacks for cases this introspector does not handle)
*
* @param ai Annotation introspector to register.
*/
public void insertAnnotationIntrospector(AnnotationIntrospector ai);
/**
* Method for registering specified {@link AnnotationIntrospector} as the lowest
* priority introspector, chained with existing introspector(s) and called
* as fallback for cases not otherwise handled.
*
* @param ai Annotation introspector to register.
*/
public void appendAnnotationIntrospector(AnnotationIntrospector ai);
/**
* Method for registering specified classes as subtypes (of supertype(s)
* they have)
*/
public void registerSubtypes(Class>... subtypes);
/**
* Method for registering specified classes as subtypes (of supertype(s)
* they have), using specified type names.
*/
public void registerSubtypes(NamedType... subtypes);
/**
* Method used for defining mix-in annotations to use for augmenting
* specified class or interface.
* All annotations from
* mixinSource
are taken to override annotations
* that target
(or its supertypes) has.
*
* Note: mix-ins are registered both for serialization and deserialization
* (which can be different internally).
*
* Note: currently only one set of mix-in annotations can be defined for
* a single class; so if multiple modules register mix-ins, highest
* priority one (last one registered) will have priority over other modules.
*
* @param target Class (or interface) whose annotations to effectively override
* @param mixinSource Class (or interface) whose annotations are to
* be "added" to target's annotations, overriding as necessary
*/
public void setMixInAnnotations(Class> target, Class> mixinSource);
/**
* Add a deserialization problem handler
*
* @param handler The deserialization problem handler
*/
public void addDeserializationProblemHandler(DeserializationProblemHandler handler);
/**
* Method that may be used to override naming strategy that is used
* by {@link ObjectMapper}.
*
* @since 2.3
*/
public void setNamingStrategy(PropertyNamingStrategy naming);
}
}
ObjectMapper.java 0000664 0000000 0000000 00000355114 12373034547 0033702 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.*;
import java.lang.reflect.Type;
import java.net.URL;
import java.text.DateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.*;
import com.fasterxml.jackson.databind.cfg.BaseSettings;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.cfg.HandlerInstantiator;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.introspect.*;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsontype.*;
import com.fasterxml.jackson.databind.jsontype.impl.StdSubtypeResolver;
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder;
import com.fasterxml.jackson.databind.node.*;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.type.*;
import com.fasterxml.jackson.databind.util.RootNameLookup;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import com.fasterxml.jackson.databind.util.TokenBuffer;
/**
* This mapper (or, data binder, or codec) provides functionality for
* converting between Java objects (instances of JDK provided core classes,
* beans), and matching JSON constructs.
* It will use instances of {@link JsonParser} and {@link JsonGenerator}
* for implementing actual reading/writing of JSON.
*
* The main conversion API is defined in {@link ObjectCodec}, so that
* implementation details of this class need not be exposed to
* streaming parser and generator classes.
*
* Note on caching: root-level deserializers are always cached, and accessed
* using full (generics-aware) type information. This is different from
* caching of referenced types, which is more limited and is done only
* for a subset of all deserializer types. The main reason for difference
* is that at root-level there is no incoming reference (and hence no
* referencing property, no referral information or annotations to
* produce differing deserializers), and that the performance impact
* greatest at root level (since it'll essentially cache the full
* graph of deserializers involved).
*/
public class ObjectMapper
extends ObjectCodec
implements Versioned,
java.io.Serializable // as of 2.1
{
private static final long serialVersionUID = 1L;
/*
/**********************************************************
/* Helper classes, enums
/**********************************************************
*/
/**
* Enumeration used with {@link ObjectMapper#enableDefaultTyping()}
* to specify what kind of types (classes) default typing should
* be used for. It will only be used if no explicit type information
* is found, but this enumeration further limits subset of those types.
*
* Since 2.4 there are special exceptions for JSON Tree model
* types (sub-types of {@link TreeNode}: default typing is never
* applied to them
* (see Issue#88 for details)
*/
public enum DefaultTyping {
/**
* This value means that only properties that have
* {@link java.lang.Object} as declared type (including
* generic types without explicit type) will use default
* typing.
*/
JAVA_LANG_OBJECT,
/**
* Value that means that default typing will be used for
* properties with declared type of {@link java.lang.Object}
* or an abstract type (abstract class or interface).
* Note that this does not include array types.
*
* Since 2.4, this does NOT apply to {@link TreeNode} and its subtypes.
*/
OBJECT_AND_NON_CONCRETE,
/**
* Value that means that default typing will be used for
* all types covered by {@link #OBJECT_AND_NON_CONCRETE}
* plus all array types for them.
*
* Since 2.4, this does NOT apply to {@link TreeNode} and its subtypes.
*/
NON_CONCRETE_AND_ARRAYS,
/**
* Value that means that default typing will be used for
* all non-final types, with exception of small number of
* "natural" types (String, Boolean, Integer, Double), which
* can be correctly inferred from JSON; as well as for
* all arrays of non-final types.
*
* Since 2.4, this does NOT apply to {@link TreeNode} and its subtypes.
*/
NON_FINAL
}
/**
* Customized {@link TypeResolverBuilder} that provides type resolver builders
* used with so-called "default typing"
* (see {@link ObjectMapper#enableDefaultTyping()} for details).
*
* Type resolver construction is based on configuration: implementation takes care
* of only providing builders in cases where type information should be applied.
* This is important since build calls may be sent for any and all types, and
* type information should NOT be applied to all of them.
*/
public static class DefaultTypeResolverBuilder
extends StdTypeResolverBuilder
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;
/**
* Definition of what types is this default typer valid for.
*/
protected final DefaultTyping _appliesFor;
public DefaultTypeResolverBuilder(DefaultTyping t) {
_appliesFor = t;
}
@Override
public TypeDeserializer buildTypeDeserializer(DeserializationConfig config,
JavaType baseType, Collection subtypes)
{
return useForType(baseType) ? super.buildTypeDeserializer(config, baseType, subtypes) : null;
}
@Override
public TypeSerializer buildTypeSerializer(SerializationConfig config,
JavaType baseType, Collection subtypes)
{
return useForType(baseType) ? super.buildTypeSerializer(config, baseType, subtypes) : null;
}
/**
* Method called to check if the default type handler should be
* used for given type.
* Note: "natural types" (String, Boolean, Integer, Double) will never
* use typing; that is both due to them being concrete and final,
* and since actual serializers and deserializers will also ignore any
* attempts to enforce typing.
*/
public boolean useForType(JavaType t)
{
switch (_appliesFor) {
case NON_CONCRETE_AND_ARRAYS:
while (t.isArrayType()) {
t = t.getContentType();
}
// fall through
case OBJECT_AND_NON_CONCRETE:
return (t.getRawClass() == Object.class) || !t.isConcrete()
// [Issue#88] Should not apply to JSON tree models:
|| TreeNode.class.isAssignableFrom(t.getRawClass());
case NON_FINAL:
while (t.isArrayType()) {
t = t.getContentType();
}
// [Issue#88] Should not apply to JSON tree models:
return !t.isFinal() && !TreeNode.class.isAssignableFrom(t.getRawClass());
default:
//case JAVA_LANG_OBJECT:
return (t.getRawClass() == Object.class);
}
}
}
/*
/**********************************************************
/* Internal constants, singletons
/**********************************************************
*/
// Quick little shortcut, to avoid having to use global TypeFactory instance...
private final static JavaType JSON_NODE_TYPE = SimpleType.constructUnsafe(JsonNode.class);
/* !!! 03-Apr-2009, tatu: Should try to avoid direct reference... but not
* sure what'd be simple and elegant way. So until then:
*/
protected final static ClassIntrospector DEFAULT_INTROSPECTOR = BasicClassIntrospector.instance;
// 16-May-2009, tatu: Ditto ^^^
protected final static AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR = new JacksonAnnotationIntrospector();
protected final static VisibilityChecker> STD_VISIBILITY_CHECKER = VisibilityChecker.Std.defaultInstance();
protected final static PrettyPrinter _defaultPrettyPrinter = new DefaultPrettyPrinter();
/**
* Base settings contain defaults used for all {@link ObjectMapper}
* instances.
*/
protected final static BaseSettings DEFAULT_BASE = new BaseSettings(DEFAULT_INTROSPECTOR,
DEFAULT_ANNOTATION_INTROSPECTOR, STD_VISIBILITY_CHECKER, null, TypeFactory.defaultInstance(),
null, StdDateFormat.instance, null,
Locale.getDefault(),
// TimeZone.getDefault()
TimeZone.getTimeZone("GMT"),
Base64Variants.getDefaultVariant() // 2.1
);
/*
/**********************************************************
/* Configuration settings, shared
/**********************************************************
*/
/**
* Factory used to create {@link JsonParser} and {@link JsonGenerator}
* instances as necessary.
*/
protected final JsonFactory _jsonFactory;
/**
* Specific factory used for creating {@link JavaType} instances;
* needed to allow modules to add more custom type handling
* (mostly to support types of non-Java JVM languages)
*/
protected TypeFactory _typeFactory;
/**
* Provider for values to inject in deserialized POJOs.
*/
protected InjectableValues _injectableValues;
/**
* Thing used for registering sub-types, resolving them to
* super/sub-types as needed.
*/
protected SubtypeResolver _subtypeResolver;
/**
* Cache for root names used when root-wrapping is enabled.
*/
protected final RootNameLookup _rootNames;
/*
/**********************************************************
/* Configuration settings: mix-in annotations
/**********************************************************
*/
/**
* Mapping that defines how to apply mix-in annotations: key is
* the type to received additional annotations, and value is the
* type that has annotations to "mix in".
*
* Annotations associated with the value classes will be used to
* override annotations of the key class, associated with the
* same field or method. They can be further masked by sub-classes:
* you can think of it as injecting annotations between the target
* class and its sub-classes (or interfaces)
*/
protected final HashMap> _mixInAnnotations;
/*
/**********************************************************
/* Configuration settings, serialization
/**********************************************************
*/
/**
* Configuration object that defines basic global
* settings for the serialization process
*/
protected SerializationConfig _serializationConfig;
/**
* Object that manages access to serializers used for serialization,
* including caching.
* It is configured with {@link #_serializerFactory} to allow
* for constructing custom serializers.
*
* Note: while serializers are only exposed {@link SerializerProvider},
* mappers and readers need to access additional API defined by
* {@link DefaultSerializerProvider}
*/
protected DefaultSerializerProvider _serializerProvider;
/**
* Serializer factory used for constructing serializers.
*/
protected SerializerFactory _serializerFactory;
/*
/**********************************************************
/* Configuration settings, deserialization
/**********************************************************
*/
/**
* Configuration object that defines basic global
* settings for the serialization process
*/
protected DeserializationConfig _deserializationConfig;
/**
* Blueprint context object; stored here to allow custom
* sub-classes. Contains references to objects needed for
* deserialization construction (cache, factory).
*/
protected DefaultDeserializationContext _deserializationContext;
/*
/**********************************************************
/* Caching
/**********************************************************
*/
/* Note: handling of serializers and deserializers is not symmetric;
* and as a result, only root-level deserializers can be cached here.
* This is mostly because typing and resolution for deserializers is
* fully static; whereas it is quite dynamic for serialization.
*/
/**
* We will use a separate main-level Map for keeping track
* of root-level deserializers. This is where most succesful
* cache lookups get resolved.
* Map will contain resolvers for all kinds of types, including
* container types: this is different from the component cache
* which will only cache bean deserializers.
*
* Given that we don't expect much concurrency for additions
* (should very quickly converge to zero after startup), let's
* explicitly define a low concurrency setting.
*
* Since version 1.5, these may are either "raw" deserializers (when
* no type information is needed for base type), or type-wrapped
* deserializers (if it is needed)
*/
final protected ConcurrentHashMap> _rootDeserializers
= new ConcurrentHashMap>(64, 0.6f, 2);
/*
/**********************************************************
/* Life-cycle: constructing instance
/**********************************************************
*/
/**
* Default constructor, which will construct the default
* {@link JsonFactory} as necessary, use
* {@link SerializerProvider} as its
* {@link SerializerProvider}, and
* {@link BeanSerializerFactory} as its
* {@link SerializerFactory}.
* This means that it
* can serialize all standard JDK types, as well as regular
* Java Beans (based on method names and Jackson-specific annotations),
* but does not support JAXB annotations.
*/
public ObjectMapper()
{
this(null, null, null);
}
/**
* Constructs instance that uses specified {@link JsonFactory}
* for constructing necessary {@link JsonParser}s and/or
* {@link JsonGenerator}s.
*/
public ObjectMapper(JsonFactory jf)
{
this(jf, null, null);
}
/**
* Copy-constructor, mostly used to support {@link #copy}.
*
* @since 2.1
*/
protected ObjectMapper(ObjectMapper src)
{
_jsonFactory = src._jsonFactory.copy();
_jsonFactory.setCodec(this);
_subtypeResolver = src._subtypeResolver;
_rootNames = new RootNameLookup();
_typeFactory = src._typeFactory;
_serializationConfig = src._serializationConfig;
HashMap> mixins = new HashMap>(src._mixInAnnotations);
_mixInAnnotations = mixins;
_serializationConfig = new SerializationConfig(src._serializationConfig, mixins);
_deserializationConfig = new DeserializationConfig(src._deserializationConfig, mixins);
_serializerProvider = src._serializerProvider;
_deserializationContext = src._deserializationContext;
// Default serializer factory is stateless, can just assign
_serializerFactory = src._serializerFactory;
}
/**
* Constructs instance that uses specified {@link JsonFactory}
* for constructing necessary {@link JsonParser}s and/or
* {@link JsonGenerator}s, and uses given providers for accessing
* serializers and deserializers.
*
* @param jf JsonFactory to use: if null, a new {@link MappingJsonFactory} will be constructed
* @param sp SerializerProvider to use: if null, a {@link SerializerProvider} will be constructed
* @param dc Blueprint deserialization context instance to use for creating
* actual context objects; if null, will construct standard
* {@link DeserializationContext}
*/
public ObjectMapper(JsonFactory jf,
DefaultSerializerProvider sp, DefaultDeserializationContext dc)
{
/* 02-Mar-2009, tatu: Important: we MUST default to using
* the mapping factory, otherwise tree serialization will
* have problems with POJONodes.
* 03-Jan-2010, tatu: and obviously we also must pass 'this',
* to create actual linking.
*/
if (jf == null) {
_jsonFactory = new MappingJsonFactory(this);
} else {
_jsonFactory = jf;
if (jf.getCodec() == null) { // as per [JACKSON-741]
_jsonFactory.setCodec(this);
}
}
_subtypeResolver = new StdSubtypeResolver();
_rootNames = new RootNameLookup();
// and default type factory is shared one
_typeFactory = TypeFactory.defaultInstance();
HashMap> mixins = new HashMap>();
_mixInAnnotations = mixins;
_serializationConfig = new SerializationConfig(DEFAULT_BASE,
_subtypeResolver, mixins);
_deserializationConfig = new DeserializationConfig(DEFAULT_BASE,
_subtypeResolver, mixins);
// Some overrides we may need
final boolean needOrder = _jsonFactory.requiresPropertyOrdering();
if (needOrder ^ _serializationConfig.isEnabled(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY)) {
configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, needOrder);
}
_serializerProvider = (sp == null) ? new DefaultSerializerProvider.Impl() : sp;
_deserializationContext = (dc == null) ?
new DefaultDeserializationContext.Impl(BeanDeserializerFactory.instance) : dc;
// Default serializer factory is stateless, can just assign
_serializerFactory = BeanSerializerFactory.instance;
}
/**
* Method for creating a new {@link ObjectMapper} instance that
* has same initial configuration as this instance. Note that this
* also requires making a copy of the underlying {@link JsonFactory}
* instance.
*
* Method is typically
* used when multiple, differently configured mappers are needed.
* Although configuration is shared, cached serializers and deserializers
* are NOT shared, which means that the new instance may be re-configured
* before use; meaning that it behaves the same way as if an instance
* was constructed from scratch.
*
* @since 2.1
*/
public ObjectMapper copy()
{
_checkInvalidCopy(ObjectMapper.class);
return new ObjectMapper(this);
}
/**
* @since 2.1
* @param exp
*/
protected void _checkInvalidCopy(Class> exp)
{
if (getClass() != exp) {
throw new IllegalStateException("Failed copy(): "+getClass().getName()
+" (version: "+version()+") does not override copy(); it has to");
}
}
/*
/**********************************************************
/* Versioned impl
/**********************************************************
*/
/**
* Method that will return version information stored in and read from jar
* that contains this class.
*/
@Override
public Version version() {
return com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
}
/*
/**********************************************************
/* Module registration, discovery
/**********************************************************
*/
/**
* Method for registering a module that can extend functionality
* provided by this mapper; for example, by adding providers for
* custom serializers and deserializers.
*
* @param module Module to register
*/
public ObjectMapper registerModule(Module module)
{
/* Let's ensure we have access to name and version information,
* even if we do not have immediate use for either. This way we know
* that they will be available from beginning
*/
String name = module.getModuleName();
if (name == null) {
throw new IllegalArgumentException("Module without defined name");
}
Version version = module.version();
if (version == null) {
throw new IllegalArgumentException("Module without defined version");
}
final ObjectMapper mapper = this;
// And then call registration
module.setupModule(new Module.SetupContext()
{
// // // Accessors
@Override
public Version getMapperVersion() {
return version();
}
@SuppressWarnings("unchecked")
@Override
public C getOwner() {
// why do we need the cast here?!?
return (C) mapper;
}
@Override
public TypeFactory getTypeFactory() {
return _typeFactory;
}
@Override
public boolean isEnabled(MapperFeature f) {
return mapper.isEnabled(f);
}
@Override
public boolean isEnabled(DeserializationFeature f) {
return mapper.isEnabled(f);
}
@Override
public boolean isEnabled(SerializationFeature f) {
return mapper.isEnabled(f);
}
@Override
public boolean isEnabled(JsonFactory.Feature f) {
return mapper.isEnabled(f);
}
@Override
public boolean isEnabled(JsonParser.Feature f) {
return mapper.isEnabled(f);
}
@Override
public boolean isEnabled(JsonGenerator.Feature f) {
return mapper.isEnabled(f);
}
// // // Methods for registering handlers: deserializers
@Override
public void addDeserializers(Deserializers d) {
DeserializerFactory df = mapper._deserializationContext._factory.withAdditionalDeserializers(d);
mapper._deserializationContext = mapper._deserializationContext.with(df);
}
@Override
public void addKeyDeserializers(KeyDeserializers d) {
DeserializerFactory df = mapper._deserializationContext._factory.withAdditionalKeyDeserializers(d);
mapper._deserializationContext = mapper._deserializationContext.with(df);
}
@Override
public void addBeanDeserializerModifier(BeanDeserializerModifier modifier) {
DeserializerFactory df = mapper._deserializationContext._factory.withDeserializerModifier(modifier);
mapper._deserializationContext = mapper._deserializationContext.with(df);
}
// // // Methods for registering handlers: serializers
@Override
public void addSerializers(Serializers s) {
mapper._serializerFactory = mapper._serializerFactory.withAdditionalSerializers(s);
}
@Override
public void addKeySerializers(Serializers s) {
mapper._serializerFactory = mapper._serializerFactory.withAdditionalKeySerializers(s);
}
@Override
public void addBeanSerializerModifier(BeanSerializerModifier modifier) {
mapper._serializerFactory = mapper._serializerFactory.withSerializerModifier(modifier);
}
// // // Methods for registering handlers: other
@Override
public void addAbstractTypeResolver(AbstractTypeResolver resolver) {
DeserializerFactory df = mapper._deserializationContext._factory.withAbstractTypeResolver(resolver);
mapper._deserializationContext = mapper._deserializationContext.with(df);
}
@Override
public void addTypeModifier(TypeModifier modifier) {
TypeFactory f = mapper._typeFactory;
f = f.withModifier(modifier);
mapper.setTypeFactory(f);
}
@Override
public void addValueInstantiators(ValueInstantiators instantiators) {
DeserializerFactory df = mapper._deserializationContext._factory.withValueInstantiators(instantiators);
mapper._deserializationContext = mapper._deserializationContext.with(df);
}
@Override
public void setClassIntrospector(ClassIntrospector ci) {
mapper._deserializationConfig = mapper._deserializationConfig.with(ci);
mapper._serializationConfig = mapper._serializationConfig.with(ci);
}
@Override
public void insertAnnotationIntrospector(AnnotationIntrospector ai) {
mapper._deserializationConfig = mapper._deserializationConfig.withInsertedAnnotationIntrospector(ai);
mapper._serializationConfig = mapper._serializationConfig.withInsertedAnnotationIntrospector(ai);
}
@Override
public void appendAnnotationIntrospector(AnnotationIntrospector ai) {
mapper._deserializationConfig = mapper._deserializationConfig.withAppendedAnnotationIntrospector(ai);
mapper._serializationConfig = mapper._serializationConfig.withAppendedAnnotationIntrospector(ai);
}
@Override
public void registerSubtypes(Class>... subtypes) {
mapper.registerSubtypes(subtypes);
}
@Override
public void registerSubtypes(NamedType... subtypes) {
mapper.registerSubtypes(subtypes);
}
@Override
public void setMixInAnnotations(Class> target, Class> mixinSource) {
mapper.addMixInAnnotations(target, mixinSource);
}
@Override
public void addDeserializationProblemHandler(DeserializationProblemHandler handler) {
mapper.addHandler(handler);
}
@Override
public void setNamingStrategy(PropertyNamingStrategy naming) {
mapper.setPropertyNamingStrategy(naming);
}
});
return this;
}
/**
* Convenience method for registering specified modules in order;
* functionally equivalent to:
*
* for (Module module : modules) {
* registerModule(module);
* }
*
*
* @since 2.2
*/
public ObjectMapper registerModules(Module... modules)
{
for (Module module : modules) {
registerModule(module);
}
return this;
}
/**
* Convenience method for registering specified modules in order;
* functionally equivalent to:
*
* for (Module module : modules) {
* registerModule(module);
* }
*
*
* @since 2.2
*/
public ObjectMapper registerModules(Iterable modules)
{
for (Module module : modules) {
registerModule(module);
}
return this;
}
/**
* Method for locating available methods, using JDK {@link ServiceLoader}
* facility, along with module-provided SPI.
*
* Note that method does not do any caching, so calls should be considered
* potentially expensive.
*
* @since 2.2
*/
public static List findModules() {
return findModules(null);
}
/**
* Method for locating available methods, using JDK {@link ServiceLoader}
* facility, along with module-provided SPI.
*
* Note that method does not do any caching, so calls should be considered
* potentially expensive.
*
* @since 2.2
*/
public static List findModules(ClassLoader classLoader)
{
ArrayList modules = new ArrayList();
ServiceLoader loader = (classLoader == null) ?
ServiceLoader.load(Module.class) : ServiceLoader.load(Module.class, classLoader);
for (Module module : loader) {
modules.add(module);
}
return modules;
}
/**
* Convenience method that is functionally equivalent to:
*
* mapper.registerModules(mapper.findModules());
*
*
* As with {@link #findModules()}, no caching is done for modules, so care
* needs to be taken to either create and share a single mapper instance;
* or to cache introspected set of modules.
*
* @since 2.2
*/
public ObjectMapper findAndRegisterModules() {
return registerModules(findModules());
}
/*
/**********************************************************
/* Configuration: main config object access
/**********************************************************
*/
/**
* Method that returns the shared default {@link SerializationConfig}
* object that defines configuration settings for serialization.
*
* Note that since instances are immutable, you can NOT change settings
* by accessing an instance and calling methods: this will simply create
* new instance of config object.
*/
public SerializationConfig getSerializationConfig() {
return _serializationConfig;
}
/**
* Method that returns
* the shared default {@link DeserializationConfig} object
* that defines configuration settings for deserialization.
*
* Note that since instances are immutable, you can NOT change settings
* by accessing an instance and calling methods: this will simply create
* new instance of config object.
*/
public DeserializationConfig getDeserializationConfig() {
return _deserializationConfig;
}
/**
* Method for getting current {@link DeserializationContext}.
*
* Note that since instances are immutable, you can NOT change settings
* by accessing an instance and calling methods: this will simply create
* new instance of context object.
*/
public DeserializationContext getDeserializationContext() {
return _deserializationContext;
}
/*
/**********************************************************
/* Configuration: ser/deser factory, provider access
/**********************************************************
*/
/**
* Method for setting specific {@link SerializerFactory} to use
* for constructing (bean) serializers.
*/
public ObjectMapper setSerializerFactory(SerializerFactory f) {
_serializerFactory = f;
return this;
}
/**
* Method for getting current {@link SerializerFactory}.
*
* Note that since instances are immutable, you can NOT change settings
* by accessing an instance and calling methods: this will simply create
* new instance of factory object.
*/
public SerializerFactory getSerializerFactory() {
return _serializerFactory;
}
/**
* Method for setting specific {@link SerializerProvider} to use
* for handling caching of {@link JsonSerializer} instances.
*/
public ObjectMapper setSerializerProvider(DefaultSerializerProvider p) {
_serializerProvider = p;
return this;
}
public SerializerProvider getSerializerProvider() {
return _serializerProvider;
}
/*
/**********************************************************
/* Configuration: mix-in annotations
/**********************************************************
*/
/**
* Method to use for defining mix-in annotations to use for augmenting
* annotations that processable (serializable / deserializable)
* classes have.
* Mixing in is done when introspecting class annotations and properties.
* Map passed contains keys that are target classes (ones to augment
* with new annotation overrides), and values that are source classes
* (have annotations to use for augmentation).
* Annotations from source classes (and their supertypes)
* will override
* annotations that target classes (and their super-types) have.
*/
public final void setMixInAnnotations(Map, Class>> sourceMixins)
{
_mixInAnnotations.clear();
if (sourceMixins != null && sourceMixins.size() > 0) {
for (Map.Entry,Class>> en : sourceMixins.entrySet()) {
_mixInAnnotations.put(new ClassKey(en.getKey()), en.getValue());
}
}
}
/**
* Method to use for adding mix-in annotations to use for augmenting
* specified class or interface. All annotations from
* mixinSource
are taken to override annotations
* that target
(or its supertypes) has.
*
* @param target Class (or interface) whose annotations to effectively override
* @param mixinSource Class (or interface) whose annotations are to
* be "added" to target's annotations, overriding as necessary
*/
public final void addMixInAnnotations(Class> target, Class> mixinSource)
{
_mixInAnnotations.put(new ClassKey(target), mixinSource);
}
public final Class> findMixInClassFor(Class> cls) {
return (_mixInAnnotations == null) ? null : _mixInAnnotations.get(new ClassKey(cls));
}
public final int mixInCount() {
return (_mixInAnnotations == null) ? 0 : _mixInAnnotations.size();
}
/*
/**********************************************************
/* Configuration, introspection
/**********************************************************
*/
/**
* Method for accessing currently configured visibility checker;
* object used for determining whether given property element
* (method, field, constructor) can be auto-detected or not.
*/
public VisibilityChecker> getVisibilityChecker() {
return _serializationConfig.getDefaultVisibilityChecker();
}
/**
* Method for setting currently configured visibility checker;
* object used for determining whether given property element
* (method, field, constructor) can be auto-detected or not.
* This default checker is used if no per-class overrides
* are defined.
*/
public void setVisibilityChecker(VisibilityChecker> vc) {
_deserializationConfig = _deserializationConfig.with(vc);
_serializationConfig = _serializationConfig.with(vc);
}
/**
* Convenience method that allows changing configuration for
* underlying {@link VisibilityChecker}s, to change details of what kinds of
* properties are auto-detected.
* Basically short cut for doing:
*
* mapper.setVisibilityChecker(
* mapper.getVisibilityChecker().withVisibility(forMethod, visibility)
* );
*
* one common use case would be to do:
*
* mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY);
*
* which would make all member fields serializable without further annotations,
* instead of just public fields (default setting).
*
* @param forMethod Type of property descriptor affected (field, getter/isGetter,
* setter, creator)
* @param visibility Minimum visibility to require for the property descriptors of type
*
* @return Modified mapper instance (that is, "this"), to allow chaining
* of configuration calls
*/
public ObjectMapper setVisibility(PropertyAccessor forMethod, JsonAutoDetect.Visibility visibility)
{
_deserializationConfig = _deserializationConfig.withVisibility(forMethod, visibility);
_serializationConfig = _serializationConfig.withVisibility(forMethod, visibility);
return this;
}
/**
* Method for accessing subtype resolver in use.
*/
public SubtypeResolver getSubtypeResolver() {
return _subtypeResolver;
}
/**
* Method for setting custom subtype resolver to use.
*/
public ObjectMapper setSubtypeResolver(SubtypeResolver str) {
_subtypeResolver = str;
_deserializationConfig = _deserializationConfig.with(str);
_serializationConfig = _serializationConfig.with(str);
return this;
}
/**
* Method for changing {@link AnnotationIntrospector} used by this
* mapper instance for both serialization and deserialization
*/
public ObjectMapper setAnnotationIntrospector(AnnotationIntrospector ai) {
_serializationConfig = _serializationConfig.with(ai);
_deserializationConfig = _deserializationConfig.with(ai);
return this;
}
/**
* Method for changing {@link AnnotationIntrospector} instances used
* by this mapper instance for serialization and deserialization,
* specifying them separately so that different introspection can be
* used for different aspects
*
* @since 2.1
*
* @param serializerAI {@link AnnotationIntrospector} to use for configuring
* serialization
* @param deserializerAI {@link AnnotationIntrospector} to use for configuring
* deserialization
*/
public ObjectMapper setAnnotationIntrospectors(AnnotationIntrospector serializerAI,
AnnotationIntrospector deserializerAI) {
_serializationConfig = _serializationConfig.with(serializerAI);
_deserializationConfig = _deserializationConfig.with(deserializerAI);
return this;
}
/**
* Method for setting custom property naming strategy to use.
*/
public ObjectMapper setPropertyNamingStrategy(PropertyNamingStrategy s) {
_serializationConfig = _serializationConfig.with(s);
_deserializationConfig = _deserializationConfig.with(s);
return this;
}
/**
* Method for setting defalt POJO property inclusion strategy for serialization.
*/
public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
_serializationConfig = _serializationConfig.withSerializationInclusion(incl);
return this;
}
/*
/**********************************************************
/* Type information configuration (1.5+)
/**********************************************************
*/
/**
* Convenience method that is equivalent to calling
*
* enableObjectTyping(DefaultTyping.OBJECT_AND_NON_CONCRETE);
*
*/
public ObjectMapper enableDefaultTyping() {
return enableDefaultTyping(DefaultTyping.OBJECT_AND_NON_CONCRETE);
}
/**
* Convenience method that is equivalent to calling
*
* enableObjectTyping(dti, JsonTypeInfo.As.WRAPPER_ARRAY);
*
*/
public ObjectMapper enableDefaultTyping(DefaultTyping dti) {
return enableDefaultTyping(dti, JsonTypeInfo.As.WRAPPER_ARRAY);
}
/**
* Method for enabling automatic inclusion of type information, needed
* for proper deserialization of polymorphic types (unless types
* have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}).
*
* @param applicability Defines kinds of types for which additional type information
* is added; see {@link DefaultTyping} for more information.
*/
public ObjectMapper enableDefaultTyping(DefaultTyping applicability, JsonTypeInfo.As includeAs)
{
TypeResolverBuilder> typer = new DefaultTypeResolverBuilder(applicability);
// we'll always use full class name, when using defaulting
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(includeAs);
return setDefaultTyping(typer);
}
/**
* Method for enabling automatic inclusion of type information -- needed
* for proper deserialization of polymorphic types (unless types
* have been annotated with {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) --
* using "As.PROPERTY" inclusion mechanism and specified property name
* to use for inclusion (default being "@class" since default type information
* always uses class name as type identifier)
*/
public ObjectMapper enableDefaultTypingAsProperty(DefaultTyping applicability, String propertyName)
{
TypeResolverBuilder> typer = new DefaultTypeResolverBuilder(applicability);
// we'll always use full class name, when using defaulting
typer = typer.init(JsonTypeInfo.Id.CLASS, null);
typer = typer.inclusion(JsonTypeInfo.As.PROPERTY);
typer = typer.typeProperty(propertyName);
return setDefaultTyping(typer);
}
/**
* Method for disabling automatic inclusion of type information; if so, only
* explicitly annotated types (ones with
* {@link com.fasterxml.jackson.annotation.JsonTypeInfo}) will have
* additional embedded type information.
*/
public ObjectMapper disableDefaultTyping() {
return setDefaultTyping(null);
}
/**
* Method for enabling automatic inclusion of type information, using
* specified handler object for determining which types this affects,
* as well as details of how information is embedded.
*
* @param typer Type information inclusion handler
*/
public ObjectMapper setDefaultTyping(TypeResolverBuilder> typer) {
_deserializationConfig = _deserializationConfig.with(typer);
_serializationConfig = _serializationConfig.with(typer);
return this;
}
/**
* Method for registering specified class as a subtype, so that
* typename-based resolution can link supertypes to subtypes
* (as an alternative to using annotations).
* Type for given class is determined from appropriate annotation;
* or if missing, default name (unqualified class name)
*/
public void registerSubtypes(Class>... classes) {
getSubtypeResolver().registerSubtypes(classes);
}
/**
* Method for registering specified class as a subtype, so that
* typename-based resolution can link supertypes to subtypes
* (as an alternative to using annotations).
* Name may be provided as part of argument, but if not will
* be based on annotations or use default name (unqualified
* class name).
*/
public void registerSubtypes(NamedType... types) {
getSubtypeResolver().registerSubtypes(types);
}
/*
/**********************************************************
/* Configuration, basic type handling
/**********************************************************
*/
/**
* Accessor for getting currently configured {@link TypeFactory} instance.
*/
public TypeFactory getTypeFactory() {
return _typeFactory;
}
/**
* Method that can be used to override {@link TypeFactory} instance
* used by this mapper.
*
* Note: will also set {@link TypeFactory} that deserialization and
* serialization config objects use.
*/
public ObjectMapper setTypeFactory(TypeFactory f)
{
_typeFactory = f;
_deserializationConfig = _deserializationConfig.with(f);
_serializationConfig = _serializationConfig.with(f);
return this;
}
/**
* Convenience method for constructing {@link JavaType} out of given
* type (typically java.lang.Class
), but without explicit
* context.
*/
public JavaType constructType(Type t) {
return _typeFactory.constructType(t);
}
/*
/**********************************************************
/* Configuration, deserialization
/**********************************************************
*/
/**
* Method for specifying {@link JsonNodeFactory} to use for
* constructing root level tree nodes (via method
* {@link #createObjectNode}
*/
public ObjectMapper setNodeFactory(JsonNodeFactory f) {
_deserializationConfig = _deserializationConfig.with(f);
return this;
}
/**
* Method for adding specified {@link DeserializationProblemHandler}
* to be used for handling specific problems during deserialization.
*/
public ObjectMapper addHandler(DeserializationProblemHandler h) {
_deserializationConfig = _deserializationConfig.withHandler(h);
return this;
}
/**
* Method for removing all registered {@link DeserializationProblemHandler}s
* instances from this mapper.
*/
public ObjectMapper clearProblemHandlers() {
_deserializationConfig = _deserializationConfig.withNoProblemHandlers();
return this;
}
/**
* Method that allows overriding of the underlying {@link DeserializationConfig}
* object.
* It is added as a fallback method that may be used if no other configuration
* modifier method works: it should not be used if there are alternatives,
* and its use is generally discouraged.
*
* NOTE : only use this method if you know what you are doing -- it allows
* by-passing some of checks applied to other configuration methods.
* Also keep in mind that as with all configuration of {@link ObjectMapper},
* this is only thread-safe if done before calling any deserialization methods.
*
* @since 2.4
*/
public ObjectMapper setConfig(DeserializationConfig config) {
_deserializationConfig = config;
return this;
}
/*
/**********************************************************
/* Configuration, serialization
/**********************************************************
*/
/**
* Convenience method that is equivalent to:
*
* mapper.setFilters(mapper.getSerializationConfig().withFilters(filterProvider));
*
*
* Note that usually it is better to use method {@link #writer(FilterProvider)};
* however, sometimes
* this method is more convenient. For example, some frameworks only allow configuring
* of ObjectMapper instances and not ObjectWriters.
*/
public void setFilters(FilterProvider filterProvider) {
_serializationConfig = _serializationConfig.withFilters(filterProvider);
}
/**
* Method that will configure default {@link Base64Variant} that
* byte[]
serializers and deserializers will use.
*
* @param v Base64 variant to use
*
* @return This mapper, for convenience to allow chaining
*
* @since 2.1
*/
public ObjectMapper setBase64Variant(Base64Variant v) {
_serializationConfig = _serializationConfig.with(v);
_deserializationConfig = _deserializationConfig.with(v);
return this;
}
/**
* Method that allows overriding of the underlying {@link SerializationConfig}
* object, which contains serialization-specific configuration settings.
* It is added as a fallback method that may be used if no other configuration
* modifier method works: it should not be used if there are alternatives,
* and its use is generally discouraged.
*
* NOTE : only use this method if you know what you are doing -- it allows
* by-passing some of checks applied to other configuration methods.
* Also keep in mind that as with all configuration of {@link ObjectMapper},
* this is only thread-safe if done before calling any serialization methods.
*
* @since 2.4
*/
public ObjectMapper setConfig(SerializationConfig config) {
_serializationConfig = config;
return this;
}
/*
/**********************************************************
/* Configuration, other
/**********************************************************
*/
/**
* Method that can be used to get hold of {@link JsonFactory} that this
* mapper uses if it needs to construct {@link JsonParser}s
* and/or {@link JsonGenerator}s.
*
* @return {@link JsonFactory} that this mapper uses when it needs to
* construct Json parser and generators
*/
@Override
public JsonFactory getFactory() { return _jsonFactory; }
/**
* @deprecated Since 2.1: Use {@link #getFactory} instead
*/
@Deprecated
@Override
public JsonFactory getJsonFactory() { return getFactory(); }
/**
* Method for configuring the default {@link DateFormat} to use when serializing time
* values as Strings, and deserializing from JSON Strings.
* This is preferably to directly modifying {@link SerializationConfig} and
* {@link DeserializationConfig} instances.
* If you need per-request configuration, use {@link #writer(DateFormat)} to
* create properly configured {@link ObjectWriter} and use that; this because
* {@link ObjectWriter}s are thread-safe whereas ObjectMapper itself is only
* thread-safe when configuring methods (such as this one) are NOT called.
*/
public ObjectMapper setDateFormat(DateFormat dateFormat)
{
_deserializationConfig = _deserializationConfig.with(dateFormat);
_serializationConfig = _serializationConfig.with(dateFormat);
return this;
}
/**
* Method for configuring {@link HandlerInstantiator} to use for creating
* instances of handlers (such as serializers, deserializers, type and type
* id resolvers), given a class.
*
* @param hi Instantiator to use; if null, use the default implementation
*/
public Object setHandlerInstantiator(HandlerInstantiator hi)
{
_deserializationConfig = _deserializationConfig.with(hi);
_serializationConfig = _serializationConfig.with(hi);
return this;
}
/**
* Method for configuring {@link InjectableValues} which used to find
* values to inject.
*/
public ObjectMapper setInjectableValues(InjectableValues injectableValues) {
_injectableValues = injectableValues;
return this;
}
/**
* Method for overriding default locale to use for formatting.
* Default value used is {@link Locale#getDefault()}.
*/
public ObjectMapper setLocale(Locale l) {
_deserializationConfig = _deserializationConfig.with(l);
_serializationConfig = _serializationConfig.with(l);
return this;
}
/**
* Method for overriding default TimeZone to use for formatting.
* Default value used is UTC (NOT local timezone).
*/
public ObjectMapper setTimeZone(TimeZone tz) {
_deserializationConfig = _deserializationConfig.with(tz);
_serializationConfig = _serializationConfig.with(tz);
return this;
}
/*
/**********************************************************
/* Configuration, simple features
/**********************************************************
*/
/**
* Method for changing state of an on/off mapper feature for
* this mapper instance.
*/
public ObjectMapper configure(MapperFeature f, boolean state) {
_serializationConfig = state ?
_serializationConfig.with(f) : _serializationConfig.without(f);
_deserializationConfig = state ?
_deserializationConfig.with(f) : _deserializationConfig.without(f);
return this;
}
/**
* Method for changing state of an on/off serialization feature for
* this object mapper.
*/
public ObjectMapper configure(SerializationFeature f, boolean state) {
_serializationConfig = state ?
_serializationConfig.with(f) : _serializationConfig.without(f);
return this;
}
/**
* Method for changing state of an on/off deserialization feature for
* this object mapper.
*/
public ObjectMapper configure(DeserializationFeature f, boolean state) {
_deserializationConfig = state ?
_deserializationConfig.with(f) : _deserializationConfig.without(f);
return this;
}
/**
* Method for changing state of an on/off {@link JsonParser} feature for
* {@link JsonFactory} instance this object mapper uses.
*
* This is method is basically a shortcut method for calling
* {@link JsonFactory#enable} on the shared
* {@link JsonFactory} this mapper uses (which is accessible
* using {@link #getJsonFactory}).
*/
public ObjectMapper configure(JsonParser.Feature f, boolean state) {
_jsonFactory.configure(f, state);
return this;
}
/**
* Method for changing state of an on/off {@link JsonGenerator} feature for
* {@link JsonFactory} instance this object mapper uses.
*
* This is method is basically a shortcut method for calling
* {@link JsonFactory#enable} on the shared
* {@link JsonFactory} this mapper uses (which is accessible
* using {@link #getJsonFactory}).
*/
public ObjectMapper configure(JsonGenerator.Feature f, boolean state) {
_jsonFactory.configure(f, state);
return this;
}
/**
* Method for enabling specified {@link MapperConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper enable(MapperFeature... f) {
_deserializationConfig = _deserializationConfig.with(f);
_serializationConfig = _serializationConfig.with(f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper disable(MapperFeature... f) {
_deserializationConfig = _deserializationConfig.without(f);
_serializationConfig = _serializationConfig.without(f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper enable(DeserializationFeature feature) {
_deserializationConfig = _deserializationConfig.with(feature);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper enable(DeserializationFeature first,
DeserializationFeature... f) {
_deserializationConfig = _deserializationConfig.with(first, f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper disable(DeserializationFeature feature) {
_deserializationConfig = _deserializationConfig.without(feature);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper disable(DeserializationFeature first,
DeserializationFeature... f) {
_deserializationConfig = _deserializationConfig.without(first, f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} feature.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper enable(SerializationFeature f) {
_serializationConfig = _serializationConfig.with(f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper enable(SerializationFeature first,
SerializationFeature... f) {
_serializationConfig = _serializationConfig.with(first, f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper disable(SerializationFeature f) {
_serializationConfig = _serializationConfig.without(f);
return this;
}
/**
* Method for enabling specified {@link DeserializationConfig} features.
* Modifies and returns this instance; no new object is created.
*/
public ObjectMapper disable(SerializationFeature first,
SerializationFeature... f) {
_serializationConfig = _serializationConfig.without(first, f);
return this;
}
/**
* Method for checking whether given Mapper
* feature is enabled.
*/
public boolean isEnabled(MapperFeature f) {
// ok to use either one, should be kept in sync
return _serializationConfig.isEnabled(f);
}
/**
* Method for checking whether given serialization-specific
* feature is enabled.
*/
public boolean isEnabled(SerializationFeature f) {
return _serializationConfig.isEnabled(f);
}
/**
* Method for checking whether given deserialization-specific
* feature is enabled.
*/
public boolean isEnabled(DeserializationFeature f) {
return _deserializationConfig.isEnabled(f);
}
/**
* Convenience method, equivalent to:
*
* getJsonFactory().isEnabled(f);
*
*/
public boolean isEnabled(JsonFactory.Feature f) {
return _jsonFactory.isEnabled(f);
}
/**
* Convenience method, equivalent to:
*
* getJsonFactory().isEnabled(f);
*
*/
public boolean isEnabled(JsonParser.Feature f) {
return _jsonFactory.isEnabled(f);
}
/**
* Convenience method, equivalent to:
*
* getJsonFactory().isEnabled(f);
*
*/
public boolean isEnabled(JsonGenerator.Feature f) {
return _jsonFactory.isEnabled(f);
}
/**
* Method that can be used to get hold of {@link JsonNodeFactory}
* that this mapper will use when directly constructing
* root {@link JsonNode} instances for Trees.
*
* Note: this is just a shortcut for calling
*
* getDeserializationConfig().getNodeFactory()
*
*/
public JsonNodeFactory getNodeFactory() {
return _deserializationConfig.getNodeFactory();
}
/*
/**********************************************************
/* Public API (from ObjectCodec): deserialization
/* (mapping from JSON to Java types);
/* main methods
/**********************************************************
*/
/**
* Method to deserialize JSON content into a non-container
* type (it can be an array type, however): typically a bean, array
* or a wrapper type (like {@link java.lang.Boolean}).
*
* Note: this method should NOT be used if the result type is a
* container ({@link java.util.Collection} or {@link java.util.Map}.
* The reason is that due to type erasure, key and value types
* can not be introspected when using this method.
*/
@Override
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), jp, _typeFactory.constructType(valueType));
}
/**
* Method to deserialize JSON content into a Java type, reference
* to which is passed as argument. Type is passed using so-called
* "super type token" (see )
* and specifically needs to be used if the root type is a
* parameterized (generic) container type.
*/
@Override
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp, TypeReference> valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), jp, _typeFactory.constructType(valueTypeRef));
}
/**
* Method to deserialize JSON content into a Java type, reference
* to which is passed as argument. Type is passed using
* Jackson specific type; instance of which can be constructed using
* {@link TypeFactory}.
*/
@Override
@SuppressWarnings("unchecked")
public final T readValue(JsonParser jp, ResolvedType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), jp, (JavaType) valueType);
}
/**
* Type-safe overloaded method, basically alias for {@link #readValue(JsonParser, ResolvedType)}.
*/
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readValue(getDeserializationConfig(), jp, valueType);
}
/**
* Method to deserialize JSON content as tree expressed
* using set of {@link JsonNode} instances. Returns
* root of the resulting tree (where root can consist
* of just a single node if the current event is a
* value event, not container).
*/
@Override
public T readTree(JsonParser jp)
throws IOException, JsonProcessingException
{
/* 02-Mar-2009, tatu: One twist; deserialization provider
* will map JSON null straight into Java null. But what
* we want to return is the "null node" instead.
*/
/* 05-Aug-2011, tatu: Also, must check for EOF here before
* calling readValue(), since that'll choke on it otherwise
*/
DeserializationConfig cfg = getDeserializationConfig();
JsonToken t = jp.getCurrentToken();
if (t == null) {
t = jp.nextToken();
if (t == null) {
return null;
}
}
JsonNode n = (JsonNode) _readValue(cfg, jp, JSON_NODE_TYPE);
if (n == null) {
n = getNodeFactory().nullNode();
}
@SuppressWarnings("unchecked")
T result = (T) n;
return result;
}
/**
* Method for reading sequence of Objects from parser stream.
* Sequence can be either root-level "unwrapped" sequence (without surrounding
* JSON array), or a sequence contained in a JSON Array.
* In either case {@link JsonParser} must point to the first token of
* the first element, OR not point to any token (in which case it is advanced
* to the next token). This means, specifically, that for wrapped sequences,
* parser MUST NOT point to the surrounding START_ARRAY
but rather
* to the token following it.
*
* Note that {@link ObjectReader} has more complete set of variants.
*/
@Override
public MappingIterator readValues(JsonParser jp, ResolvedType valueType)
throws IOException, JsonProcessingException
{
return readValues(jp, (JavaType) valueType);
}
/**
* Type-safe overloaded method, basically alias for {@link #readValues(JsonParser, ResolvedType)}.
*/
public MappingIterator readValues(JsonParser jp, JavaType valueType)
throws IOException, JsonProcessingException
{
DeserializationConfig config = getDeserializationConfig();
DeserializationContext ctxt = createDeserializationContext(jp, config);
JsonDeserializer> deser = _findRootDeserializer(ctxt, valueType);
// false -> do NOT close JsonParser (since caller passed it)
return new MappingIterator(valueType, jp, ctxt, deser,
false, null);
}
/**
* Type-safe overloaded method, basically alias for {@link #readValues(JsonParser, ResolvedType)}.
*/
@Override
public MappingIterator readValues(JsonParser jp, Class valueType)
throws IOException, JsonProcessingException
{
return readValues(jp, _typeFactory.constructType(valueType));
}
/**
* Method for reading sequence of Objects from parser stream.
*/
@Override
public MappingIterator readValues(JsonParser jp, TypeReference> valueTypeRef)
throws IOException, JsonProcessingException
{
return readValues(jp, _typeFactory.constructType(valueTypeRef));
}
/*
/**********************************************************
/* Public API not included in ObjectCodec: deserialization
/* (mapping from JSON to Java types)
/**********************************************************
*/
/**
* Method to deserialize JSON content as tree expressed
* using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist
* of just a single node if the current event is a
* value event, not container).
*
* @param in Input stream used to read JSON content
* for building the JSON tree.
*/
public JsonNode readTree(InputStream in)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(in), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/**
* Method to deserialize JSON content as tree expressed
* using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist
* of just a single node if the current event is a
* value event, not container).
*
* @param r Reader used to read JSON content
* for building the JSON tree.
*/
public JsonNode readTree(Reader r)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(r), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/**
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist of just a single node if the current
* event is a value event, not container).
*
* @param content JSON content to parse to build the JSON tree.
*/
public JsonNode readTree(String content)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(content), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/**
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist of just a single node if the current
* event is a value event, not container).
*
* @param content JSON content to parse to build the JSON tree.
*/
public JsonNode readTree(byte[] content)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(content), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/**
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist of just a single node if the current
* event is a value event, not container).
*
* @param file File of which contents to parse as JSON for building a tree instance
*/
public JsonNode readTree(File file)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(file), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/**
* Method to deserialize JSON content as tree expressed using set of {@link JsonNode} instances.
* Returns root of the resulting tree (where root can consist of just a single node if the current
* event is a value event, not container).
*
* @param source URL to use for fetching contents to parse as JSON for building a tree instance
*/
public JsonNode readTree(URL source)
throws IOException, JsonProcessingException
{
JsonNode n = (JsonNode) _readMapAndClose(_jsonFactory.createParser(source), JSON_NODE_TYPE);
return (n == null) ? NullNode.instance : n;
}
/*
/**********************************************************
/* Public API (from ObjectCodec): serialization
/* (mapping from Java types to Json)
/**********************************************************
*/
/**
* Method that can be used to serialize any Java value as
* JSON output, using provided {@link JsonGenerator}.
*/
@Override
public void writeValue(JsonGenerator jgen, Object value)
throws IOException, JsonGenerationException, JsonMappingException
{
SerializationConfig config = getSerializationConfig();
// 10-Aug-2012, tatu: as per [Issue#12], must handle indentation:
if (config.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
}
if (config.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
_writeCloseableValue(jgen, value, config);
} else {
_serializerProvider(config).serializeValue(jgen, value);
if (config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
jgen.flush();
}
}
}
/*
/**********************************************************
/* Public API (from TreeCodec via ObjectCodec): Tree Model support
/**********************************************************
*/
@Override
public void writeTree(JsonGenerator jgen, TreeNode rootNode)
throws IOException, JsonProcessingException
{
SerializationConfig config = getSerializationConfig();
_serializerProvider(config).serializeValue(jgen, rootNode);
if (config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
jgen.flush();
}
}
/**
* Method to serialize given JSON Tree, using generator
* provided.
*/
public void writeTree(JsonGenerator jgen, JsonNode rootNode)
throws IOException, JsonProcessingException
{
SerializationConfig config = getSerializationConfig();
_serializerProvider(config).serializeValue(jgen, rootNode);
if (config.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
jgen.flush();
}
}
/**
*
* Note: return type is co-variant, as basic ObjectCodec
* abstraction can not refer to concrete node types (as it's
* part of core package, whereas impls are part of mapper
* package)
*/
@Override
public ObjectNode createObjectNode() {
return _deserializationConfig.getNodeFactory().objectNode();
}
/**
*
* Note: return type is co-variant, as basic ObjectCodec
* abstraction can not refer to concrete node types (as it's
* part of core package, whereas impls are part of mapper
* package)
*/
@Override
public ArrayNode createArrayNode() {
return _deserializationConfig.getNodeFactory().arrayNode();
}
/**
* Method for constructing a {@link JsonParser} out of JSON tree
* representation.
*
* @param n Root node of the tree that resulting parser will read from
*/
@Override
public JsonParser treeAsTokens(TreeNode n)
{
return new TreeTraversingParser((JsonNode) n, this);
}
/**
* Convenience conversion method that will bind data given JSON tree
* contains into specific value (usually bean) type.
*
* Functionally equivalent to:
*
* objectMapper.convertValue(n, valueClass);
*
*/
@SuppressWarnings("unchecked")
@Override
public T treeToValue(TreeNode n, Class valueType)
throws JsonProcessingException
{
try {
// [Issue-11]: Simple cast when we just want to cast to, say, ObjectNode
// ... one caveat; while everything is Object.class, let's not take shortcut
if (valueType != Object.class && valueType.isAssignableFrom(n.getClass())) {
return (T) n;
}
return readValue(treeAsTokens(n), valueType);
} catch (JsonProcessingException e) {
throw e;
} catch (IOException e) { // should not occur, no real i/o...
throw new IllegalArgumentException(e.getMessage(), e);
}
}
/**
* Reverse of {@link #treeToValue}; given a value (usually bean), will
* construct equivalent JSON Tree representation. Functionally similar
* to serializing value into JSON and parsing JSON as tree, but
* more efficient.
*
* NOTE: one known difference from actual serialization is that so-called
* "raw values" are not supported -- since they are opaque sequence of
* bytes to include (which may or may not be supported by the backend)
* they can not be converted using this method. It may be possible to
* support conversions using full serialization, if raw values must be
* preserved.
*
* @param Actual node type; usually either basic {@link JsonNode} or
* {@link com.fasterxml.jackson.databind.node.ObjectNode}
* @param fromValue Bean value to convert
* @return Root node of the resulting JSON tree
*/
@SuppressWarnings({ "unchecked", "resource" })
public T valueToTree(Object fromValue)
throws IllegalArgumentException
{
if (fromValue == null) return null;
TokenBuffer buf = new TokenBuffer(this, false);
JsonNode result;
try {
writeValue(buf, fromValue);
JsonParser jp = buf.asParser();
result = readTree(jp);
jp.close();
} catch (IOException e) { // should not occur, no real i/o...
throw new IllegalArgumentException(e.getMessage(), e);
}
return (T) result;
}
/*
/**********************************************************
/* Extended Public API, accessors
/**********************************************************
*/
/**
* Method that can be called to check whether mapper thinks
* it could serialize an instance of given Class.
* Check is done
* by checking whether a serializer can be found for the type.
*
* NOTE: since this method does NOT throw exceptions, but internal
* processing may, caller usually has little information as to why
* serialization would fail.
*
* @return True if mapper can find a serializer for instances of
* given class (potentially serializable), false otherwise (not
* serializable)
*/
public boolean canSerialize(Class> type) {
return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, null);
}
/**
* Method similar to {@link #canSerialize(Class)} but that can return
* actual {@link Throwable} that was thrown when trying to construct
* serializer: this may be useful in figuring out what the actual problem is.
*
* @since 2.3
*/
public boolean canSerialize(Class> type, AtomicReference cause) {
return _serializerProvider(getSerializationConfig()).hasSerializerFor(type, cause);
}
/**
* Method that can be called to check whether mapper thinks
* it could deserialize an Object of given type.
* Check is done
* by checking whether a deserializer can be found for the type.
*
* @return True if mapper can find a serializer for instances of
* given class (potentially serializable), false otherwise (not
* serializable)
*/
public boolean canDeserialize(JavaType type)
{
return createDeserializationContext(null,
getDeserializationConfig()).hasValueDeserializerFor(type, null);
}
/**
* Method similar to {@link #canDeserialize(JavaType)} but that can return
* actual {@link Throwable} that was thrown when trying to construct
* serializer: this may be useful in figuring out what the actual problem is.
*
* @since 2.3
*/
public boolean canDeserialize(JavaType type, AtomicReference cause)
{
return createDeserializationContext(null,
getDeserializationConfig()).hasValueDeserializerFor(type, cause);
}
/*
/**********************************************************
/* Extended Public API, deserialization,
/* convenience methods
/**********************************************************
*/
@SuppressWarnings("unchecked")
public T readValue(File src, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(File src, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(File src, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(URL src, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(URL src, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(URL src, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(String content, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(String content, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(String content, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(content), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(Reader src, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(Reader src, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(Reader src, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(InputStream src, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(InputStream src, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(InputStream src, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(byte[] src, Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueType));
}
@SuppressWarnings("unchecked")
public T readValue(byte[] src, int offset, int len,
Class valueType)
throws IOException, JsonParseException, JsonMappingException
{
// !!! TODO
// _setupClassLoaderForDeserialization(valueType);
return (T) _readMapAndClose(_jsonFactory.createParser(src, offset, len), _typeFactory.constructType(valueType));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(byte[] src, TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public T readValue(byte[] src, int offset, int len,
TypeReference valueTypeRef)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src, offset, len), _typeFactory.constructType(valueTypeRef));
}
@SuppressWarnings("unchecked")
public T readValue(byte[] src, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src), valueType);
}
@SuppressWarnings("unchecked")
public T readValue(byte[] src, int offset, int len,
JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
return (T) _readMapAndClose(_jsonFactory.createParser(src, offset, len), valueType);
}
/*
/**********************************************************
/* Extended Public API: serialization
/* (mapping from Java types to JSON)
/**********************************************************
*/
/**
* Method that can be used to serialize any Java value as
* JSON output, written to File provided.
*/
public void writeValue(File resultFile, Object value)
throws IOException, JsonGenerationException, JsonMappingException
{
_configAndWriteValue(_jsonFactory.createGenerator(resultFile, JsonEncoding.UTF8), value);
}
/**
* Method that can be used to serialize any Java value as
* JSON output, using output stream provided (using encoding
* {@link JsonEncoding#UTF8}).
*
* Note: method does not close the underlying stream explicitly
* here; however, {@link JsonFactory} this mapper uses may choose
* to close the stream depending on its settings (by default,
* it will try to close it when {@link JsonGenerator} we construct
* is closed).
*/
public void writeValue(OutputStream out, Object value)
throws IOException, JsonGenerationException, JsonMappingException
{
_configAndWriteValue(_jsonFactory.createGenerator(out, JsonEncoding.UTF8), value);
}
/**
* Method that can be used to serialize any Java value as
* JSON output, using Writer provided.
*
* Note: method does not close the underlying stream explicitly
* here; however, {@link JsonFactory} this mapper uses may choose
* to close the stream depending on its settings (by default,
* it will try to close it when {@link JsonGenerator} we construct
* is closed).
*/
public void writeValue(Writer w, Object value)
throws IOException, JsonGenerationException, JsonMappingException
{
_configAndWriteValue(_jsonFactory.createGenerator(w), value);
}
/**
* Method that can be used to serialize any Java value as
* a String. Functionally equivalent to calling
* {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter}
* and constructing String, but more efficient.
*
* Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
@SuppressWarnings("resource")
public String writeValueAsString(Object value)
throws JsonProcessingException
{
// alas, we have to pull the recycler directly here...
SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
try {
_configAndWriteValue(_jsonFactory.createGenerator(sw), value);
} catch (JsonProcessingException e) { // to support [JACKSON-758]
throw e;
} catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
throw JsonMappingException.fromUnexpectedIOE(e);
}
return sw.getAndClear();
}
/**
* Method that can be used to serialize any Java value as
* a byte array. Functionally equivalent to calling
* {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream}
* and getting bytes, but more efficient.
* Encoding used will be UTF-8.
*
* Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
@SuppressWarnings("resource")
public byte[] writeValueAsBytes(Object value)
throws JsonProcessingException
{
ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
try {
_configAndWriteValue(_jsonFactory.createGenerator(bb, JsonEncoding.UTF8), value);
} catch (JsonProcessingException e) { // to support [JACKSON-758]
throw e;
} catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
throw JsonMappingException.fromUnexpectedIOE(e);
}
byte[] result = bb.toByteArray();
bb.release();
return result;
}
/*
/**********************************************************
/* Extended Public API: constructing ObjectWriters
/* for more advanced configuration
/**********************************************************
*/
/**
* Convenience method for constructing {@link ObjectWriter}
* with default settings.
*/
public ObjectWriter writer() {
return new ObjectWriter(this, getSerializationConfig());
}
/**
* Factory method for constructing {@link ObjectWriter} with
* specified feature enabled (compared to settings that this
* mapper instance has).
*/
public ObjectWriter writer(SerializationFeature feature) {
return new ObjectWriter(this, getSerializationConfig().with(feature));
}
/**
* Factory method for constructing {@link ObjectWriter} with
* specified features enabled (compared to settings that this
* mapper instance has).
*/
public ObjectWriter writer(SerializationFeature first,
SerializationFeature... other) {
return new ObjectWriter(this, getSerializationConfig().with(first, other));
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified {@link DateFormat}; or, if
* null passed, using timestamp (64-bit number.
*/
public ObjectWriter writer(DateFormat df) {
return new ObjectWriter(this, getSerializationConfig().with(df));
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified JSON View (filter).
*/
public ObjectWriter writerWithView(Class> serializationView) {
return new ObjectWriter(this, getSerializationConfig().withView(serializationView));
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified root type, instead of actual
* runtime type of value. Type must be a super-type of runtime
* type.
*/
public ObjectWriter writerWithType(Class> rootType) {
return new ObjectWriter(this, getSerializationConfig(),
// 15-Mar-2013, tatu: Important! Indicate that static typing is needed:
((rootType == null) ? null :_typeFactory.constructType(rootType)),
/*PrettyPrinter*/null);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified root type, instead of actual
* runtime type of value. Type must be a super-type of runtime type.
*/
public ObjectWriter writerWithType(TypeReference> rootType) {
return new ObjectWriter(this, getSerializationConfig(),
// 15-Mar-2013, tatu: Important! Indicate that static typing is needed:
((rootType == null) ? null : _typeFactory.constructType(rootType)),
/*PrettyPrinter*/null);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified root type, instead of actual
* runtime type of value. Type must be a super-type of runtime type.
*/
public ObjectWriter writerWithType(JavaType rootType) {
return new ObjectWriter(this, getSerializationConfig(), rootType, /*PrettyPrinter*/null);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified pretty printer for indentation
* (or if null, no pretty printer)
*/
public ObjectWriter writer(PrettyPrinter pp) {
if (pp == null) { // need to use a marker to indicate explicit disabling of pp
pp = ObjectWriter.NULL_PRETTY_PRINTER;
}
return new ObjectWriter(this, getSerializationConfig(), /*root type*/ null, pp);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using the default pretty printer for indentation
*/
public ObjectWriter writerWithDefaultPrettyPrinter() {
return new ObjectWriter(this, getSerializationConfig(),
/*root type*/ null, _defaultPrettyPrinter());
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* serialize objects using specified filter provider.
*/
public ObjectWriter writer(FilterProvider filterProvider) {
return new ObjectWriter(this,
getSerializationConfig().withFilters(filterProvider));
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* pass specific schema object to {@link JsonGenerator} used for
* writing content.
*
* @param schema Schema to pass to generator
*/
public ObjectWriter writer(FormatSchema schema) {
_verifySchemaType(schema);
return new ObjectWriter(this, getSerializationConfig(), schema);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* use specified Base64 encoding variant for Base64-encoded binary data.
*
* @since 2.1
*/
public ObjectWriter writer(Base64Variant defaultBase64) {
return new ObjectWriter(this, getSerializationConfig().with(defaultBase64));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* use specified character escaping details for output.
*
* @since 2.3
*/
public ObjectWriter writer(CharacterEscapes escapes) {
return writer().with(escapes);
}
/**
* Factory method for constructing {@link ObjectWriter} that will
* use specified default attributes.
*
* @since 2.3
*/
public ObjectWriter writer(ContextAttributes attrs) {
return new ObjectWriter(this, getSerializationConfig().with(attrs));
}
/*
/**********************************************************
/* Extended Public API: constructing ObjectReaders
/* for more advanced configuration
/**********************************************************
*/
/**
* Factory method for constructing {@link ObjectReader} with
* default settings. Note that the resulting instance is NOT usable as is,
* without defining expected value type.
*/
public ObjectReader reader() {
return new ObjectReader(this, getDeserializationConfig())
.with(_injectableValues);
}
/**
* Factory method for constructing {@link ObjectReader} with
* specified feature enabled (compared to settings that this
* mapper instance has).
* Note that the resulting instance is NOT usable as is,
* without defining expected value type.
*/
public ObjectReader reader(DeserializationFeature feature) {
return new ObjectReader(this, getDeserializationConfig().with(feature));
}
/**
* Factory method for constructing {@link ObjectReader} with
* specified features enabled (compared to settings that this
* mapper instance has).
* Note that the resulting instance is NOT usable as is,
* without defining expected value type.
*/
public ObjectReader reader(DeserializationFeature first,
DeserializationFeature... other) {
return new ObjectReader(this, getDeserializationConfig().with(first, other));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* update given Object (usually Bean, but can be a Collection or Map
* as well, but NOT an array) with JSON data. Deserialization occurs
* normally except that the root-level value in JSON is not used for
* instantiating a new object; instead give updateable object is used
* as root.
* Runtime type of value object is used for locating deserializer,
* unless overridden by other factory methods of {@link ObjectReader}
*/
public ObjectReader readerForUpdating(Object valueToUpdate)
{
JavaType t = _typeFactory.constructType(valueToUpdate.getClass());
return new ObjectReader(this, getDeserializationConfig(), t, valueToUpdate,
null, _injectableValues);
}
/**
* Factory method for constructing {@link ObjectReader} that will
* read or update instances of specified type
*/
public ObjectReader reader(JavaType type)
{
return new ObjectReader(this, getDeserializationConfig(), type, null,
null, _injectableValues);
}
/**
* Factory method for constructing {@link ObjectReader} that will
* read or update instances of specified type
*/
public ObjectReader reader(Class> type)
{
return reader(_typeFactory.constructType(type));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* read or update instances of specified type
*/
public ObjectReader reader(TypeReference> type)
{
return reader(_typeFactory.constructType(type));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* use specified {@link JsonNodeFactory} for constructing JSON trees.
*/
public ObjectReader reader(JsonNodeFactory f)
{
return new ObjectReader(this, getDeserializationConfig()).with(f);
}
/**
* Factory method for constructing {@link ObjectReader} that will
* pass specific schema object to {@link JsonParser} used for
* reading content.
*
* @param schema Schema to pass to parser
*/
public ObjectReader reader(FormatSchema schema) {
_verifySchemaType(schema);
return new ObjectReader(this, getDeserializationConfig(), null, null,
schema, _injectableValues);
}
/**
* Factory method for constructing {@link ObjectReader} that will
* use specified injectable values.
*
* @param injectableValues Injectable values to use
*/
public ObjectReader reader(InjectableValues injectableValues) {
return new ObjectReader(this, getDeserializationConfig(), null, null,
null, injectableValues);
}
/**
* Factory method for constructing {@link ObjectReader} that will
* deserialize objects using specified JSON View (filter).
*/
public ObjectReader readerWithView(Class> view) {
return new ObjectReader(this, getDeserializationConfig().withView(view));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* use specified Base64 encoding variant for Base64-encoded binary data.
*
* @since 2.1
*/
public ObjectReader reader(Base64Variant defaultBase64) {
return new ObjectReader(this, getDeserializationConfig().with(defaultBase64));
}
/**
* Factory method for constructing {@link ObjectReader} that will
* use specified default attributes.
*
* @since 2.3
*/
public ObjectReader reader(ContextAttributes attrs) {
return new ObjectReader(this, getDeserializationConfig().with(attrs));
}
/*
/**********************************************************
/* Extended Public API: convenience type conversion
/**********************************************************
*/
/**
* Convenience method for doing two-step conversion from given value, into
* instance of given value type. This is functionality equivalent to first
* serializing given value into JSON, then binding JSON data into value
* of given type, but may be executed without fully serializing into
* JSON. Same converters (serializers, deserializers) will be used as for
* data binding, meaning same object mapper configuration works.
*
* @throws IllegalArgumentException If conversion fails due to incompatible type;
* if so, root cause will contain underlying checked exception data binding
* functionality threw
*/
@SuppressWarnings("unchecked")
public T convertValue(Object fromValue, Class toValueType)
throws IllegalArgumentException
{
// sanity check for null first:
if (fromValue == null) return null;
return (T) _convert(fromValue, _typeFactory.constructType(toValueType));
}
@SuppressWarnings("unchecked")
public T convertValue(Object fromValue, TypeReference> toValueTypeRef)
throws IllegalArgumentException
{
return (T) convertValue(fromValue, _typeFactory.constructType(toValueTypeRef));
}
@SuppressWarnings("unchecked")
public T convertValue(Object fromValue, JavaType toValueType)
throws IllegalArgumentException
{
// sanity check for null first:
if (fromValue == null) return null;
return (T) _convert(fromValue, toValueType);
}
/**
* Actual conversion implementation: instead of using existing read
* and write methods, much of code is inlined. Reason for this is
* that we must avoid root value wrapping/unwrapping both for efficiency and
* for correctness. If root value wrapping/unwrapping is actually desired,
* caller must use explicit writeValue
and
* readValue
methods.
*/
@SuppressWarnings("resource")
protected Object _convert(Object fromValue, JavaType toValueType)
throws IllegalArgumentException
{
// also, as per [Issue-11], consider case for simple cast
/* But with caveats: one is that while everything is Object.class, we don't
* want to "optimize" that out; and the other is that we also do not want
* to lose conversions of generic types.
*/
Class> targetType = toValueType.getRawClass();
if (targetType != Object.class
&& !toValueType.hasGenericTypes()
&& targetType.isAssignableFrom(fromValue.getClass())) {
return fromValue;
}
/* Then use TokenBuffer, which is a JsonGenerator:
* (see [JACKSON-175])
*/
TokenBuffer buf = new TokenBuffer(this, false);
try {
// inlined 'writeValue' with minor changes:
// first: disable wrapping when writing
SerializationConfig config = getSerializationConfig().without(SerializationFeature.WRAP_ROOT_VALUE);
// no need to check for closing of TokenBuffer
_serializerProvider(config).serializeValue(buf, fromValue);
// then matching read, inlined 'readValue' with minor mods:
final JsonParser jp = buf.asParser();
Object result;
// ok to pass in existing feature flags; unwrapping handled by mapper
final DeserializationConfig deserConfig = getDeserializationConfig();
JsonToken t = _initForReading(jp);
if (t == JsonToken.VALUE_NULL) {
DeserializationContext ctxt = createDeserializationContext(jp, deserConfig);
result = _findRootDeserializer(ctxt, toValueType).getNullValue();
} else if (t == JsonToken.END_ARRAY || t == JsonToken.END_OBJECT) {
result = null;
} else { // pointing to event other than null
DeserializationContext ctxt = createDeserializationContext(jp, deserConfig);
JsonDeserializer deser = _findRootDeserializer(ctxt, toValueType);
// note: no handling of unwarpping
result = deser.deserialize(jp, ctxt);
}
jp.close();
return result;
} catch (IOException e) { // should not occur, no real i/o...
throw new IllegalArgumentException(e.getMessage(), e);
}
}
/*
/**********************************************************
/* Extended Public API: JSON Schema generation
/**********************************************************
*/
/**
* Generate Json-schema
* instance for specified class.
*
* @param t The class to generate schema for
* @return Constructed JSON schema.
*/
@SuppressWarnings("deprecation")
public com.fasterxml.jackson.databind.jsonschema.JsonSchema generateJsonSchema(Class> t)
throws JsonMappingException {
return _serializerProvider(getSerializationConfig()).generateJsonSchema(t);
}
/**
* Method for visiting type hierarchy for given type, using specified visitor.
*
* This method can be used for things like
* generating Json Schema
* instance for specified type.
*
* @param type Type to generate schema for (possibly with generic signature)
*
* @since 2.1
*/
public void acceptJsonFormatVisitor(Class> type, JsonFormatVisitorWrapper visitor)
throws JsonMappingException
{
acceptJsonFormatVisitor(_typeFactory.constructType(type), visitor);
}
/**
* Method for visiting type hierarchy for given type, using specified visitor.
* Visitation uses Serializer
hierarchy and related properties
*
* This method can be used for things like
* generating Json Schema
* instance for specified type.
*
* @param type Type to generate schema for (possibly with generic signature)
*
* @since 2.1
*/
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor)
throws JsonMappingException
{
if (type == null) {
throw new IllegalArgumentException("type must be provided");
}
_serializerProvider(getSerializationConfig()).acceptJsonFormatVisitor(type, visitor);
}
/*
/**********************************************************
/* Internal methods for serialization, overridable
/**********************************************************
*/
/**
* Overridable helper method used for constructing
* {@link SerializerProvider} to use for serialization.
*/
protected DefaultSerializerProvider _serializerProvider(SerializationConfig config) {
return _serializerProvider.createInstance(config, _serializerFactory);
}
/**
* Helper method that should return default pretty-printer to
* use for generators constructed by this mapper, when instructed
* to use default pretty printer.
*/
protected PrettyPrinter _defaultPrettyPrinter() {
return _defaultPrettyPrinter;
}
/**
* Method called to configure the generator as necessary and then
* call write functionality
*/
protected final void _configAndWriteValue(JsonGenerator jgen, Object value)
throws IOException, JsonGenerationException, JsonMappingException
{
SerializationConfig cfg = getSerializationConfig();
// [JACKSON-96]: allow enabling pretty printing for ObjectMapper directly
if (cfg.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
}
// [Issue#232]
if (cfg.isEnabled(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN)) {
jgen.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
}
// [JACKSON-282]: consider Closeable
if (cfg.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
_configAndWriteCloseable(jgen, value, cfg);
return;
}
boolean closed = false;
try {
_serializerProvider(cfg).serializeValue(jgen, value);
closed = true;
jgen.close();
} finally {
/* won't try to close twice; also, must catch exception (so it
* will not mask exception that is pending)
*/
if (!closed) {
/* 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
* structures, which typically causes more damage.
*/
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
jgen.close();
} catch (IOException ioe) { }
}
}
}
protected final void _configAndWriteValue(JsonGenerator jgen, Object value, Class> viewClass)
throws IOException, JsonGenerationException, JsonMappingException
{
SerializationConfig cfg = getSerializationConfig().withView(viewClass);
if (cfg.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
jgen.useDefaultPrettyPrinter();
}
// [Issue#232]
if (cfg.isEnabled(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN)) {
jgen.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
}
// [JACKSON-282]: consider Closeable
if (cfg.isEnabled(SerializationFeature.CLOSE_CLOSEABLE) && (value instanceof Closeable)) {
_configAndWriteCloseable(jgen, value, cfg);
return;
}
boolean closed = false;
try {
_serializerProvider(cfg).serializeValue(jgen, value);
closed = true;
jgen.close();
} finally {
if (!closed) {
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
// structures, which typically causes more damage.
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
jgen.close();
} catch (IOException ioe) { }
}
}
}
/**
* Helper method used when value to serialize is {@link Closeable} and its close()
* method is to be called right after serialization has been called
*/
private final void _configAndWriteCloseable(JsonGenerator jgen, Object value, SerializationConfig cfg)
throws IOException, JsonGenerationException, JsonMappingException
{
Closeable toClose = (Closeable) value;
try {
_serializerProvider(cfg).serializeValue(jgen, value);
JsonGenerator tmpJgen = jgen;
jgen = null;
tmpJgen.close();
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
/* Need to close both generator and value, as long as they haven't yet
* been closed
*/
if (jgen != null) {
// 04-Mar-2014, tatu: But! Let's try to prevent auto-closing of
// structures, which typically causes more damage.
jgen.disable(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT);
try {
jgen.close();
} catch (IOException ioe) { }
}
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
}
}
/**
* Helper method used when value to serialize is {@link Closeable} and its close()
* method is to be called right after serialization has been called
*/
private final void _writeCloseableValue(JsonGenerator jgen, Object value, SerializationConfig cfg)
throws IOException, JsonGenerationException, JsonMappingException
{
Closeable toClose = (Closeable) value;
try {
_serializerProvider(cfg).serializeValue(jgen, value);
if (cfg.isEnabled(SerializationFeature.FLUSH_AFTER_WRITE_VALUE)) {
jgen.flush();
}
Closeable tmpToClose = toClose;
toClose = null;
tmpToClose.close();
} finally {
if (toClose != null) {
try {
toClose.close();
} catch (IOException ioe) { }
}
}
}
/*
/**********************************************************
/* Internal methods for deserialization, overridable
/**********************************************************
*/
/**
* Internal helper method called to create an instance of {@link DeserializationContext}
* for deserializing a single root value.
* Can be overridden if a custom context is needed.
*/
protected DefaultDeserializationContext createDeserializationContext(JsonParser jp,
DeserializationConfig cfg)
{
return _deserializationContext.createInstance(cfg,
jp, _injectableValues);
}
/**
* Actual implementation of value reading+binding operation.
*/
protected Object _readValue(DeserializationConfig cfg, JsonParser jp, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
/* First: may need to read the next token, to initialize
* state (either before first read from parser, or after
* previous token has been cleared)
*/
Object result;
JsonToken t = _initForReading(jp);
if (t == JsonToken.VALUE_NULL) {
// [JACKSON-643]: Ask JsonDeserializer what 'null value' to use:
DeserializationContext ctxt = createDeserializationContext(jp, cfg);
result = _findRootDeserializer(ctxt, valueType).getNullValue();
} else if (t == JsonToken.END_ARRAY || t == JsonToken.END_OBJECT) {
result = null;
} else { // pointing to event other than null
DeserializationContext ctxt = createDeserializationContext(jp, cfg);
JsonDeserializer deser = _findRootDeserializer(ctxt, valueType);
// ok, let's get the value
if (cfg.useRootWrapping()) {
result = _unwrapAndDeserialize(jp, ctxt, cfg, valueType, deser);
} else {
result = deser.deserialize(jp, ctxt);
}
}
// Need to consume the token too
jp.clearCurrentToken();
return result;
}
protected Object _readMapAndClose(JsonParser jp, JavaType valueType)
throws IOException, JsonParseException, JsonMappingException
{
try {
Object result;
JsonToken t = _initForReading(jp);
if (t == JsonToken.VALUE_NULL) {
// [JACKSON-643]: Ask JsonDeserializer what 'null value' to use:
DeserializationContext ctxt = createDeserializationContext(jp,
getDeserializationConfig());
result = _findRootDeserializer(ctxt, valueType).getNullValue();
} else if (t == JsonToken.END_ARRAY || t == JsonToken.END_OBJECT) {
result = null;
} else {
DeserializationConfig cfg = getDeserializationConfig();
DeserializationContext ctxt = createDeserializationContext(jp, cfg);
JsonDeserializer deser = _findRootDeserializer(ctxt, valueType);
if (cfg.useRootWrapping()) {
result = _unwrapAndDeserialize(jp, ctxt, cfg, valueType, deser);
} else {
result = deser.deserialize(jp, ctxt);
}
ctxt.checkUnresolvedObjectId();
}
// Need to consume the token too
jp.clearCurrentToken();
return result;
} finally {
try {
jp.close();
} catch (IOException ioe) { }
}
}
/**
* Method called to ensure that given parser is ready for reading
* content for data binding.
*
* @return First token to be used for data binding after this call:
* can never be null as exception will be thrown if parser can not
* provide more tokens.
*
* @throws IOException if the underlying input source has problems during
* parsing
* @throws JsonParseException if parser has problems parsing content
* @throws JsonMappingException if the parser does not have any more
* content to map (note: Json "null" value is considered content;
* enf-of-stream not)
*/
protected JsonToken _initForReading(JsonParser jp)
throws IOException, JsonParseException, JsonMappingException
{
/* First: must point to a token; if not pointing to one, advance.
* This occurs before first read from JsonParser, as well as
* after clearing of current token.
*/
JsonToken t = jp.getCurrentToken();
if (t == null) {
// and then we must get something...
t = jp.nextToken();
if (t == null) {
/* [JACKSON-546] Throw mapping exception, since it's failure to map,
* not an actual parsing problem
*/
throw JsonMappingException.from(jp, "No content to map due to end-of-input");
}
}
return t;
}
protected Object _unwrapAndDeserialize(JsonParser jp, DeserializationContext ctxt,
DeserializationConfig config,
JavaType rootType, JsonDeserializer deser)
throws IOException, JsonParseException, JsonMappingException
{
String expName = config.getRootName();
if (expName == null) {
PropertyName pname = _rootNames.findRootName(rootType, config);
expName = pname.getSimpleName();
}
if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
throw JsonMappingException.from(jp, "Current token not START_OBJECT (needed to unwrap root name '"
+expName+"'), but "+jp.getCurrentToken());
}
if (jp.nextToken() != JsonToken.FIELD_NAME) {
throw JsonMappingException.from(jp, "Current token not FIELD_NAME (to contain expected root name '"
+expName+"'), but "+jp.getCurrentToken());
}
String actualName = jp.getCurrentName();
if (!expName.equals(actualName)) {
throw JsonMappingException.from(jp, "Root name '"+actualName+"' does not match expected ('"
+expName+"') for type "+rootType);
}
// ok, then move to value itself....
jp.nextToken();
Object result = deser.deserialize(jp, ctxt);
// and last, verify that we now get matching END_OBJECT
if (jp.nextToken() != JsonToken.END_OBJECT) {
throw JsonMappingException.from(jp, "Current token not END_OBJECT (to match wrapper object with root name '"
+expName+"'), but "+jp.getCurrentToken());
}
return result;
}
/*
/**********************************************************
/* Internal methods, other
/**********************************************************
*/
/**
* Method called to locate deserializer for the passed root-level value.
*/
protected JsonDeserializer _findRootDeserializer(DeserializationContext ctxt,
JavaType valueType)
throws JsonMappingException
{
// First: have we already seen it?
JsonDeserializer deser = _rootDeserializers.get(valueType);
if (deser != null) {
return deser;
}
// Nope: need to ask provider to resolve it
deser = ctxt.findRootValueDeserializer(valueType);
if (deser == null) { // can this happen?
throw new JsonMappingException("Can not find a deserializer for type "+valueType);
}
_rootDeserializers.put(valueType, deser);
return deser;
}
/**
* @since 2.2
*/
protected void _verifySchemaType(FormatSchema schema)
{
if (schema != null) {
if (!_jsonFactory.canUseSchema(schema)) {
throw new IllegalArgumentException("Can not use FormatSchema of type "+schema.getClass().getName()
+" for format "+_jsonFactory.getFormatName());
}
}
}
}
ObjectReader.java 0000664 0000000 0000000 00000161401 12373034547 0033652 0 ustar 00root root 0000000 0000000 jackson-databind-jackson-databind-2.4.2/src/main/java/com/fasterxml/jackson/databind package com.fasterxml.jackson.databind;
import java.io.*;
import java.net.URL;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.concurrent.ConcurrentHashMap;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.DataFormatReaders;
import com.fasterxml.jackson.databind.deser.DefaultDeserializationContext;
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.TreeTraversingParser;
import com.fasterxml.jackson.databind.type.SimpleType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.RootNameLookup;
/**
* Builder object that can be used for per-serialization configuration of
* deserialization parameters, such as root type to use or object
* to update (instead of constructing new instance).
*
* Uses "fluent" (or, kind of, builder) pattern so that instances are immutable
* (and thus fully thread-safe with no external synchronization);
* new instances are constructed for different configurations.
* Instances are initially constructed by {@link ObjectMapper} and can be
* reused, shared, cached; both because of thread-safety and because
* instances are relatively light-weight.
*/
public class ObjectReader
extends ObjectCodec
implements Versioned, java.io.Serializable // since 2.1
{
private static final long serialVersionUID = -4251443320039569153L;
private final static JavaType JSON_NODE_TYPE = SimpleType.constructUnsafe(JsonNode.class);
/*
/**********************************************************
/* Immutable configuration from ObjectMapper
/**********************************************************
*/
/**
* General serialization configuration settings; while immutable,
* can use copy-constructor to create modified instances as necessary.
*/
protected final DeserializationConfig _config;
/**
* Blueprint instance of deserialization context; used for creating
* actual instance when needed.
*/
protected final DefaultDeserializationContext _context;
/**
* Factory used for constructing {@link JsonGenerator}s
*/
protected final JsonFactory _parserFactory;
/**
* Flag that indicates whether root values are expected to be unwrapped or not
*/
protected final boolean _unwrapRoot;
/*
/**********************************************************
/* Configuration that can be changed during building
/**********************************************************
*/
/**
* Declared type of value to instantiate during deserialization.
* Defines which deserializer to use; as well as base type of instance
* to construct if an updatable value is not configured to be used
* (subject to changes by embedded type information, for polymorphic
* types). If {@link #_valueToUpdate} is non-null, only used for
* locating deserializer.
*/
protected final JavaType _valueType;
/**
* We may pre-fetch deserializer as soon as {@link #_valueType}
* is known, and if so, reuse it afterwards.
* This allows avoiding further deserializer lookups and increases
* performance a bit on cases where readers are reused.
*
* @since 2.1
*/
protected final JsonDeserializer _rootDeserializer;
/**
* Instance to update with data binding; if any. If null,
* a new instance is created, if non-null, properties of
* this value object will be updated instead.
* Note that value can be of almost any type, except not
* {@link com.fasterxml.jackson.databind.type.ArrayType}; array
* types can not be modified because array size is immutable.
*/
protected final Object _valueToUpdate;
/**
* When using data format that uses a schema, schema is passed
* to parser.
*/
protected final FormatSchema _schema;
/**
* Values that can be injected during deserialization, if any.
*/
protected final InjectableValues _injectableValues;
/**
* Optional detector used for auto-detecting data format that byte-based
* input uses.
*
* NOTE: If defined non-null, readValue()
methods that take
* {@link Reader} or {@link String} input will fail with exception ,
* because format-detection only works on byte-sources. Also, if format
* can not be detect reliably (as per detector settings),
* a {@link JsonParseException} will be thrown).
*
* @since 2.1
*/
protected final DataFormatReaders _dataFormatReaders;
/*
/**********************************************************
/* Caching
/**********************************************************
*/
/**
* Root-level cached deserializers
*/
final protected ConcurrentHashMap> _rootDeserializers;
/**
* Cache for root names used when root-wrapping is enabled.
*/
protected final RootNameLookup _rootNames;
/*
/**********************************************************
/* Life-cycle, construction
/**********************************************************
*/
/**
* Constructor used by {@link ObjectMapper} for initial instantiation
*/
protected ObjectReader(ObjectMapper mapper, DeserializationConfig config)
{
this(mapper, config, null, null, null, null);
}
/**
* Constructor called when a root deserializer should be fetched based
* on other configuration.
*/
protected ObjectReader(ObjectMapper mapper, DeserializationConfig config,
JavaType valueType, Object valueToUpdate,
FormatSchema schema, InjectableValues injectableValues)
{
_config = config;
_context = mapper._deserializationContext;
_rootDeserializers = mapper._rootDeserializers;
_parserFactory = mapper._jsonFactory;
_rootNames = mapper._rootNames;
_valueType = valueType;
_valueToUpdate = valueToUpdate;
if (valueToUpdate != null && valueType.isArrayType()) {
throw new IllegalArgumentException("Can not update an array value");
}
_schema = schema;
_injectableValues = injectableValues;
_unwrapRoot = config.useRootWrapping();
_rootDeserializer = _prefetchRootDeserializer(config, valueType);
_dataFormatReaders = null;
}
/**
* Copy constructor used for building variations.
*/
protected ObjectReader(ObjectReader base, DeserializationConfig config,
JavaType valueType, JsonDeserializer rootDeser, Object valueToUpdate,
FormatSchema schema, InjectableValues injectableValues,
DataFormatReaders dataFormatReaders)
{
_config = config;
_context = base._context;
_rootDeserializers = base._rootDeserializers;
_parserFactory = base._parserFactory;
_rootNames = base._rootNames;
_valueType = valueType;
_rootDeserializer = rootDeser;
_valueToUpdate = valueToUpdate;
if (valueToUpdate != null && valueType.isArrayType()) {
throw new IllegalArgumentException("Can not update an array value");
}
_schema = schema;
_injectableValues = injectableValues;
_unwrapRoot = config.useRootWrapping();
_dataFormatReaders = dataFormatReaders;
}
/**
* Copy constructor used when modifying simple feature flags
*/
protected ObjectReader(ObjectReader base, DeserializationConfig config)
{
_config = config;
_context = base._context;
_rootDeserializers = base._rootDeserializers;
_parserFactory = base._parserFactory;
_rootNames = base._rootNames;
_valueType = base._valueType;
_rootDeserializer = base._rootDeserializer;
_valueToUpdate = base._valueToUpdate;
_schema = base._schema;
_injectableValues = base._injectableValues;
_unwrapRoot = config.useRootWrapping();
_dataFormatReaders = base._dataFormatReaders;
}
protected ObjectReader(ObjectReader base, JsonFactory f)
{
// may need to override ordering, based on data format capabilities
_config = base._config
.with(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, f.requiresPropertyOrdering());
_context = base._context;
_rootDeserializers = base._rootDeserializers;
_parserFactory = f;
_rootNames = base._rootNames;
_valueType = base._valueType;
_rootDeserializer = base._rootDeserializer;
_valueToUpdate = base._valueToUpdate;
_schema = base._schema;
_injectableValues = base._injectableValues;
_unwrapRoot = base._unwrapRoot;
_dataFormatReaders = base._dataFormatReaders;
}
/**
* Method that will return version information stored in and read from jar
* that contains this class.
*/
@Override
public Version version() {
return com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
}
/*
/**********************************************************
/* Life-cycle, fluent factory methods
/**********************************************************
*/
public ObjectReader with(DeserializationConfig config) {
return _with(config);
}
/**
* Method for constructing a new reader instance that is configured
* with specified feature enabled.
*/
public ObjectReader with(DeserializationFeature feature) {
return _with(_config.with(feature));
}
/**
* Method for constructing a new reader instance that is configured
* with specified features enabled.
*/
public ObjectReader with(DeserializationFeature first,
DeserializationFeature... other)
{
return _with(_config.with(first, other));
}
/**
* Method for constructing a new reader instance that is configured
* with specified features enabled.
*/
public ObjectReader withFeatures(DeserializationFeature... features) {
return _with(_config.withFeatures(features));
}
/**
* Method for constructing a new reader instance that is configured
* with specified feature disabled.
*/
public ObjectReader without(DeserializationFeature feature) {
return _with(_config.without(feature));
}
/**
* Method for constructing a new reader instance that is configured
* with specified features disabled.
*/
public ObjectReader without(DeserializationFeature first,
DeserializationFeature... other)
{
return _with(_config.without(first, other));
}
/**
* Method for constructing a new reader instance that is configured
* with specified features disabled.
*/
public ObjectReader withoutFeatures(DeserializationFeature... features) {
return _with(_config.withoutFeatures(features));
}
/**
* Method for constructing a new instance with configuration that uses
* passed {@link InjectableValues} to provide injectable values.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader with(InjectableValues injectableValues)
{
if (_injectableValues == injectableValues) {
return this;
}
return new ObjectReader(this, _config,
_valueType, _rootDeserializer, _valueToUpdate,
_schema, injectableValues, _dataFormatReaders);
}
/**
* Method for constructing a new reader instance with configuration that uses
* passed {@link JsonNodeFactory} for constructing {@link JsonNode}
* instances.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader with(JsonNodeFactory f) {
return _with(_config.with(f));
}
/**
* Method for constructing a new reader instance with configuration that uses
* passed {@link JsonFactory} for constructing underlying Readers.
*
* NOTE: only factories that DO NOT REQUIRE SPECIAL MAPPERS
* (that is, ones that return false
for
* {@link JsonFactory#requiresCustomCodec()}) can be used: trying
* to use one that requires custom codec will throw exception
*
* @since 2.1
*/
public ObjectReader with(JsonFactory f) {
if (f == _parserFactory) {
return this;
}
ObjectReader r = new ObjectReader(this, f);
// Also, try re-linking, if possible...
if (f.getCodec() == null) {
f.setCodec(r);
}
return r;
}
/**
* Method for constructing a new instance with configuration that
* specifies what root name to expect for "root name unwrapping".
* See {@link DeserializationConfig#withRootName(String)} for
* details.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withRootName(String rootName) {
return _with(_config.withRootName(rootName));
}
/**
* Method for constructing a new instance with configuration that
* passes specified {@link FormatSchema} to {@link JsonParser} that
* is constructed for parsing content.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader with(FormatSchema schema)
{
if (_schema == schema) {
return this;
}
_verifySchemaType(schema);
return new ObjectReader(this, _config, _valueType, _rootDeserializer, _valueToUpdate,
schema, _injectableValues, _dataFormatReaders);
}
/**
* Method for constructing a new reader instance that is configured
* to data bind into specified type.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withType(JavaType valueType)
{
if (valueType != null && valueType.equals(_valueType)) {
return this;
}
JsonDeserializer rootDeser = _prefetchRootDeserializer(_config, valueType);
// type is stored here, no need to make a copy of config
DataFormatReaders det = _dataFormatReaders;
if (det != null) {
det = det.withType(valueType);
}
return new ObjectReader(this, _config, valueType, rootDeser,
_valueToUpdate, _schema, _injectableValues, det);
}
/**
* Method for constructing a new reader instance that is configured
* to data bind into specified type.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withType(Class> valueType) {
return withType(_config.constructType(valueType));
}
/**
* Method for constructing a new reader instance that is configured
* to data bind into specified type.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withType(java.lang.reflect.Type valueType) {
return withType(_config.getTypeFactory().constructType(valueType));
}
/**
* Method for constructing a new reader instance that is configured
* to data bind into specified type.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withType(TypeReference> valueTypeRef) {
return withType(_config.getTypeFactory().constructType(valueTypeRef.getType()));
}
/**
* Method for constructing a new instance with configuration that
* updates passed Object (as root value), instead of constructing
* a new value.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withValueToUpdate(Object value)
{
if (value == _valueToUpdate) return this;
if (value == null) {
throw new IllegalArgumentException("cat not update null value");
}
JavaType t;
/* no real benefit from pre-fetching, as updating readers are much
* less likely to be reused, and value type may also be forced
* with a later chained call...
*/
if (_valueType == null) {
t = _config.constructType(value.getClass());
} else {
t = _valueType;
}
return new ObjectReader(this, _config, t, _rootDeserializer, value,
_schema, _injectableValues, _dataFormatReaders);
}
/**
* Method for constructing a new instance with configuration that
* uses specified View for filtering.
*
* Note that the method does NOT change state of this reader, but
* rather construct and returns a newly configured instance.
*/
public ObjectReader withView(Class> activeView) {
return _with(_config.withView(activeView));
}
public ObjectReader with(Locale l) {
return _with(_config.with(l));
}
public ObjectReader with(TimeZone tz) {
return _with(_config.with(tz));
}
public ObjectReader withHandler(DeserializationProblemHandler h) {
return _with(_config.withHandler(h));
}
public ObjectReader with(Base64Variant defaultBase64) {
return _with(_config.with(defaultBase64));
}
/**
* Fluent factory method for constructing a reader that will try to
* auto-detect underlying data format, using specified list of
* {@link JsonFactory} instances, and default {@link DataFormatReaders} settings
* (for customized {@link DataFormatReaders}, you can construct instance yourself).
* to construct appropriate {@link JsonParser} for actual parsing.
*
* Note: since format detection only works with byte sources, it is possible to
* get a failure from some 'readValue()' methods. Also, if input can not be reliably
* (enough) detected as one of specified types, an exception will be thrown.
*
* Note: not all {@link JsonFactory} types can be passed: specifically, ones that
* require "custom codec" (like XML factory) will not work. Instead, use
* method that takes {@link ObjectReader} instances instead of factories.
*
* @param readers Data formats accepted, in decreasing order of priority (that is,
* matches checked in listed order, first match wins)
*
* @return Newly configured writer instance
*
* @since 2.1
*/
public ObjectReader withFormatDetection(ObjectReader... readers)
{
return withFormatDetection(new DataFormatReaders(readers));
}
/**
* Fluent factory method for constructing a reader that will try to
* auto-detect underlying data format, using specified
* {@link DataFormatReaders}.
*
* NOTE: since format detection only works with byte sources, it is possible to
* get a failure from some 'readValue()' methods. Also, if input can not be reliably
* (enough) detected as one of specified types, an exception will be thrown.
*
* @param readers DataFormatReaders to use for detecting underlying format.
*
* @return Newly configured writer instance
*
* @since 2.1
*/
public ObjectReader withFormatDetection(DataFormatReaders readers)
{
return new ObjectReader(this, _config, _valueType, _rootDeserializer, _valueToUpdate,
_schema, _injectableValues, readers);
}
/**
* @since 2.3
*/
public ObjectReader with(ContextAttributes attrs) {
DeserializationConfig newConfig = _config.with(attrs);
return (newConfig == _config) ? this : new ObjectReader(this, newConfig);
}
/**
* @since 2.3
*/
public ObjectReader withAttributes(Map attrs) {
DeserializationConfig newConfig = _config.withAttributes(attrs);
return (newConfig == _config) ? this : new ObjectReader(this, newConfig);
}
/**
* @since 2.3
*/
public ObjectReader withAttribute(Object key, Object value) {
DeserializationConfig newConfig = _config.withAttribute(key, value);
return (newConfig == _config) ? this : new ObjectReader(this, newConfig);
}
/**
* @since 2.3
*/
public ObjectReader withoutAttribute(Object key) {
DeserializationConfig newConfig = _config.withoutAttribute(key);
return (newConfig == _config) ? this : new ObjectReader(this, newConfig);
}
/*
/**********************************************************
/* Simple accessors
/**********************************************************
*/
public boolean isEnabled(DeserializationFeature f) {
return _config.isEnabled(f);
}
public boolean isEnabled(MapperFeature f) {
return _config.isEnabled(f);
}
public boolean isEnabled(JsonParser.Feature f) {
return _parserFactory.isEnabled(f);
}
/**
* @since 2.2
*/
public DeserializationConfig getConfig() {
return _config;
}
/**
* @since 2.1
*/
@Override
public JsonFactory getFactory() {
return _parserFactory;
}
/**
* @deprecated Since 2.1: Use {@link #getFactory} instead
*/
@Deprecated
@Override
public JsonFactory getJsonFactory() {
return _parserFactory;
}
public TypeFactory getTypeFactory() {
return _config.getTypeFactory();
}
/**
* @since 2.3
*/
public ContextAttributes getAttributes() {
return _config.getAttributes();
}
/*
/**********************************************************
/* Deserialization methods; basic ones to support ObjectCodec first
/* (ones that take JsonParser)
/**********************************************************
*/
/**
* Method that binds content read using given parser, using
* configuration of this reader, including expected result type.
* Value return is either newly constructed, or root value that
* was specified with {@link #withValueToUpdate(Object)}.
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp)
throws IOException, JsonProcessingException
{
return (T) _bind(jp, _valueToUpdate);
}
/**
* Convenience method that binds content read using given parser, using
* configuration of this reader, except that expected value type
* is specified with the call (instead of currently configured root type).
* Value return is either newly constructed, or root value that
* was specified with {@link #withValueToUpdate(Object)}.
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@SuppressWarnings("unchecked")
@Override
public T readValue(JsonParser jp, Class valueType)
throws IOException, JsonProcessingException
{
return (T) withType(valueType).readValue(jp);
}
/**
* Convenience method that binds content read using given parser, using
* configuration of this reader, except that expected value type
* is specified with the call (instead of currently configured root type).
* Value return is either newly constructed, or root value that
* was specified with {@link #withValueToUpdate(Object)}.
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@SuppressWarnings("unchecked")
@Override
public T readValue(JsonParser jp, TypeReference> valueTypeRef)
throws IOException, JsonProcessingException
{
return (T) withType(valueTypeRef).readValue(jp);
}
/**
* Convenience method that binds content read using given parser, using
* configuration of this reader, except that expected value type
* is specified with the call (instead of currently configured root type).
* Value return is either newly constructed, or root value that
* was specified with {@link #withValueToUpdate(Object)}.
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@Override
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp, ResolvedType valueType) throws IOException, JsonProcessingException {
return (T) withType((JavaType)valueType).readValue(jp);
}
/**
* Type-safe overloaded method, basically alias for {@link #readValue(JsonParser, ResolvedType)}.
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@SuppressWarnings("unchecked")
public T readValue(JsonParser jp, JavaType valueType) throws IOException, JsonProcessingException {
return (T) withType(valueType).readValue(jp);
}
/**
* Convenience method that is equivalent to:
*
* withType(valueType).readValues(jp);
*
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@Override
public Iterator readValues(JsonParser jp, Class valueType)
throws IOException, JsonProcessingException {
return withType(valueType).readValues(jp);
}
/**
* Convenience method that is equivalent to:
*
* withType(valueTypeRef).readValues(jp);
*
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@Override
public Iterator readValues(JsonParser jp, TypeReference> valueTypeRef)
throws IOException, JsonProcessingException {
return withType(valueTypeRef).readValues(jp);
}
/**
* Convenience method that is equivalent to:
*
* withType(valueType).readValues(jp);
*
*
* NOTE: this method never tries to auto-detect format, since actual
* (data-format specific) parser is given.
*/
@Override
public