Search in sources :

Example 11 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project carina by qaprosoft.

the class JsonUtils method fromJson.

public static <T> T fromJson(String json, Type type) {
    try {
        TypeFactory tf = mapper.getTypeFactory();
        JavaType javaType = tf.constructType(type);
        return mapper.readValue(json, javaType);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 12 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project redisson by redisson.

the class JsonJacksonCodec method createObjectMapper.

protected static ObjectMapper createObjectMapper(ClassLoader classLoader, ObjectMapper om) {
    TypeFactory tf = TypeFactory.defaultInstance().withClassLoader(classLoader);
    om.setTypeFactory(tf);
    return om;
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory)

Example 13 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project yyl_example by Relucent.

the class YamlFactoryExample method createObjectMapper.

private static ObjectMapper createObjectMapper() throws JsonProcessingException {
    ObjectMapper om = new ObjectMapper(new YAMLFactory());
    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 : YAMLFactory(com.fasterxml.jackson.dataformat.yaml.YAMLFactory) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 14 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project dhis2-core by dhis2.

the class EventUtils method jsonToEventDataValues.

/**
 * Converts the Event Data Value json payload into a Set of EventDataValue
 *
 * Note that the EventDataValue payload is stored as a map: {dataelementid:{
 * ...}, {dataelementid:{ ...} }
 *
 * Therefore, the conversion is a bit convoluted, since the payload has to
 * be converted into a Map and then into a Set
 */
public static Set<EventDataValue> jsonToEventDataValues(ObjectMapper jsonMapper, Object eventsDataValues) throws JsonProcessingException {
    final TypeFactory typeFactory = jsonMapper.getTypeFactory();
    MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, EventDataValue.class);
    String content = null;
    if (eventsDataValues instanceof String) {
        content = (String) eventsDataValues;
    } else if (eventsDataValues instanceof PGobject) {
        content = ((PGobject) eventsDataValues).getValue();
    }
    Set<EventDataValue> dataValues = new HashSet<>();
    if (!StringUtils.isEmpty(content)) {
        Map<String, EventDataValue> parsed = jsonMapper.readValue(content, mapType);
        for (String dataElementId : parsed.keySet()) {
            EventDataValue edv = parsed.get(dataElementId);
            edv.setDataElement(dataElementId);
            dataValues.add(edv);
        }
    }
    return dataValues;
}
Also used : TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) MapType(com.fasterxml.jackson.databind.type.MapType) PGobject(org.postgresql.util.PGobject) HashSet(java.util.HashSet)

Example 15 with TypeFactory

use of com.fasterxml.jackson.databind.type.TypeFactory in project graylog2-server by Graylog2.

the class InMemorySearchJobServiceTest method setup.

@Test
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) Test(org.junit.Test)

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