Java Code Examples for com.fasterxml.jackson.datatype.jsr310.JavaTimeModule#addDeserializer()
The following examples show how to use
com.fasterxml.jackson.datatype.jsr310.JavaTimeModule#addDeserializer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomConfig.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
private void setConfigForJdk8(ObjectMapper objectMapper) { JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addSerializer(Date.class, new DateSerializer()); timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .registerModule(timeModule).registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()); }
Example 2
Source File: CustomConfig.java From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International | 6 votes |
private void setConfigForJdk8(ObjectMapper objectMapper) { JavaTimeModule timeModule = new JavaTimeModule(); timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); timeModule.addSerializer(Date.class, new DateSerializer()); timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .registerModule(timeModule).registerModule(new ParameterNamesModule()) .registerModule(new Jdk8Module()); }
Example 3
Source File: ApplicationTests.java From oauth2-client with MIT License | 6 votes |
@Ignore @Test public void contextLoads() { //用登陆后的token ,请求api资源 //header格式,Authorization : Bearer xxxxx RestTemplate restTemplate = new RestTemplate(); JavaTimeModule module = new JavaTimeModule(); LocalDateTimeDeserializer localDateTimeDeserializer = new LocalDateTimeDeserializer( DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); module.addDeserializer(LocalDateTime.class, localDateTimeDeserializer); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).build(); MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter(); jsonMessageConverter.setObjectMapper(objectMapper); restTemplate.getMessageConverters().add(0, jsonMessageConverter); // request some api }
Example 4
Source File: _TestUtil.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 6 votes |
/** * Convert an object to JSON byte array. * * @param object * the object to convert * @return the JSON byte array * @throws IOException */ public static byte[] convertObjectToJsonBytes(Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); mapper.registerModule(module); return mapper.writeValueAsBytes(object); }
Example 5
Source File: TestUtil.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 6 votes |
/** * Convert an object to JSON byte array. * * @param object * the object to convert * @return the JSON byte array * @throws IOException */ public static byte[] convertObjectToJsonBytes(Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); mapper.registerModule(module); return mapper.writeValueAsBytes(object); }
Example 6
Source File: JacksonConfig.java From smaker with GNU Lesser General Public License v3.0 | 6 votes |
/** * 针对JDK 1.8的日期时间格式特殊处理 * * @return ObjectMapper */ @Bean public ObjectMapper getObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss"))); // javaTimeModule只能手动注册,参考https://github.com/FasterXML/jackson-modules-java8 objectMapper.registerModule(javaTimeModule); // 忽略json字符串中不识别的属性 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 忽略无法转换的对象 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); return objectMapper; }
Example 7
Source File: TestUtil.java From gpmr with Apache License 2.0 | 6 votes |
/** * Convert an object to JSON byte array. * * @param object * the object to convert * @return the JSON byte array * @throws IOException */ public static byte[] convertObjectToJsonBytes(Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); mapper.registerModule(module); return mapper.writeValueAsBytes(object); }
Example 8
Source File: TestUtil.java From OpenIoE with Apache License 2.0 | 6 votes |
/** * Convert an object to JSON byte array. * * @param object * the object to convert * @return the JSON byte array * @throws IOException */ public static byte[] convertObjectToJsonBytes(Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); mapper.registerModule(module); return mapper.writeValueAsBytes(object); }
Example 9
Source File: TestUtil.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 6 votes |
/** * Convert an object to JSON byte array. * * @param object * the object to convert * @return the JSON byte array * @throws IOException */ public static byte[] convertObjectToJsonBytes(Object object) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); mapper.registerModule(module); return mapper.writeValueAsBytes(object); }
Example 10
Source File: JacksonConfiguration.java From klask-io with GNU General Public License v3.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); return new Jackson2ObjectMapperBuilder() .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 11
Source File: JacksonConfiguration.java From gpmr with Apache License 2.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); return new Jackson2ObjectMapperBuilder() .findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 12
Source File: JacksonConfiguration.java From OpenIoE with Apache License 2.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); return new Jackson2ObjectMapperBuilder() .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 13
Source File: JacksonConfiguration.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); return new Jackson2ObjectMapperBuilder() .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 14
Source File: JacksonConfiguration.java From jhipster-ribbon-hystrix with GNU General Public License v3.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addSerializer(OffsetDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(ZonedDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(LocalDateTime.class, JSR310DateTimeSerializer.INSTANCE); module.addSerializer(Instant.class, JSR310DateTimeSerializer.INSTANCE); module.addDeserializer(LocalDate.class, JSR310LocalDateDeserializer.INSTANCE); return new Jackson2ObjectMapperBuilder() .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 15
Source File: ZonedDateTimeObjectMapperCustomizer.java From quarkus with Apache License 2.0 | 5 votes |
@Override public void customize(ObjectMapper objectMapper) { JavaTimeModule customDateModule = new JavaTimeModule(); customDateModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer( new DateTimeFormatterBuilder().appendInstant(0).toFormatter().withZone(ZoneId.of("Z")))); customDateModule.addDeserializer(ZonedDateTime.class, new ZonedDateTimeEuropeLondonDeserializer()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .registerModule(customDateModule); }
Example 16
Source File: FilterObjectMapperBuilder.java From jfilter with Apache License 2.0 | 5 votes |
/** * Build configured ObjectMapper * * @return {@link ObjectMapper} */ public ObjectMapper build() { //Add mixin filter if filterFields isn't null and has filterable fields if(filterFields != null && filterFields.getFieldsMap().size() > 0) { objectMapper.addMixIn(Object.class, MixinFilter.class); objectMapper.setFilterProvider(new SimpleFilterProvider() .addFilter("com.jfilter.converter.MixinFilter", new MixinFilter(filterFields))); } //Set dateTimeModule if option is enabled if (serializationConfig.isDateTimeModuleEnabled()) { //Add JavaTimeModule to fix issue with LocalDate/LocalDateTime serialization JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE); javaTimeModule.addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE); javaTimeModule.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); javaTimeModule.addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE); javaTimeModule.addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE); javaTimeModule.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); objectMapper.registerModule(javaTimeModule); objectMapper.registerModule(new Jdk8Module()); objectMapper.findAndRegisterModules(); } return objectMapper; }
Example 17
Source File: JacksonConfiguration.java From celerio-angular-quickstart with Apache License 2.0 | 5 votes |
@Bean Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { JavaTimeModule module = new JavaTimeModule(); module.addDeserializer(LocalDate.class, new CustomLocalDateDeserializer()); module.addSerializer(LocalDate.class, new CustomLocalDateSerializer()); module.addDeserializer(LocalDateTime.class, new CustomLocalDateTimeDeserializer()); module.addSerializer(LocalDateTime.class, new CustomLocalDateTimeSerializer()); return new Jackson2ObjectMapperBuilder().featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).findModulesViaServiceLoader(true) .modulesToInstall(module); }
Example 18
Source File: Jackson2ObjectMapperBuilderTests.java From spring-analysis-note with MIT License | 5 votes |
@Test // gh-22576 public 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); assertNotNull(demoPojo.getOffsetDateTime()); }
Example 19
Source File: WebConfig.java From hdw-dubbo with Apache License 2.0 | 4 votes |
@Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); SimpleModule simpleModule = new SimpleModule(); // Long类型序列化成字符串,避免Long精度丢失 simpleModule.addSerializer(Long.class, ToStringSerializer.instance); simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); // XSS序列化 simpleModule.addSerializer(String.class, new XssJacksonSerializer()); simpleModule.addDeserializer(String.class, new XssJacksonDeserializer()); // Date序列化 simpleModule.addSerializer(Date.class, new JacksonDateSerializer()); simpleModule.addDeserializer(Date.class, new JacksonDateDeserializer()); // Integer、Double反序列化 simpleModule.addDeserializer(Integer.class, new JacksonIntegerDeserializer()); simpleModule.addDeserializer(Double.class, new JacksonDoubleDeserializer()); // jdk8日期序列化和反序列化设置 JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN))); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN))); objectMapper.registerModule(simpleModule) .registerModule(javaTimeModule).registerModule(new ParameterNamesModule()); jackson2HttpMessageConverter.setObjectMapper(objectMapper); //放到第一个 converters.add(0, jackson2HttpMessageConverter); }
Example 20
Source File: JacksonConfig.java From spring-boot-plus with Apache License 2.0 | 4 votes |
@Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); SimpleModule simpleModule = new SimpleModule(); // Long类型序列化成字符串,避免Long精度丢失 // simpleModule.addSerializer(Long.class, ToStringSerializer.instance); // simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance); // XSS序列化 if (enableXss){ simpleModule.addSerializer(String.class, new XssJacksonSerializer()); simpleModule.addDeserializer(String.class, new XssJacksonDeserializer()); } // Date simpleModule.addSerializer(Date.class, new JacksonDateSerializer()); simpleModule.addDeserializer(Date.class, new JacksonDateDeserializer()); simpleModule.addDeserializer(Integer.class, new JacksonIntegerDeserializer()); simpleModule.addDeserializer(Double.class, new JacksonDoubleDeserializer()); // jdk8日期序列化和反序列化设置 JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD_HH_MM_SS))); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD_HH_MM_SS))); javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD))); javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(DatePattern.YYYY_MM_DD))); javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(DatePattern.HH_MM_SS))); javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DatePattern.HH_MM_SS))); objectMapper.registerModule(simpleModule).registerModule(javaTimeModule).registerModule(new ParameterNamesModule()); jackson2HttpMessageConverter.setObjectMapper(objectMapper); //放到第一个 converters.add(0, jackson2HttpMessageConverter); }