use of com.fasterxml.jackson.databind.type.TypeFactory in project incubator-servicecomb-java-chassis by apache.
the class AbstractConverter method convert.
@Override
public JavaType convert(ClassLoader classLoader, String packageName, Swagger swagger, Object def) {
TypeFactory typeFactory = TypeFactory.defaultInstance().withClassLoader(classLoader);
Map<String, Object> vendorExtensions = findVendorExtensions(def);
String canonical = ClassUtils.getVendorExtension(vendorExtensions, SwaggerConst.EXT_JAVA_CLASS);
if (!StringUtils.isEmpty(canonical)) {
Class<?> clsResult = ClassUtils.getClassByName(classLoader, canonical);
if (clsResult != null) {
return typeFactory.constructType(clsResult);
}
}
// ensure all depend model exist
// maybe create dynamic class by canonical
JavaType result = doConvert(classLoader, packageName, swagger, def);
String rawClassName = ClassUtils.getRawClassName(canonical);
if (StringUtils.isEmpty(rawClassName)) {
return result;
}
try {
JavaType rawType = typeFactory.constructFromCanonical(rawClassName);
if (rawType.getRawClass().getTypeParameters().length > 0) {
return typeFactory.constructFromCanonical(canonical);
}
return result;
} catch (IllegalArgumentException e) {
LOGGER.info("failed to load generic class {}, use {}. cause: {}.", rawClassName, result.getGenericSignature(), e.getMessage());
return result;
}
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project redisson by redisson.
the class ConfigSupport method createMapper.
private ObjectMapper createMapper(JsonFactory mapping, ClassLoader classLoader) {
ObjectMapper mapper = new ObjectMapper(mapping);
mapper.addMixIn(Config.class, ConfigMixIn.class);
mapper.addMixIn(ReferenceCodecProvider.class, ClassMixIn.class);
mapper.addMixIn(AddressResolverGroupFactory.class, ClassMixIn.class);
mapper.addMixIn(Codec.class, ClassMixIn.class);
mapper.addMixIn(RedissonNodeInitializer.class, ClassMixIn.class);
mapper.addMixIn(LoadBalancer.class, ClassMixIn.class);
mapper.addMixIn(NatMapper.class, ClassMixIn.class);
mapper.addMixIn(NameMapper.class, ClassMixIn.class);
mapper.addMixIn(NettyHook.class, ClassMixIn.class);
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("classFilter", SimpleBeanPropertyFilter.filterOutAllExcept());
mapper.setFilterProvider(filterProvider);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
if (classLoader != null) {
TypeFactory tf = TypeFactory.defaultInstance().withClassLoader(classLoader);
mapper.setTypeFactory(tf);
}
return mapper;
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project graylog2-server by Graylog2.
the class QueryTest method setup.
@Before
public void setup() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
final TypeFactory typeFactory = mapper.getTypeFactory().withClassLoader(this.getClass().getClassLoader());
this.objectMapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy()).setTypeFactory(typeFactory).registerModule(new GuavaModule()).registerModule(new JodaModule()).registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()).registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false)).registerModule(new SimpleModule("Graylog").addKeyDeserializer(Period.class, new JodaTimePeriodKeyDeserializer()).addSerializer(new RangeJsonSerializer()).addSerializer(new SizeSerializer()).addSerializer(new ObjectIdSerializer()));
// kludge because we don't have an injector in tests
ImmutableMap<String, Class> subtypes = ImmutableMap.<String, Class>builder().put(StreamFilter.NAME, StreamFilter.class).put(ElasticsearchQueryString.NAME, ElasticsearchQueryString.class).put(MessageList.NAME, MessageList.class).build();
subtypes.forEach((name, klass) -> objectMapper.registerSubtypes(new NamedType(klass, name)));
}
use of com.fasterxml.jackson.databind.type.TypeFactory in project yyl_example by Relucent.
the class JacksonTypeFactoryExample method createObjectMapper.
private static ObjectMapper createObjectMapper() {
ObjectMapper om = new ObjectMapper();
TypeFactory tf = TypeFactory.defaultInstance();
VisibilityChecker<?> visibility = om.getSerializationConfig().getDefaultVisibilityChecker();
visibility.withFieldVisibility(JsonAutoDetect.Visibility.ANY);
visibility.withGetterVisibility(JsonAutoDetect.Visibility.NONE);
visibility.withSetterVisibility(JsonAutoDetect.Visibility.NONE);
visibility.withCreatorVisibility(JsonAutoDetect.Visibility.NONE);
om.activateDefaultTyping(om.getPolymorphicTypeValidator(), DefaultTyping.NON_FINAL);
om.addMixIn(Throwable.class, ThrowableMixIn.class);
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
om.enable(Feature.WRITE_BIGDECIMAL_AS_PLAIN);
om.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY);
om.setSerializationInclusion(Include.ALWAYS);
om.setTypeFactory(tf);
om.setVisibility(visibility);
SimpleModule module = new SimpleModule();
module.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE);
module.addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE);
module.addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE);
module.addSerializer(Duration.class, DurationSerializer.INSTANCE);
module.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE);
module.addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE);
module.addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE);
module.addDeserializer(Duration.class, DurationDeserializer.INSTANCE);
om.registerModule(module);
return om;
}
Aggregations