/** * Method that will try to determine statically defined type of property * being serialized, based on annotations (for overrides), and alternatively * declared type (if static typing for serialization is enabled). * If neither can be used (no annotations, dynamic typing), returns null. */ protected JavaType findSerializationType(Annotated a, boolean useStaticTyping) { // [JACKSON-120]: Check to see if serialization type is fixed Class<?> serializationType = _annotationIntrospector.findSerializationType(a); if (serializationType != null) { // Must be a super type... Class<?> raw = a.getRawType(); if (!serializationType.isAssignableFrom(raw)) { throw new IllegalArgumentException("Illegal concrete-type annotation for method '"+a.getName()+"': class "+serializationType.getName()+" not a super-type of (declared) class "+raw.getName()); } return TypeFactory.type(serializationType); } /* [JACKSON-114]: if using static typing, declared type is known * to be the type... */ JsonSerialize.Typing typing = _annotationIntrospector.findSerializationTyping(a); if (typing != null) { useStaticTyping = (typing == JsonSerialize.Typing.STATIC); } if (useStaticTyping) { return TypeFactory.type(a.getGenericType()); } return null; }
/** * Helper method called to check if a class or method * has an annotation * (@link org.codehaus.jackson.map.ser.JsonSerialize#using) * that tells the class to use for serialization. * Returns null if no such annotation found. */ @SuppressWarnings("unchecked") protected JsonSerializer<Object> findSerializerFromAnnotation(SerializationConfig config, Annotated a) { Object serDef = config.getAnnotationIntrospector().findSerializer(a); if (serDef != null) { if (serDef instanceof JsonSerializer) { return (JsonSerializer<Object>) serDef; } /* Alas, there's no way to force return type of "either class * X or Y" -- need to throw an exception after the fact */ if (!(serDef instanceof Class)) { throw new IllegalStateException("AnnotationIntrospector returned value of type "+serDef.getClass().getName()+"; expected type JsonSerializer or Class<JsonSerializer> instead"); } Class<?> cls = (Class<?>) serDef; if (!JsonSerializer.class.isAssignableFrom(cls)) { throw new IllegalStateException("AnnotationIntrospector returned Class "+cls.getName()+"; expected Class<JsonSerializer>"); } return (JsonSerializer<Object>) ClassUtil.createInstance(cls, config.isEnabled(SerializationConfig.Feature.CAN_OVERRIDE_ACCESS_MODIFIERS)); } return null; }
/** * * @param base Base member to use for type resolution: either annotated type (class), * or property (field, getter/setter) */ public static List<NamedType> collectAndResolveSubtypes(Annotated base, MapperConfig<?> config, AnnotationIntrospector ai) { // but if annotations found, may need to resolve subtypes: Collection<NamedType> st = ai.findSubtypes(base); AnnotatedClass ac = (base instanceof AnnotatedClass) ? (AnnotatedClass) base : null; // If no explicit definitions, base itself might have name if (st == null || st.isEmpty()) { if (ac != null) { String name = ai.findTypeName(ac); if (name != null) { ArrayList<NamedType> result = new ArrayList<NamedType>(); result.add(new NamedType(ac.getRawType(), name)); return result; } } return null; } return instance._collectAndResolve(ac, config, ai, st); }
JsonDeserializer<Object> _constructDeserializer(DeserializationConfig paramDeserializationConfig, Annotated paramAnnotated, BeanProperty paramBeanProperty, Object paramObject) { if ((paramObject instanceof JsonDeserializer)) { JsonDeserializer localJsonDeserializer2 = (JsonDeserializer)paramObject; if ((localJsonDeserializer2 instanceof ContextualDeserializer)) localJsonDeserializer2 = ((ContextualDeserializer)localJsonDeserializer2).createContextual(paramDeserializationConfig, paramBeanProperty); return localJsonDeserializer2; } if (!(paramObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned deserializer definition of type " + paramObject.getClass().getName() + "; expected type JsonDeserializer or Class<JsonDeserializer> instead"); Class localClass = (Class)paramObject; if (!JsonDeserializer.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<JsonDeserializer>"); JsonDeserializer localJsonDeserializer1 = paramDeserializationConfig.deserializerInstance(paramAnnotated, localClass); if ((localJsonDeserializer1 instanceof ContextualDeserializer)) localJsonDeserializer1 = ((ContextualDeserializer)localJsonDeserializer1).createContextual(paramDeserializationConfig, paramBeanProperty); return localJsonDeserializer1; }
/** * Update the generator with a default codec and pretty printer * * @param jsonFactory Factory to set as codec * @param jsonGenerator Generator to configure */ private static void updateGenerator(JsonFactory jsonFactory, JsonGenerator jsonGenerator) { ObjectMapper mapper = new ObjectMapper(jsonFactory); //Update the annotation interceptor to also include jaxb annotations as a second choice AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondary = new JaxbAnnotationIntrospector() { /** * BUG FIX: * By contract, if findSerializationInclusion didn't encounter any annotations that should change the * inclusion value, it should return the default value it has received; but actually returns null, which * overrides the NON_NULL option we set. * The doc states issue JACKSON-256 which was supposed to be fixed in 1.5.0. *twilight zone theme song* */ @Override public JsonSerialize.Inclusion findSerializationInclusion(Annotated a, JsonSerialize.Inclusion defValue) { JsonSerialize.Inclusion inclusion = super.findSerializationInclusion(a, defValue); if (inclusion == null) { return defValue; } return inclusion; } }; AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary); mapper.getSerializationConfig().setAnnotationIntrospector(pair); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); jsonGenerator.setCodec(mapper); jsonGenerator.useDefaultPrettyPrinter(); }
protected static JsonSerializer<Object> findContentSerializer(SerializationConfig paramSerializationConfig, Annotated paramAnnotated, BeanProperty paramBeanProperty) { AnnotationIntrospector localAnnotationIntrospector = paramSerializationConfig.getAnnotationIntrospector(); Class localClass = localAnnotationIntrospector.findContentSerializer(paramAnnotated); if (((localClass == null) || (localClass == JsonSerializer.None.class)) && (paramBeanProperty != null)) localClass = localAnnotationIntrospector.findContentSerializer(paramBeanProperty.getMember()); if ((localClass != null) && (localClass != JsonSerializer.None.class)) return paramSerializationConfig.serializerInstance(paramAnnotated, localClass); return null; }
protected static JsonSerializer<Object> findKeySerializer(SerializationConfig paramSerializationConfig, Annotated paramAnnotated, BeanProperty paramBeanProperty) { AnnotationIntrospector localAnnotationIntrospector = paramSerializationConfig.getAnnotationIntrospector(); Class localClass = localAnnotationIntrospector.findKeySerializer(paramAnnotated); if (((localClass == null) || (localClass == JsonSerializer.None.class)) && (paramBeanProperty != null)) localClass = localAnnotationIntrospector.findKeySerializer(paramBeanProperty.getMember()); if ((localClass != null) && (localClass != JsonSerializer.None.class)) return paramSerializationConfig.serializerInstance(paramAnnotated, localClass); return null; }
protected <T extends JavaType> T modifyTypeByAnnotation(SerializationConfig paramSerializationConfig, Annotated paramAnnotated, T paramT) { Class localClass = paramSerializationConfig.getAnnotationIntrospector().findSerializationType(paramAnnotated); if (localClass != null); try { JavaType localJavaType = paramT.widenBy(localClass); paramT = localJavaType; return modifySecondaryTypesByAnnotation(paramSerializationConfig, paramAnnotated, paramT); } catch (IllegalArgumentException localIllegalArgumentException) { } throw new IllegalArgumentException("Failed to widen type " + paramT + " with concrete-type annotation (value " + localClass.getName() + "), method '" + paramAnnotated.getName() + "': " + localIllegalArgumentException.getMessage()); }
public JsonSerializer<Object> serializerInstance(Annotated paramAnnotated, Class<? extends JsonSerializer<?>> paramClass) { HandlerInstantiator localHandlerInstantiator = getHandlerInstantiator(); if (localHandlerInstantiator != null) { JsonSerializer localJsonSerializer = localHandlerInstantiator.serializerInstance(this, paramAnnotated, paramClass); if (localJsonSerializer != null) return localJsonSerializer; } return (JsonSerializer)ClassUtil.createInstance(paramClass, canOverrideAccessModifiers()); }
protected JsonDeserializer<Object> findDeserializerFromAnnotation(DeserializationConfig paramDeserializationConfig, Annotated paramAnnotated, BeanProperty paramBeanProperty) throws JsonMappingException { Object localObject = paramDeserializationConfig.getAnnotationIntrospector().findDeserializer(paramAnnotated); if (localObject != null) return _constructDeserializer(paramDeserializationConfig, paramAnnotated, paramBeanProperty, localObject); return null; }
public JsonDeserializer<Object> deserializerInstance(Annotated paramAnnotated, Class<? extends JsonDeserializer<?>> paramClass) { HandlerInstantiator localHandlerInstantiator = getHandlerInstantiator(); if (localHandlerInstantiator != null) { JsonDeserializer localJsonDeserializer = localHandlerInstantiator.deserializerInstance(this, paramAnnotated, paramClass); if (localJsonDeserializer != null) return localJsonDeserializer; } return (JsonDeserializer)ClassUtil.createInstance(paramClass, canOverrideAccessModifiers()); }
public KeyDeserializer keyDeserializerInstance(Annotated paramAnnotated, Class<? extends KeyDeserializer> paramClass) { HandlerInstantiator localHandlerInstantiator = getHandlerInstantiator(); if (localHandlerInstantiator != null) { KeyDeserializer localKeyDeserializer = localHandlerInstantiator.keyDeserializerInstance(this, paramAnnotated, paramClass); if (localKeyDeserializer != null) return localKeyDeserializer; } return (KeyDeserializer)ClassUtil.createInstance(paramClass, canOverrideAccessModifiers()); }
@Deprecated public Object findDeserializer(Annotated paramAnnotated, BeanProperty paramBeanProperty) { if (paramBeanProperty != null) return findDeserializer(paramAnnotated); return null; }
@Deprecated public Object findSerializer(Annotated paramAnnotated, BeanProperty paramBeanProperty) { if (paramBeanProperty != null) return findSerializer(paramAnnotated); return null; }
public Class<? extends JsonDeserializer<?>> findContentDeserializer(Annotated paramAnnotated) { Class localClass = this._primary.findContentDeserializer(paramAnnotated); if ((localClass == null) || (localClass == JsonDeserializer.None.class)) localClass = this._secondary.findContentDeserializer(paramAnnotated); return localClass; }
public Class<? extends JsonSerializer<?>> findContentSerializer(Annotated paramAnnotated) { Class localClass = this._primary.findContentSerializer(paramAnnotated); if ((localClass == null) || (localClass == JsonSerializer.None.class)) localClass = this._secondary.findContentSerializer(paramAnnotated); return localClass; }
public Class<?> findDeserializationContentType(Annotated paramAnnotated, JavaType paramJavaType, String paramString) { Class localClass = this._primary.findDeserializationContentType(paramAnnotated, paramJavaType, paramString); if (localClass == null) localClass = this._secondary.findDeserializationContentType(paramAnnotated, paramJavaType, paramString); return localClass; }
public Class<?> findDeserializationKeyType(Annotated paramAnnotated, JavaType paramJavaType, String paramString) { Class localClass = this._primary.findDeserializationKeyType(paramAnnotated, paramJavaType, paramString); if (localClass == null) localClass = this._secondary.findDeserializationKeyType(paramAnnotated, paramJavaType, paramString); return localClass; }
public Class<?> findDeserializationType(Annotated paramAnnotated, JavaType paramJavaType, String paramString) { Class localClass = this._primary.findDeserializationType(paramAnnotated, paramJavaType, paramString); if (localClass == null) localClass = this._secondary.findDeserializationType(paramAnnotated, paramJavaType, paramString); return localClass; }
public Object findDeserializer(Annotated paramAnnotated) { Object localObject = this._primary.findDeserializer(paramAnnotated); if (localObject == null) localObject = this._secondary.findDeserializer(paramAnnotated); return localObject; }
public Object findDeserializer(Annotated paramAnnotated, BeanProperty paramBeanProperty) { Object localObject = this._primary.findDeserializer(paramAnnotated, paramBeanProperty); if (localObject == null) localObject = this._secondary.findDeserializer(paramAnnotated, paramBeanProperty); return localObject; }
public Class<? extends KeyDeserializer> findKeyDeserializer(Annotated paramAnnotated) { Class localClass = this._primary.findKeyDeserializer(paramAnnotated); if ((localClass == null) || (localClass == KeyDeserializer.None.class)) localClass = this._secondary.findKeyDeserializer(paramAnnotated); return localClass; }
public Class<? extends JsonSerializer<?>> findKeySerializer(Annotated paramAnnotated) { Class localClass = this._primary.findKeySerializer(paramAnnotated); if ((localClass == null) || (localClass == JsonSerializer.None.class)) localClass = this._secondary.findKeySerializer(paramAnnotated); return localClass; }
public Class<?> findSerializationContentType(Annotated paramAnnotated, JavaType paramJavaType) { Class localClass = this._primary.findSerializationContentType(paramAnnotated, paramJavaType); if (localClass == null) localClass = this._secondary.findSerializationContentType(paramAnnotated, paramJavaType); return localClass; }
public Class<?> findSerializationKeyType(Annotated paramAnnotated, JavaType paramJavaType) { Class localClass = this._primary.findSerializationKeyType(paramAnnotated, paramJavaType); if (localClass == null) localClass = this._secondary.findSerializationKeyType(paramAnnotated, paramJavaType); return localClass; }
public Class<?> findSerializationType(Annotated paramAnnotated) { Class localClass = this._primary.findSerializationType(paramAnnotated); if (localClass == null) localClass = this._secondary.findSerializationType(paramAnnotated); return localClass; }
public JsonSerialize.Typing findSerializationTyping(Annotated paramAnnotated) { JsonSerialize.Typing localTyping = this._primary.findSerializationTyping(paramAnnotated); if (localTyping == null) localTyping = this._secondary.findSerializationTyping(paramAnnotated); return localTyping; }
public Class<?>[] findSerializationViews(Annotated paramAnnotated) { Class[] arrayOfClass = this._primary.findSerializationViews(paramAnnotated); if (arrayOfClass == null) arrayOfClass = this._secondary.findSerializationViews(paramAnnotated); return arrayOfClass; }
public Object findSerializer(Annotated paramAnnotated) { Object localObject = this._primary.findSerializer(paramAnnotated); if (localObject == null) localObject = this._secondary.findSerializer(paramAnnotated); return localObject; }
public Object findSerializer(Annotated paramAnnotated, BeanProperty paramBeanProperty) { Object localObject = this._primary.findSerializer(paramAnnotated, paramBeanProperty); if (localObject == null) localObject = this._secondary.findSerializer(paramAnnotated, paramBeanProperty); return localObject; }
public List<NamedType> findSubtypes(Annotated paramAnnotated) { List localList1 = this._primary.findSubtypes(paramAnnotated); List localList2 = this._secondary.findSubtypes(paramAnnotated); if ((localList1 == null) || (localList1.isEmpty())) return localList2; if ((localList2 == null) || (localList2.isEmpty())) return localList1; ArrayList localArrayList = new ArrayList(localList1.size() + localList2.size()); localArrayList.addAll(localList1); localArrayList.addAll(localList2); return localArrayList; }
public TypeIdResolver typeIdResolverInstance(Annotated paramAnnotated, Class<? extends TypeIdResolver> paramClass) { HandlerInstantiator localHandlerInstantiator = getHandlerInstantiator(); if (localHandlerInstantiator != null) { TypeIdResolver localTypeIdResolver = localHandlerInstantiator.typeIdResolverInstance(this, paramAnnotated, paramClass); if (localTypeIdResolver != null) return localTypeIdResolver; } return (TypeIdResolver)ClassUtil.createInstance(paramClass, canOverrideAccessModifiers()); }
public TypeResolverBuilder<?> typeResolverBuilderInstance(Annotated paramAnnotated, Class<? extends TypeResolverBuilder<?>> paramClass) { HandlerInstantiator localHandlerInstantiator = getHandlerInstantiator(); if (localHandlerInstantiator != null) { TypeResolverBuilder localTypeResolverBuilder = localHandlerInstantiator.typeResolverBuilderInstance(this, paramAnnotated, paramClass); if (localTypeResolverBuilder != null) return localTypeResolverBuilder; } return (TypeResolverBuilder)ClassUtil.createInstance(paramClass, canOverrideAccessModifiers()); }
/** * Helper method called to check if a class or method * has annotation that tells which class to use for deserialization. * Returns null if no such annotation found. */ protected JsonDeserializer<Object> findDeserializerFromAnnotation(DeserializationConfig config, Annotated a) { Object deserDef = config.getAnnotationIntrospector().findDeserializer(a); if (deserDef != null) { return _constructDeserializer(config, deserDef); } return null; }
protected JsonSerializer<Object> findSerializerFromAnnotation(SerializationConfig paramSerializationConfig, Annotated paramAnnotated, BeanProperty paramBeanProperty) throws JsonMappingException { Object localObject = paramSerializationConfig.getAnnotationIntrospector().findSerializer(paramAnnotated); JsonSerializer localJsonSerializer; if (localObject == null) localJsonSerializer = null; do { while (true) { return localJsonSerializer; if (!(localObject instanceof JsonSerializer)) break; localJsonSerializer = (JsonSerializer)localObject; if ((localJsonSerializer instanceof ContextualSerializer)) return ((ContextualSerializer)localJsonSerializer).createContextual(paramSerializationConfig, paramBeanProperty); } if (!(localObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned value of type " + localObject.getClass().getName() + "; expected type JsonSerializer or Class<JsonSerializer> instead"); Class localClass = (Class)localObject; if (!JsonSerializer.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<JsonSerializer>"); localJsonSerializer = paramSerializationConfig.serializerInstance(paramAnnotated, localClass); } while (!(localJsonSerializer instanceof ContextualSerializer)); return ((ContextualSerializer)localJsonSerializer).createContextual(paramSerializationConfig, paramBeanProperty); }