Search in sources :

Example 31 with TypeFactory

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;
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 32 with TypeFactory

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;
}
Also used : SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider) SimpleFilterProvider(com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)

Example 33 with TypeFactory

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)));
}
Also used : SizeSerializer(org.graylog2.shared.jackson.SizeSerializer) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Period(org.joda.time.Period) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JodaTimePeriodKeyDeserializer(org.graylog2.jackson.JodaTimePeriodKeyDeserializer) MetricsModule(com.codahale.metrics.json.MetricsModule) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectIdSerializer(org.graylog2.database.ObjectIdSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) RangeJsonSerializer(org.graylog2.shared.rest.RangeJsonSerializer) MessageList(org.graylog.plugins.views.search.searchtypes.MessageList) Before(org.junit.Before)

Example 34 with TypeFactory

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;
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Aggregations

TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)34 JavaType (com.fasterxml.jackson.databind.JavaType)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 IOException (java.io.IOException)8 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 CollectionType (com.fasterxml.jackson.databind.type.CollectionType)4 MapType (com.fasterxml.jackson.databind.type.MapType)4 List (java.util.List)4 ArrayList (java.util.ArrayList)3 MetricsModule (com.codahale.metrics.json.MetricsModule)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 ArrayType (com.fasterxml.jackson.databind.type.ArrayType)2 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)2 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)2 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)2 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)2 Query (io.druid.query.Query)2