use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project xm-ms-entity by xm-online.
the class XmEntityResourceExtendedIntTest method convertObjectToJsonBytesByFields.
/**
* Convert an object to JSON byte array.
*
* @param object the object to convert
* @return the JSON byte array
*/
public static byte[] convertObjectToJsonBytesByFields(Object object) throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE).setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
JavaTimeModule module = new JavaTimeModule();
mapper.registerModule(module);
return mapper.writeValueAsBytes(object);
}
use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project xm-ms-entity by xm-online.
the class JsonDataValidator method initialize.
@Override
public void initialize(JsonData constraintAnnotation) {
log.trace("Json data validator inited");
objectMapper.registerModule(new JavaTimeModule());
}
use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project spring-framework by spring-projects.
the class Jackson2ObjectMapperBuilderTests method overrideWellKnownModuleWithModule.
// gh-22576
@Test
void overrideWellKnownModuleWithModule() throws IOException {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
builder.modulesToInstall(javaTimeModule);
builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
ObjectMapper objectMapper = builder.build();
DemoPojo demoPojo = objectMapper.readValue(DATA, DemoPojo.class);
assertThat(demoPojo.getOffsetDateTime()).isNotNull();
}
use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project dhis2-core by dhis2.
the class JacksonObjectMapperConfig method configureMapper.
/**
* Shared configuration for all Jackson mappers
*
* @param objectMapper an {@see ObjectMapper}
* @param autoDetectGetters if true, enable `autoDetectGetters`
* @return a configured {@see ObjectMapper}
*/
private static ObjectMapper configureMapper(ObjectMapper objectMapper, boolean autoDetectGetters) {
SimpleModule module = new SimpleModule();
module.addDeserializer(String.class, new EmptyStringToNullStdDeserializer());
module.addDeserializer(Date.class, new ParseDateStdDeserializer());
module.addDeserializer(JsonPointer.class, new JsonPointerStdDeserializer());
module.addSerializer(Date.class, new WriteDateStdSerializer());
module.addSerializer(JsonPointer.class, new JsonPointerStdSerializer());
// Registering a custom Instant serializer/deserializer for DTOs using
// Instant
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(Instant.class, new WriteInstantStdSerializer());
javaTimeModule.addDeserializer(Instant.class, new ParseInstantStdDeserializer());
objectMapper.registerModules(module, javaTimeModule, new Jdk8Module());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.enable(SerializationFeature.WRAP_EXCEPTIONS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
objectMapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
objectMapper.enable(DeserializationFeature.WRAP_EXCEPTIONS);
objectMapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS);
if (!autoDetectGetters) {
objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
}
objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
return objectMapper;
}
use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule 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)));
}
Aggregations