public static Class<?> rawClass(Type t) { if (t instanceof Class<?>) { return (Class<?>) t; } // Can optimize bit more in future... return defaultInstance().constructType(t).getRawClass(); }
/** * 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(); case NON_FINAL: while (t.isArrayType()) { t = t.getContentType(); } return !t.isFinal(); // includes Object.class default: //case JAVA_LANG_OBJECT: return (t.getRawClass() == Object.class); } } }
/** * Since 1.7, it is possible to use {@link JsonTypeInfo} from a property too. */ @Override public TypeResolverBuilder<?> findPropertyTypeResolver(MapperConfig<?> config, AnnotatedMember am, JavaType baseType) { /* As per definition of @JsonTypeInfo, should only apply to contents of container * (collection, map) types, not container types themselves: */ if (baseType.isContainerType()) return null; // No per-member type overrides (yet) return _findTypeResolver(config, am, baseType); }
/** * @since 1.9 */ public JavaType withContentValueHandler(Object h) { /* 16-Aug-2011, tatu: This is not entirely correct, as we can not * create new immutable instances. However, sub-classes can, * so if mapper is version 1.9, things work as expected. * Otherwise, in 2.0 we can make this method abstract. */ getContentType().setValueHandler(h); return this; }
@Override public JavaType narrowContentsBy(Class<?> contentClass) { // Can do a quick check first: if (contentClass == _elementType.getRawClass()) { return this; } return new CollectionLikeType(_class, _elementType.narrowBy(contentClass), _valueHandler, _typeHandler); }
Class<?> enumClass = ClassUtil.findEnumType((EnumSet<?>) value); str = TypeFactory.defaultInstance().constructCollectionType(EnumSet.class, enumClass).toCanonical(); } else if (value instanceof EnumMap<?,?>) { Class<?> enumClass = ClassUtil.findEnumType((EnumMap<?,?>) value); Class<?> valueClass = Object.class; str = TypeFactory.defaultInstance().constructMapType(EnumMap.class, enumClass, valueClass).toCanonical(); } else { String end = str.substring(9); Class<?> staticType = _baseType.getRawClass(); if (ClassUtil.getOuterClass(staticType) == null) { cls = _baseType.getRawClass(); str = cls.getName();
throws JsonMappingException if (type.isEnumType()) { return (JsonDeserializer<Object>) _factory.createEnumDeserializer(config, this, type, property); if (type.isContainerType()) { if (type.isArrayType()) { return (JsonDeserializer<Object>)_factory.createArrayDeserializer(config, this, (ArrayType) type, property); if (type.isMapLikeType()) { MapLikeType mlt = (MapLikeType) type; if (mlt.isTrueMapType()) { mlt, property); if (type.isCollectionLikeType()) { CollectionLikeType clt = (CollectionLikeType) type; if (clt.isTrueCollectionType()) { if (JsonNode.class.isAssignableFrom(type.getRawClass())) { return (JsonDeserializer<Object>)_factory.createTreeDeserializer(config, this, type, property);
@Override public Class findContentDeserializer(Annotated am) { if (am.hasAnnotation(ObjectId.class)) { JavaType type = deserializationConfig.getTypeFactory().constructType(am.getGenericType()); if (type.isCollectionLikeType()) { return findObjectIdDeserializer(type.containedType(0)); } else if (type.isMapLikeType()) { return findObjectIdDeserializer(type.containedType(1)); } } return null; }
if (type.isContainerType()) { AnnotationIntrospector intr = config.getAnnotationIntrospector(); JavaType keyType = type.getKeyType(); if (keyType != null) { Class<? extends KeyDeserializer> kdClass = intr.findKeyDeserializer(member); if (kdClass != null && kdClass != KeyDeserializer.None.class) { KeyDeserializer kd = config.keyDeserializerInstance(member, kdClass); keyType.setValueHandler(kd); JsonDeserializer<Object> cd = config.deserializerInstance(member, cdClass); type.getContentType().setValueHandler(cd); (AnnotatedMember) member, property); if (contentTypeDeser != null) { type = type.withContentTypeHandler(contentTypeDeser); type = type.withTypeHandler(valueTypeDeser);
|| Collection.class.isAssignableFrom(subclass)) { if (!baseType.getRawClass().isAssignableFrom(subclass)) { throw new IllegalArgumentException("Class "+subclass.getClass().getName()+" not subtype of "+baseType); JavaType subtype = _fromClass(subclass, new TypeBindings(this, baseType.getRawClass())); Object h = baseType.getValueHandler(); if (h != null) { subtype = subtype.withValueHandler(h); h = baseType.getTypeHandler(); if (h != null) { subtype = subtype.withTypeHandler(h); return baseType.narrowBy(subclass);
/** * Helper method that handles configuration details when constructing serializers for * {@link java.util.EnumMap} types. *<p> * Note: signature changed in 1.8, to take 'staticTyping' argument */ protected JsonSerializer<?> buildEnumMapSerializer(SerializationConfig config, JavaType type, BasicBeanDescription beanDesc, BeanProperty property, boolean staticTyping, TypeSerializer elementTypeSerializer, JsonSerializer<Object> elementValueSerializer) { JavaType keyType = type.getKeyType(); // Need to find key enum values... EnumValues enums = null; if (keyType.isEnumType()) { // non-enum if we got it as type erased class (from instance) @SuppressWarnings("unchecked") Class<Enum<?>> enumClass = (Class<Enum<?>>) keyType.getRawClass(); enums = EnumValues.construct(enumClass, config.getAnnotationIntrospector()); } return new EnumMapSerializer(type.getContentType(), staticTyping, enums, elementTypeSerializer, property, elementValueSerializer); }
type = provider.constructType(prop.getGenericPropertyType()); if (!type.isFinal()) { if (type.isContainerType() || type.containedTypeCount() > 0) { prop.setNonTrivialBaseType(type); JsonSerializer<Object> ser = provider.findValueSerializer(type, prop); if (type.isContainerType()) { TypeSerializer typeSer = type.getContentType().getTypeHandler(); if (typeSer != null) {
@SuppressWarnings("unchecked") @Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { ObjectNode o = createSchemaNode("object", true); if (typeHint instanceof ParameterizedType) { Type[] typeArgs = ((ParameterizedType) typeHint).getActualTypeArguments(); if (typeArgs.length == 2) { JavaType enumType = provider.constructType(typeArgs[0]); JavaType valueType = provider.constructType(typeArgs[1]); ObjectNode propsNode = JsonNodeFactory.instance.objectNode(); Class<Enum<?>> enumClass = (Class<Enum<?>>) enumType.getRawClass(); for (Enum<?> enumValue : enumClass.getEnumConstants()) { JsonSerializer<Object> ser = provider.findValueSerializer(valueType.getRawClass(), _property); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(provider, null) : JsonSchema.getDefaultSchemaNode(); propsNode.put(provider.getConfig().getAnnotationIntrospector().findEnumValue((Enum<?>)enumValue), schemaNode); } o.put("properties", propsNode); } } return o; } }
throws JsonMappingException Class<?> cls = baseType.getRawClass(); BasicBeanDescription bean = config.introspectClassAnnotations(cls); AnnotatedClass ac = bean.getClassInfo(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findTypeResolver(config, ac, baseType); b = config.getDefaultTyper(baseType); if (b == null) { return null; if ((b.getDefaultImpl() == null) && baseType.isAbstract()) { JavaType defaultType = mapAbstractType(config, baseType); if (defaultType != null && defaultType.getRawClass() != baseType.getRawClass()) { b = b.defaultImpl(defaultType.getRawClass());
@Override public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException { ObjectNode o = createSchemaNode("array", true); if (typeHint != null) { JavaType javaType = provider.constructType(typeHint); if (javaType.isArrayType()) { Class<?> componentType = ((ArrayType) javaType).getContentType().getRawClass(); // 15-Oct-2010, tatu: We can't serialize plain Object.class; but what should it produce here? Untyped? if (componentType == Object.class) { o.put("items", JsonSchema.getDefaultSchemaNode()); } else { JsonSerializer<Object> ser = provider.findValueSerializer(componentType, _property); JsonNode schemaNode = (ser instanceof SchemaAware) ? ((SchemaAware) ser).getSchema(provider, null) : JsonSchema.getDefaultSchemaNode(); o.put("items", schemaNode); } } } return o; }
/** * Method called to construct a type serializer for values with given declared * base type. This is called for values other than those of bean property * types. */ @Override public TypeSerializer createTypeSerializer(SerializationConfig config, JavaType baseType, BeanProperty property) { BasicBeanDescription bean = config.introspectClassAnnotations(baseType.getRawClass()); AnnotatedClass ac = bean.getClassInfo(); AnnotationIntrospector ai = config.getAnnotationIntrospector(); TypeResolverBuilder<?> b = ai.findTypeResolver(config, 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<NamedType> subtypes = null; if (b == null) { b = config.getDefaultTyper(baseType); } else { subtypes = config.getSubtypeResolver().collectAndResolveSubtypes(ac, config, ai); } return (b == null) ? null : b.buildTypeSerializer(config, baseType, subtypes, property); }
JavaType elementType = type.getContentType(); TypeSerializer elementTypeSerializer = createTypeSerializer(config, elementType, property); beanDesc.getClassInfo(), property); if (type.isMapLikeType()) { // implements java.util.Map MapLikeType mlt = (MapLikeType) type; JsonSerializer<Object> keySerializer = findKeySerializer(config, beanDesc.getClassInfo(), property); keySerializer, elementTypeSerializer, elementValueSerializer); if (type.isCollectionLikeType()) { CollectionLikeType clt = (CollectionLikeType) type; if (clt.isTrueCollectionType()) { elementTypeSerializer, elementValueSerializer); if (type.isArrayType()) { return buildArraySerializer(config, (ArrayType) type, beanDesc, property, staticTyping, elementTypeSerializer, elementValueSerializer);
/** * Helper method called to see if we need a comparator Object to check if values * of a container (Collection, array) property should be suppressed. * This is usually * * @param propertyName Name of property to handle * @param propertyType Declared type of values of the property to handle * @return Object whose <code>equals()</code> method is called to check if given value * is "empty Collection" value to suppress; or null if no such check should be done * (declared type not Collection or array) * * @since 1.9 */ protected Object getContainerValueChecker(String propertyName, JavaType propertyType) { // currently we will only check for certain kinds of empty containers: if (!_config.isEnabled(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS)) { if (propertyType.isArrayType()) { return new EmptyArrayChecker(); } if (Collection.class.isAssignableFrom(propertyType.getRawClass())) { return new EmptyCollectionChecker(); } } return null; }
public final static TypeFactory instance = new TypeFactory(); Class<?> raw = type.getRawClass(); if (raw == expType) { int count = type.containedTypeCount(); if (count == 0) return null; JavaType[] result = new JavaType[count]; for (int i = 0; i < count; ++i) { result[i] = type.containedType(i); return findTypeParameters(raw, expType, new TypeBindings(this, type));
JavaType contentType = null; if (typeHint != null) { JavaType javaType = provider.constructType(typeHint); contentType = javaType.getContentType(); if (contentType == null) { // could still be parametrized (Iterators) if (typeHint instanceof ParameterizedType) { Type[] typeArgs = ((ParameterizedType) typeHint).getActualTypeArguments(); if (typeArgs.length == 1) { contentType = provider.constructType(typeArgs[0]); JsonNode schemaNode = null; if (contentType.getRawClass() != Object.class) { JsonSerializer<Object> ser = provider.findValueSerializer(contentType, _property); if (ser instanceof SchemaAware) { schemaNode = ((SchemaAware) ser).getSchema(provider, null);