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);
}
}
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;
}
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;
}
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;
}
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)));
}
Aggregations