下面列出了怎么用com.fasterxml.jackson.databind.jsontype.TypeDeserializer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JsonDeserializer<?> findMapLikeDeserializer(MapLikeType type,
DeserializationConfig config, BeanDescription beanDesc,
KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
Class<?> raw = type.getRawClass();
if (Map.class.isAssignableFrom(raw)) {
return new MapDeserializer(type, keyDeserializer, elementTypeDeserializer, elementDeserializer);
}
if (Multimap.class.isAssignableFrom(raw)) {
return new MultimapDeserializer(type, keyDeserializer, elementTypeDeserializer, elementDeserializer);
}
return super.findMapLikeDeserializer(type, config, beanDesc, keyDeserializer, elementTypeDeserializer, elementDeserializer);
}
/**
* Method called to create a type information deserializer for values of
* given non-container property, if one is needed.
* If not needed (no polymorphic handling configured for property), should return null.
*<p>
* Note that this method is only called for non-container bean properties,
* and not for values in container types or root values (or container properties)
*
* @param baseType Declared base type of the value to deserializer (actual
* deserializer type will be this type or its subtype)
*
* @return Type deserializer to use for given base type, if one is needed; null if not.
*/
public TypeDeserializer findPropertyTypeDeserializer(DeserializationConfig config,
JavaType baseType, AnnotatedMember annotated)
throws JsonMappingException
{
AnnotationIntrospector ai = config.getAnnotationIntrospector();
TypeResolverBuilder<?> b = ai.findPropertyTypeResolver(config, annotated, baseType);
// Defaulting: if no annotations on member, check value class
if (b == null) {
return findTypeDeserializer(config, baseType);
}
// but if annotations found, may need to resolve subtypes:
Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByTypeId(
config, annotated, baseType);
return b.buildTypeDeserializer(config, baseType, subtypes);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException
{
JsonDeserializer<?> deser = _valueDeserializer;
if (deser == null) {
deser = ctxt.findContextualValueDeserializer(_fullType.getReferencedType(), property);
} else { // otherwise directly assigned, probably not contextual yet:
deser = ctxt.handleSecondaryContextualization(deser, property, _fullType.getReferencedType());
}
TypeDeserializer typeDeser = _valueTypeDeserializer;
if (typeDeser != null) {
typeDeser = typeDeser.forProperty(property);
}
// !!! 23-Oct-2016, tatu: TODO: full support for configurable ValueInstantiators?
if ((deser == _valueDeserializer) && (typeDeser == _valueTypeDeserializer)) {
return this;
}
return withResolved(typeDeser, deser);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
JsonDeserializer<?> deser = _valueDeserializer;
TypeDeserializer typeDeser = _typeDeserializerForValue;
if (deser == null) {
deser = ctxt.findContextualValueDeserializer(_elementType, property);
}
if (typeDeser != null) {
typeDeser = typeDeser.forProperty(property);
}
if (deser == _valueDeserializer && typeDeser == _typeDeserializerForValue) {
return this;
}
return withResolved(typeDeser, deser);
}
protected T _deserializeContents(JsonParser p, DeserializationContext ctxt)
throws IOException {
JsonDeserializer<?> valueDes = _valueDeserializer;
JsonToken t;
final TypeDeserializer typeDeser = _typeDeserializerForValue;
// No way to pass actual type parameter; but does not matter, just
// compiler-time fluff:
T collection = createEmptyCollection();
while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
Object value;
if (t == JsonToken.VALUE_NULL) {
value = null;
} else if (typeDeser == null) {
value = valueDes.deserialize(p, ctxt);
} else {
value = valueDes.deserializeWithType(p, ctxt, typeDeser);
}
// .plus is always overridden to return the correct subclass
@SuppressWarnings("unchecked")
T newCollection = (T) collection.plus(value);
collection = newCollection;
}
return collection;
}
protected MapDeserializer(MapDeserializer src,
KeyDeserializer keyDeser, JsonDeserializer<Object> valueDeser,
TypeDeserializer valueTypeDeser,
NullValueProvider nuller,
Set<String> ignorable)
{
super(src, nuller, src._unwrapSingle);
_keyDeserializer = keyDeser;
_valueDeserializer = valueDeser;
_valueTypeDeserializer = valueTypeDeser;
_valueInstantiator = src._valueInstantiator;
_propertyBasedCreator = src._propertyBasedCreator;
_delegateDeserializer = src._delegateDeserializer;
_hasDefaultCreator = src._hasDefaultCreator;
_ignorableProperties = ignorable;
_standardStringKey = _isStdKeyDeser(_containerType, keyDeser);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType collectionType,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
Class<?> raw = collectionType.getRawClass();
if (raw == CharSeq.class) {
return new CharSeqDeserializer(collectionType);
}
if (Seq.class.isAssignableFrom(raw)) {
return new SeqDeserializer(collectionType, collectionType.getContentType(), elementTypeDeserializer,
elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
if (Set.class.isAssignableFrom(raw)) {
return new SetDeserializer(collectionType, collectionType.getContentType(), elementTypeDeserializer,
elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
if (PriorityQueue.class.isAssignableFrom(raw)) {
return new PriorityQueueDeserializer(collectionType, collectionType.getContentType(),
elementTypeDeserializer, elementDeserializer, settings.deserializeNullAsEmptyCollection());
}
return super.findCollectionLikeDeserializer(collectionType, config, beanDesc, elementTypeDeserializer, elementDeserializer);
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
JsonDeserializer<?> elementDeser = elementDeserializer;
TypeDeserializer elementTypeDeser = elementTypeDeserializer;
if (elementDeser == null) {
elementDeser = ctxt.findContextualValueDeserializer(elementType, property);
} else {
elementDeser = ctxt.handleSecondaryContextualization(elementDeser, property, elementType);
}
if (elementTypeDeser != null) {
elementTypeDeser = elementTypeDeser.forProperty(property);
}
return createDeserializer(elementTypeDeser, elementDeser);
}
public MethodProperty(BeanPropertyDefinition propDef,
JavaType type, TypeDeserializer typeDeser,
Annotations contextAnnotations, AnnotatedMethod method)
{
super(propDef, type, typeDeser, contextAnnotations);
_annotated = method;
_setter = method.getAnnotated();
_skipNulls = NullsConstantProvider.isSkipper(_nullProvider);
}
@SuppressWarnings("unchecked")
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext cxt, TypeDeserializer tDeserializer) throws IOException {
// This method will be called by Jackson for each "Json object with TypeId" in the input wire stream
// it is trying to deserialize.
// The below variable 'currentJsonNode' will hold the JsonNode corresponds to current
// Json object this method is called to handle.
//
JsonNode currentJsonNode = mapper.readTree(jp);
final Class<?> tClass = this.defaultDeserializer.handledType();
for (Class<?> c : TypeToken.of(tClass).getTypes().classes().rawTypes()) {
if (c.isAssignableFrom(Object.class)) {
continue;
} else {
final JsonTypeInfo typeInfo = c.getAnnotation(JsonTypeInfo.class);
if (typeInfo != null) {
String typeId = typeInfo.property();
if (containsDot(typeId)) {
final String typeIdOnWire = unescapeEscapedDots(typeId);
JsonNode typeIdValue = ((ObjectNode) currentJsonNode).remove(typeIdOnWire);
if (typeIdValue != null) {
((ObjectNode) currentJsonNode).put(typeId, typeIdValue);
}
}
}
}
}
return tDeserializer.deserializeTypedFromAny(newJsonParserForNode(currentJsonNode), cxt);
}
public RefValueHandler(
JavaType valueType,
JsonDeserializer<?> _valueDeserializer,
TypeDeserializer _typeDeserializerForValue
) {
if (valueType == null) { throw new IllegalArgumentException("valueType == null"); }
this._valueType = valueType;
this._valueDeserializer = _valueDeserializer;
this._typeDeserializerForValue = _typeDeserializerForValue;
}
@Override
public TypeDeserializer forProperty(BeanProperty prop) {
if (prop == _property) { // usually if it's null
return this;
}
return new AsExternalTypeDeserializer(this, prop);
}
@Override
public JsonDeserializer<?> findArrayDeserializer(ArrayType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer)
throws JsonMappingException
{
return _find(type);
}
@Override
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer)
throws JsonMappingException {
// 21-Oct-2015, tatu: Unlikely this will really get used (reference types need more
// work, simple registration probably not sufficient). But whatever.
return _find(refType);
}
@Override
public JsonDeserializer<?> findCollectionLikeDeserializer(CollectionLikeType type, DeserializationConfig config,
BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
for (Deserializers deserializers : deserializersList) {
JsonDeserializer<?> deserializer = deserializers.findCollectionLikeDeserializer(type, config, beanDesc, elementTypeDeserializer, elementDeserializer);
if (deserializer != null) {
return deserializer;
}
}
return null;
}
@Override
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType type,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer)
throws JsonMappingException {
Class<?> raw = type.getRawClass();
if (raw == Lazy.class) {
return new LazyDeserializer(type, type.getContentType(), contentTypeDeserializer, contentDeserializer);
}
if (raw == Option.class) {
return new OptionDeserializer(type, type.getContentType(), contentTypeDeserializer, contentDeserializer, settings.useOptionInPlainFormat());
}
return super.findReferenceDeserializer(type, config, beanDesc, contentTypeDeserializer, contentDeserializer);
}
@Override
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType, DeserializationConfig config,
BeanDescription beanDesc, TypeDeserializer contentTypeDeserializer,
JsonDeserializer<?> contentDeserializer) throws JsonMappingException {
for (Deserializers deserializers : deserializersList) {
JsonDeserializer<?> deserializer = deserializers.findReferenceDeserializer(refType, config, beanDesc, contentTypeDeserializer, contentDeserializer);
if (deserializer != null) {
return deserializer;
}
}
return null;
}
@Override
public JsonDeserializer<?> createCollectionLikeDeserializer(DeserializationContext ctxt,
CollectionLikeType type, final BeanDescription beanDesc)
throws JsonMappingException
{
JavaType contentType = type.getContentType();
// Very first thing: is deserializer hard-coded for elements?
JsonDeserializer<Object> contentDeser = contentType.getValueHandler();
final DeserializationConfig config = ctxt.getConfig();
// Then optional type info (1.5): if type has been resolved, we may already know type deserializer:
TypeDeserializer contentTypeDeser = contentType.getTypeHandler();
// but if not, may still be possible to find:
if (contentTypeDeser == null) {
contentTypeDeser = findTypeDeserializer(config, contentType);
}
JsonDeserializer<?> deser = _findCustomCollectionLikeDeserializer(type, config, beanDesc,
contentTypeDeser, contentDeser);
if (deser != null) {
// and then new with 2.2: ability to post-process it too (Issue#120)
if (_factoryConfig.hasDeserializerModifiers()) {
for (BeanDeserializerModifier mod : _factoryConfig.deserializerModifiers()) {
deser = mod.modifyCollectionLikeDeserializer(config, type, beanDesc, deser);
}
}
}
return deser;
}
@Override // since 2.7
public JsonDeserializer<?> findReferenceDeserializer(ReferenceType refType,
DeserializationConfig config, BeanDescription beanDesc,
TypeDeserializer contentTypeDeserializer, JsonDeserializer<?> contentDeserializer)
{
// 28-Oct-2016, tatu: Should try to support subtypes too, with ValueInstantiators, but
// not 100% clear how this could work at this point
// if (refType.isTypeOrSubTypeOf(Optional.class)) {
if (refType.hasRawClass(Optional.class)) {
return new GuavaOptionalDeserializer(refType, null, contentTypeDeserializer, contentDeserializer);
}
return null;
}
@Override
public GuavaMapDeserializer<ImmutableSortedMap<Object, Object>> withResolved(KeyDeserializer keyDeser,
JsonDeserializer<?> valueDeser, TypeDeserializer typeDeser,
NullValueProvider nuller)
{
return new ImmutableSortedMapDeserializer(_containerType, keyDeser, valueDeser, typeDeser, nuller);
}
/**
* Constructor used when creating contextualized instances.
*/
protected ArrayBlockingQueueDeserializer(JavaType containerType,
JsonDeserializer<Object> valueDeser, TypeDeserializer valueTypeDeser,
ValueInstantiator valueInstantiator,
JsonDeserializer<Object> delegateDeser,
NullValueProvider nuller, Boolean unwrapSingle)
{
super(containerType, valueDeser, valueTypeDeser, valueInstantiator, delegateDeser,
nuller, unwrapSingle);
}
@Override
protected JsonDeserializer<?> _createContextual(JavaType type,
KeyDeserializer keyDeserializer, TypeDeserializer typeDeserializer,
JsonDeserializer<?> elementDeserializer, Method method, NullValueProvider nvp) {
return new LinkedListMultimapDeserializer(type, keyDeserializer, typeDeserializer,
elementDeserializer, method, nvp);
}
@Override
protected JsonDeserializer<?> _createContextual(JavaType type,
KeyDeserializer keyDeserializer, TypeDeserializer typeDeserializer,
JsonDeserializer<?> elementDeserializer, Method method,
NullValueProvider nvp) {
return new ArrayListMultimapDeserializer(type, keyDeserializer, typeDeserializer,
elementDeserializer, method, nvp);
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException
{
// 16-Feb-2012, tatu: ObjectId may be used as well... need to check that first
if (_objectIdReader != null) {
// 05-Aug-2013, tatu: May use native Object Id
if (p.canReadObjectId()) {
Object id = p.getObjectId();
if (id != null) {
Object ob = typeDeserializer.deserializeTypedFromObject(p, ctxt);
return _handleTypedObjectId(p, ctxt, ob, id);
}
}
// or, Object Ids Jackson explicitly sets
JsonToken t = p.getCurrentToken();
if (t != null) {
// Most commonly, a scalar (int id, uuid String, ...)
if (t.isScalarValue()) {
return deserializeFromObjectId(p, ctxt);
}
// but, with 2.5+, a simple Object-wrapped value also legal:
if (t == JsonToken.START_OBJECT) {
t = p.nextToken();
}
if ((t == JsonToken.FIELD_NAME) && _objectIdReader.maySerializeAsObject()
&& _objectIdReader.isValidReferencePropertyName(p.getCurrentName(), p)) {
return deserializeFromObjectId(p, ctxt);
}
}
}
// In future could check current token... for now this should be enough:
return typeDeserializer.deserializeTypedFromObject(p, ctxt);
}
/**
* Overridable fluent factory method used for creating contextual
* instances.
*/
private OptionDeserializer withResolved(JavaType refType, TypeDeserializer typeDeser, JsonDeserializer<?> valueDeser) {
if (refType == valueType && valueDeser == valueDeserializer && typeDeser == valueTypeDeserializer) {
return this;
}
return new OptionDeserializer(this, typeDeser, valueDeser);
}
protected ObjectArrayDeserializer(ObjectArrayDeserializer base,
JsonDeserializer<Object> elemDeser, TypeDeserializer elemTypeDeser,
NullValueProvider nuller, Boolean unwrapSingle)
{
super(base, nuller, unwrapSingle);
_elementClass = base._elementClass;
_untyped = base._untyped;
_elementDeserializer = elemDeser;
_elementTypeDeserializer = elemTypeDeser;
}
@Override
public JsonDeserializer<?> findCollectionDeserializer(CollectionType type, DeserializationConfig config, BeanDescription beanDesc, TypeDeserializer elementTypeDeserializer, JsonDeserializer<?> elementDeserializer) throws JsonMappingException {
if (IterableX.class.isAssignableFrom(type.getRawClass())) {
return new IterableXDeserializer(type.getRawClass(),type.containedTypeOrUnknown(0).getRawClass(),elementTypeDeserializer,elementDeserializer, type);
}
return super.findCollectionDeserializer(type, config, beanDesc, elementTypeDeserializer, elementDeserializer);
}
@Override
public Object[] deserializeWithType(JsonParser p, DeserializationContext ctxt,
TypeDeserializer typeDeserializer)
throws IOException
{
// Should there be separate handling for base64 stuff?
// for now this should be enough:
return (Object[]) typeDeserializer.deserializeTypedFromArray(p, ctxt);
}
@Override
public JsonDeserializer<?> findMapLikeDeserializer(
MapLikeType type,
DeserializationConfig config,
BeanDescription beanDesc,
KeyDeserializer keyDeserializer,
TypeDeserializer elementTypeDeserializer,
JsonDeserializer<?> elementDeserializer) {
// TODO: use keyDeserializer, elementTypeDeserializer, and elementDeserializer
return findBeanDeserializer(type, config, beanDesc);
}