@Bean(name = "objectMapper") public ObjectMapper getObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new GuavaModule()); mapper.registerModule(new Jdk8Module()); mapper.registerModule(new JodaModule()); mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() { // borrowed from: http://jackson-users.ning.com/forum/topics/how-to-not-include-type-info-during-serialization-with @Override protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config, Annotated ann, JavaType baseType) { // Don't serialize JsonTypeInfo Property includes if (ann.hasAnnotation(JsonTypeInfo.class) && ann.getAnnotation(JsonTypeInfo.class).include() == JsonTypeInfo.As.PROPERTY && SerializationConfig.class.isAssignableFrom(config.getClass())) { return null; } return super._findTypeResolver(config, ann, baseType); } }); return mapper; }
@Override public Object findSerializer(Annotated annotatedMethod) { IdFormat annotation = annotatedMethod.getAnnotation(IdFormat.class); if (annotatedMethod.getRawType() == UUID.class) { if (annotation != null) { switch (annotation.value()) { case RAW: return UUIDSerializer.class; case URL62: return FriendlyIdSerializer.class; } } return FriendlyIdSerializer.class; } else { return null; } }
@Override public Object findDeserializer(Annotated annotatedMethod) { IdFormat annotation = annotatedMethod.getAnnotation(IdFormat.class); if (rawDeserializationType(annotatedMethod) == UUID.class) { if (annotation != null) { switch (annotation.value()) { case RAW: return UUIDDeserializer.class; case URL62: return FriendlyIdDeserializer.class; } } return FriendlyIdDeserializer.class; } else { return null; } }
@Override public Object findDeserializer(Annotated a) { // System.err.println("Annotated:" + a.getClass().getName() + " " + a); if (a instanceof AnnotatedClass) { Class<?> clazz = ((AnnotatedClass) a).getAnnotated(); // System.err.println("clazz:" + clazz.getName() + " " + a.getName()); if (clazz.isEnum()) { if (Onum.class.isAssignableFrom(clazz)) { // System.err.println("OnumJsonSerializerIntrospector findDeserializer:" + clazz.getName() + " a:" + a); return new OnumJsonDeserializer(clazz); } } } Object deserializer = super.findDeserializer(a); return deserializer; }
ObjectMapper getObjectMapper() { ObjectMapper objectMapper = ObjectMapperBuilder.buildNewObjectMapper(); // objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); SimpleModule module = new SimpleModule(); module.addSerializer(BigDecimal.class, new ToStringSerializer()); module.addSerializer(Long.class, new ToStringSerializer()); objectMapper.registerModule(module); //Disable JsonIdentityInfo JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() { @Override public ObjectIdInfo findObjectIdInfo(Annotated ann) { return null; } }; objectMapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector); return objectMapper; }
@Override public Object findDeserializationConverter(Annotated a) { JtToMap ann = a.getAnnotation(JtToMap.class); if (ann == null) { return null; } JavaType javaType = a.getType(); if(a instanceof AnnotatedMethod) { AnnotatedMethod am = (AnnotatedMethod) a; if(am.getParameterCount() == 1) { javaType = am.getParameterType(0); } else { throw new RuntimeException("Invalid property setter: " + am.getAnnotated()); } } return new DeserializationConverterImpl(ann, new Ctx(a, javaType)); }
@Override public Object findDeserializationConverter(Annotated a) { Class<? extends PropertyInterceptor>[] interceptors = getInterceptors(a); if (interceptors == null) { return null; } JavaType javaType = a.getType(); if(a instanceof AnnotatedMethod) { AnnotatedMethod am = (AnnotatedMethod) a; if(am.getParameterCount() == 1) { javaType = am.getParameterType(0); } else { throw new RuntimeException("Invalid property setter: " + am.getAnnotated()); } } return new KvInterceptorsDeserializationConverter(interceptors, new KvPropertyContextImpl(a, javaType)); }
@Before public void setup() { typeResolver = mock(TypeResolver.class); configuration = Configuration.build(); instantiator = mock(HandlerInstantiator.class); doReturn(new ResourceDeserializer(Object.class, typeResolver, configuration)) .when(instantiator).deserializerInstance(any(DeserializationConfig.class), any(Annotated.class), eq(ResourceDeserializer.class)); mapper = new ObjectMapper(); mapper.setHandlerInstantiator(instantiator); mapper.registerModule(new Jackson2HalModule()); mapper.registerModule(new TestModule()); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); }
@Override public PropertyName findNameForSerialization(Annotated a) { if (!(a instanceof AnnotatedField) && !(a instanceof AnnotatedMethod)) { return super.findNameForSerialization(a); } IndexableProperty property = a.getAnnotation(IndexableProperty.class); if (property != null && !property.name().isEmpty()) { return new PropertyName(property.name()); } IndexableComponent component = a.getAnnotation(IndexableComponent.class); if (component != null && !component.name().isEmpty()) { return new PropertyName(component.name()); } IndexableProperties properties = a.getAnnotation(IndexableProperties.class); if (properties != null && !properties.name().isEmpty()) { return new PropertyName(properties.name()); } return PropertyName.USE_DEFAULT; }
@Override public JsonInclude.Include findSerializationInclusion(Annotated a, JsonInclude.Include defValue) { IndexableProperty property = a.getAnnotation(IndexableProperty.class); if (property != null && property.jsonInclude() != com.github.aureliano.evtbridge.output.elasticsearch.enumeration.JsonInclude.DEFAULT) { return JsonInclude.Include.valueOf(property.jsonInclude().toString()); } IndexableComponent component = a.getAnnotation(IndexableComponent.class); if (component != null && component.jsonInclude() != com.github.aureliano.evtbridge.output.elasticsearch.enumeration.JsonInclude.DEFAULT) { return JsonInclude.Include.valueOf(component.jsonInclude().toString()); } IndexableProperties properties = a.getAnnotation(IndexableProperties.class); if (properties != null && properties.jsonInclude() != com.github.aureliano.evtbridge.output.elasticsearch.enumeration.JsonInclude.DEFAULT) { return JsonInclude.Include.valueOf(properties.jsonInclude().toString()); } return defValue; }
@Override public PropertyName findNameForDeserialization(Annotated a) { if (!(a instanceof AnnotatedField) && !(a instanceof AnnotatedMethod)) { return super.findNameForDeserialization(a); } IndexableProperty property = a.getAnnotation(IndexableProperty.class); if (property != null && !property.name().isEmpty()) { return new PropertyName(property.name()); } IndexableComponent component = a.getAnnotation(IndexableComponent.class); if (component != null && !component.name().isEmpty()) { return new PropertyName(component.name()); } IndexableProperties properties = a.getAnnotation(IndexableProperties.class); if (properties != null && !properties.name().isEmpty()) { return new PropertyName(properties.name()); } return PropertyName.USE_DEFAULT; }
@Override public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException { if (property != null) { JsonFormat.Value format = ctxt.getAnnotationIntrospector().findFormat((Annotated) property.getMember()); if (format != null) { if (format.hasPattern()) { final String pattern = format.getPattern(); final Locale locale = format.hasLocale() ? format.getLocale() : ctxt.getLocale(); DateTimeFormatter df; if (locale == null) { df = DateTimeFormatter.ofPattern(pattern); } else { df = DateTimeFormatter.ofPattern(pattern, locale); } return withDateFormat(df); } // any use for TimeZone? } } return this; }
@Override public Object findSerializer( Annotated annotated ) { if( ! annotated.hasAnnotation(Column.class) || ! isStringType(annotated) ) { return super.findSerializer( annotated ); } Class classType = annotated.getRawType(); if( Types.isNotString(classType) ) { if( Types.isBoolean(classType) ) { return ColumnBooleanSerializer.class; } else if( ! Types.isPrimitive(classType) ) { return ColumnBeanSerializer.class; } } return super.findSerializer( annotated ); }
@Override public Object findDeserializer( Annotated annotated ) { if( ! annotated.hasAnnotation(Column.class) || ! isStringType(annotated) ) { return super.findDeserializer( annotated ); } Class classType; try { classType = ( (AnnotatedMethod) annotated ).getRawParameterType( 0 ); } catch( Exception e ) { classType = annotated.getRawType(); } if( Types.isNotString(classType) ) { if( Types.isBoolean(classType) ) { return ColumnBooleanDeserializer.class; } else if( ! Types.isPrimitive(classType) ) { return ColumnBeanDeserializer.class; } } return super.findDeserializer( annotated ); }
private PropertyName getPropertyName( Annotated annotated ) { Column column = annotated.getAnnotation( Column.class ); if ( column != null && StringUtil.isNotEmpty(annotated.getName()) ) { String name = column.name(); name = StringUtil.toUncamel( name ); name = StringUtil.toCamel( name ); return new PropertyName( name ); } JsonProperty jsonProperty = annotated.getAnnotation( JsonProperty.class ); if( jsonProperty != null ) { if( StringUtil.isNotEmpty(jsonProperty.value()) ) { return new PropertyName( jsonProperty.value() ); } else { return PropertyName.USE_DEFAULT; } } return null; }
@Override public String findPropertyDescription(Annotated a) { ApiParam apiParam = a.getAnnotation(ApiParam.class); if (apiParam != null) { return apiParam.description(); } ApiModel model = a.getAnnotation(ApiModel.class); if (model != null && !"".equals(model.description())) { return model.description(); } ApiModelProperty prop = a.getAnnotation(ApiModelProperty.class); if (prop != null) { return prop.value(); } return null; }
@Override public List<NamedType> findSubtypes(Annotated a) { final ApiModel api = a.getAnnotation(ApiModel.class); if (api != null) { final Class<?>[] classes = api.subTypes(); final List<NamedType> names = new ArrayList<>(classes.length); for (Class<?> subType : classes) { names.add(new NamedType(subType)); } if (!names.isEmpty()) { return names; } } return Collections.emptyList(); }
public ValueInstantiator _valueInstantiatorInstance(DeserializationConfig paramDeserializationConfig, Annotated paramAnnotated, Object paramObject) { if (paramObject == null) return null; if ((paramObject instanceof ValueInstantiator)) return (ValueInstantiator)paramObject; if (!(paramObject instanceof Class)) throw new IllegalStateException("AnnotationIntrospector returned key deserializer definition of type " + paramObject.getClass().getName() + "; expected type KeyDeserializer or Class<KeyDeserializer> instead"); Class localClass = (Class)paramObject; if (localClass == NoClass.class) return null; if (!ValueInstantiator.class.isAssignableFrom(localClass)) throw new IllegalStateException("AnnotationIntrospector returned Class " + localClass.getName() + "; expected Class<ValueInstantiator>"); HandlerInstantiator localHandlerInstantiator = paramDeserializationConfig.getHandlerInstantiator(); if (localHandlerInstantiator != null) { ValueInstantiator localValueInstantiator = localHandlerInstantiator.valueInstantiatorInstance(paramDeserializationConfig, paramAnnotated, localClass); if (localValueInstantiator != null) return localValueInstantiator; } return (ValueInstantiator)ClassUtil.createInstance(localClass, paramDeserializationConfig.canOverrideAccessModifiers()); }
public PropertyName findNameForDeserialization(Annotated paramAnnotated) { String str; if ((paramAnnotated instanceof AnnotatedField)) str = findDeserializationName((AnnotatedField)paramAnnotated); else if ((paramAnnotated instanceof AnnotatedMethod)) str = findDeserializationName((AnnotatedMethod)paramAnnotated); else if ((paramAnnotated instanceof AnnotatedParameter)) str = findDeserializationName((AnnotatedParameter)paramAnnotated); else str = null; if (str != null) { if (str.length() == 0) return PropertyName.USE_DEFAULT; return new PropertyName(str); } return null; }
public PropertyName findNameForSerialization(Annotated paramAnnotated) { String str; if ((paramAnnotated instanceof AnnotatedField)) str = findSerializationName((AnnotatedField)paramAnnotated); else if ((paramAnnotated instanceof AnnotatedMethod)) str = findSerializationName((AnnotatedMethod)paramAnnotated); else str = null; if (str != null) { if (str.length() == 0) return PropertyName.USE_DEFAULT; return new PropertyName(str); } return null; }
@Override public Object findSerializer(Annotated a) { final JsonId ann = a.getAnnotation(JsonId.class); if (ann != null) { return new JsonSerializer<Long>() { @Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(baseUrl + ann.path().replace("{id}", value.toString())); } }; } else { return super.findSerializer(a); } }
@Override public boolean hasCreatorAnnotation(Annotated annotated) { if (super.hasCreatorAnnotation(annotated)) { return true; } else if (!(annotated instanceof AnnotatedConstructor)) { return false; } else { AnnotatedConstructor annotatedConstructor = (AnnotatedConstructor) annotated; ConstructorProperties properties = getConstructorPropertiesAnnotation(annotatedConstructor); if (properties == null) { return false; } else { addJacksonAnnotationsToContructorParameters(annotatedConstructor); return true; } } }
@Override public JsonDeserializer<?> deserializerInstance(DeserializationConfig config, Annotated annotated, Class<?> deserClass) { try{ if(MigrationDeserializer.class.equals(deserClass)) { return migrationDeserializer; } else if(None.class.equals(deserClass)) { return null; } System.out.println("DeserializationConfig " + config); System.out.println("Annotated " + annotated); System.out.println("deserClass Class<?> " + deserClass); return (JsonDeserializer<?>) deserClass.newInstance(); }catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
/** report all non-alias plugin types */ @Override public List<NamedType> findSubtypes(Annotated a) { Pluggable pluggable = a.getAnnotation(Pluggable.class); PluginMap pluginMap; if (pluggable != null) { pluginMap = pluginRegistry.byCategory().get(pluggable.value()); } else if (pluginRegistry.byClass().containsKey(a.getRawType())) { pluginMap = pluginRegistry.byClass().get(a.getRawType()); } else { return null; } List<NamedType> result = new ArrayList<>(pluginMap.asBiMap().size()); for (Map.Entry<String, Class<?>> type : pluginMap.asBiMap().entrySet()) { result.add(new NamedType(type.getValue(), type.getKey())); } return result; }
@Override public Object findSerializer(Annotated a) { AnnotatedElement ae = a.getAnnotated(); Url an = ae.getAnnotation(Url.class); if (an == null) { return null; } if (an.type() == String.class) { return new UriSerializer(an); } else if (an.type() == List.class) { return new UrisSerializer(an); } throw new UnsupportedOperationException("Unsupported type " + an.type()); }
@Override public ObjectIdGenerator<?> objectIdGeneratorInstance(Annotated annotated, ObjectIdInfo objectIdInfo) throws JsonMappingException { Class<?> implClass = objectIdInfo.getGeneratorType(); HandlerInstantiator hi = _config.getHandlerInstantiator(); ObjectIdGenerator<?> gen; if (hi != null) { gen = hi.objectIdGeneratorInstance(_config, annotated, implClass); } else { gen = (ObjectIdGenerator<?>) ClassUtil.createInstance(implClass, _config.canOverrideAccessModifiers()); } return gen.forScope(objectIdInfo.getScope()); }
private String getName(Annotated ann) { AnnotatedElement element = ann.getAnnotated(); if (element instanceof Field) return ((Field) element).getName(); if (element instanceof java.lang.reflect.Method) { String name = ((java.lang.reflect.Method) element).getName(); if (name.startsWith("is")) name = name.substring(2); if (name.startsWith("get")) name = name.substring(3); if (name.length() < 2) return name; return name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1); } return null; }
/** * Apply the {@link JsonWrapperIntrospector#filterName} filter to all instances of Map. * * @param a objects to be serialized * @return either the specified filter or a the default supplied by the parent. * @see JacksonAnnotationIntrospector */ @Override public Object findFilterId(Annotated a) { if (Map.class.isAssignableFrom(a.getRawType())) { return filterName; } return super.findFilterId(a); }
@Override public Object findFilterId(Annotated a) { Object id = super.findFilterId(a); if (id == null) { id = JAVASSIST_FILTER_ID; } return id; }
@Override public Object findFilterId(Annotated a) { Object id = super.findFilterId(a); if (id == null) { id = CGLIB_FILTER_ID; } return id; }
@SuppressFBWarnings(value = "SIC_INNER_SHOULD_BE_STATIC_ANON", justification = "This is required") @SuppressWarnings("unchecked") @Override protected <A extends Annotation> A _findAnnotation( final Annotated annotated, final Class<A> annoClass) { A annotation = super._findAnnotation(annotated, annoClass); if (null == annotation) { annotation = (A) delegateAnnotations.get(annoClass); } return annotation; }
@SuppressWarnings("unchecked") @Override public Object findSerializer(Annotated am) { if (am.hasAnnotation(ManyToOne.class)) { return new ManyToOneSerializer((Class<AbstractEntity>) am.getRawType()); } else { return super.findDeserializer(am); } }
@Override public Object findDeserializer(Annotated am) { if (am.hasAnnotation(ManyToOne.class)) { return new ManyToOneDeserializer(am.getRawType()); } else { return super.findDeserializer(am); } }
@Override public Object findSerializer(Annotated am) { MaskableSensitiveData annotation = am.getAnnotation(MaskableSensitiveData.class); if (annotation != null) { return MaskableSerializerFactory.createSerializer(am.getType(), annotation); } return null; }
protected Class<?> rawDeserializationType(Annotated a) { if (a instanceof AnnotatedMethod) { AnnotatedMethod am = (AnnotatedMethod) a; if (am.getParameterCount() == 1) { return am.getRawParameterType(0); } } return a.getRawType(); }
@Override public Object findSerializer(Annotated am) { if (am.hasAnnotation(Obfuscate.class)) { return OBFUSCATE_SERIALIZER; } else { return null; } }
@Override public Object findSerializer(Annotated am) { // System.err.println("am:" + am.getName()); return null; // Object serializer = super.findSerializer(am); // if (serializer != null) { // Class<?> clazz = (Class<?>) serializer; // String className = clazz.getName(); // if (className.endsWith("ProvinceJsonSerializer")) { // return null; // } // } // return serializer; }
@Override public Object findSerializer(Annotated a) { if (a instanceof AnnotatedMethod) { AnnotatedElement m = a.getAnnotated(); DateTimeFormat an = m.getAnnotation(DateTimeFormat.class); if (an != null) { if (!DEFAULT_DATE_FORMAT.equals(an.pattern())) { return new JsonDateSerializer(an.pattern()); } } } return super.findSerializer(a); }