List of usage examples for com.fasterxml.jackson.databind ObjectMapper setTimeZone
public ObjectMapper setTimeZone(TimeZone tz)
From source file:org.emfjson.jackson.module.EMFModule.java
/** * Returns a pre configured mapper with the EMF module. * * @return mapper//from w w w. j a v a 2 s . co m */ public static ObjectMapper setupDefaultMapper() { final ObjectMapper mapper = new ObjectMapper(); // same as emf final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH); dateFormat.setTimeZone(TimeZone.getDefault()); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.setDateFormat(dateFormat); mapper.setTimeZone(TimeZone.getDefault()); mapper.registerModule(new EMFModule()); return mapper; }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to TRUE * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone * It also tests the serialization and deserialization process * /*from w ww . j a va2s. c o m*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception { Field field = classWhenConfigIsTrue.getDeclaredField("customFormatDefaultTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd'T'HH:mm:ss", annotation.pattern()); // Assert that the timezones match assertEquals("UTC", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("UTC")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatDefaultTZ", "2016-11-06T00:00:00"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue); Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsTrue).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06T00:00:00", jsonVersion.get("customFormatDefaultTZ").asText()); }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to TRUE * The field should have @JsonFormat annotation with pattern and timezone defined in json schema * It also tests the serialization and deserialization process * /*from w w w . jav a 2s.com*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsTrue() throws Exception { Field field = classWhenConfigIsTrue.getDeclaredField("customFormatCustomTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd", annotation.pattern()); // Assert that the timezones match assertEquals("PST", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("PST")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatCustomTZ", "2016-11-06"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue); Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsTrue).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateFormatter.parse("2016-11-06").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06", jsonVersion.get("customFormatCustomTZ").asText()); }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to FALSE * The field should have @JsonFormat annotation with pattern defined in json schema and UTC timezone * It also tests the serialization and deserialization process * /*from ww w . j a v a2s . c o m*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithDefaultTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception { Field field = classWhenConfigIsFalse.getDeclaredField("customFormatDefaultTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd'T'HH:mm:ss", annotation.pattern()); // Assert that the timezones match assertEquals("UTC", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("UTC")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatDefaultTZ", "2016-11-06T00:00:00"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsFalse); Method getter = new PropertyDescriptor("customFormatDefaultTZ", classWhenConfigIsFalse).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateTimeFormatter.parse("2016-11-06T00:00:00").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06T00:00:00", jsonVersion.get("customFormatDefaultTZ").asText()); }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to FALSE * The field should have @JsonFormat annotation with pattern and timezone defined in json schema * It also tests the serialization and deserialization process * /*www . j a va 2 s.com*/ * @throws Exception */ @Test public void testCustomDateTimePatternWithCustomTimezoneWhenFormatDateTimesConfigIsFalse() throws Exception { Field field = classWhenConfigIsFalse.getDeclaredField("customFormatCustomTZ"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd", annotation.pattern()); // Assert that the timezones match assertEquals("PST", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("PST")); ObjectNode node = objectMapper.createObjectNode(); node.put("customFormatCustomTZ", "2016-11-06"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsFalse); Method getter = new PropertyDescriptor("customFormatCustomTZ", classWhenConfigIsFalse).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateFormatter.parse("2016-11-06").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06", jsonVersion.get("customFormatCustomTZ").asText()); }
From source file:org.jsonschema2pojo.integration.CustomDateTimeFormatIT.java
/** * This tests the class generated when formatDateTimes config option is set to TRUE * The field should have @JsonFormat annotation with iso8601 date time pattern and UTC timezone * It also tests the serialization and deserialization process * //from www . j a va 2s . c om * @throws Exception */ @Test public void testDefaultWhenFormatDateTimesConfigIsTrue() throws Exception { Field field = classWhenConfigIsTrue.getDeclaredField("defaultFormat"); JsonFormat annotation = field.getAnnotation(JsonFormat.class); assertThat(annotation, notNullValue()); // Assert that the patterns match assertEquals("yyyy-MM-dd'T'HH:mm:ss.SSS", annotation.pattern()); // Assert that the timezones match assertEquals("UTC", annotation.timezone()); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setTimeZone(TimeZone.getTimeZone("UTC")); ObjectNode node = objectMapper.createObjectNode(); node.put("defaultFormat", "2016-11-06T00:00:00.000"); Object pojo = objectMapper.treeToValue(node, classWhenConfigIsTrue); Method getter = new PropertyDescriptor("defaultFormat", classWhenConfigIsTrue).getReadMethod(); // Assert that the Date object in the deserialized class is as expected assertEquals(dateTimeMilliSecFormatter.parse("2016-11-06T00:00:00.000").toString(), getter.invoke(pojo).toString()); JsonNode jsonVersion = objectMapper.valueToTree(pojo); // Assert that when the class is serialized, the date object is serialized as expected assertEquals("2016-11-06T00:00:00.000", jsonVersion.get("defaultFormat").asText()); }
From source file:com.wiiyaya.consumer.web.initializer.config.WebConfig.java
/** * ???/*from ww w .ja v a2 s. c o m*/ */ @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(ConfigConstant.SYSTEM_CHARSET); stringConverter.setWriteAcceptCharset(false); converters.add(new ByteArrayHttpMessageConverter()); converters.add(stringConverter); converters.add(new ResourceHttpMessageConverter()); converters.add(new SourceHttpMessageConverter<Source>()); converters.add(new AllEncompassingFormHttpMessageConverter()); converters.add(new Jaxb2RootElementHttpMessageConverter()); MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(); ObjectMapper om = jacksonConverter.getObjectMapper(); om.setDateFormat(new SimpleDateFormat(ConfigConstant.SYSTEM_DATE_FORMAT)); om.setLocale(ConfigConstant.SYSTEM_LOCALE); om.setTimeZone(TimeZone.getTimeZone(ConfigConstant.SYSTEM_TIME_ZONE)); om.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { gen.writeString(StringUtils.EMPTY); } }); om.configure(Feature.WRITE_NUMBERS_AS_STRINGS, true); om.configure(Feature.WRITE_BIGDECIMAL_AS_PLAIN, true); List<MediaType> mediaTypeList = new ArrayList<MediaType>(); mediaTypeList.add(new MediaType("text", "plain", ConfigConstant.SYSTEM_CHARSET)); jacksonConverter.setSupportedMediaTypes(mediaTypeList); converters.add(jacksonConverter); }
From source file:com.feedeo.rest.client.AbstractRestClient.java
protected ObjectMapper createObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(WRITE_DATES_AS_TIMESTAMPS, true); objectMapper.configure(WRITE_ENUMS_USING_TO_STRING, true); objectMapper.configure(READ_ENUMS_USING_TO_STRING, true); objectMapper.registerModule(new JodaModule()); objectMapper.setTimeZone(getDefault()); return objectMapper; }
From source file:org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.java
/** * Configure an existing {@link ObjectMapper} instance with this builder's * settings. This can be applied to any number of {@code ObjectMappers}. * @param objectMapper the ObjectMapper to configure *//*from w ww . j av a 2 s .c om*/ public void configure(ObjectMapper objectMapper) { Assert.notNull(objectMapper, "ObjectMapper must not be null"); if (this.findModulesViaServiceLoader) { // Jackson 2.2+ objectMapper.registerModules(ObjectMapper.findModules(this.moduleClassLoader)); } else if (this.findWellKnownModules) { registerWellKnownModulesIfAvailable(objectMapper); } if (this.modules != null) { for (Module module : this.modules) { // Using Jackson 2.0+ registerModule method, not Jackson 2.2+ registerModules objectMapper.registerModule(module); } } if (this.moduleClasses != null) { for (Class<? extends Module> module : this.moduleClasses) { objectMapper.registerModule(BeanUtils.instantiateClass(module)); } } if (this.dateFormat != null) { objectMapper.setDateFormat(this.dateFormat); } if (this.locale != null) { objectMapper.setLocale(this.locale); } if (this.timeZone != null) { objectMapper.setTimeZone(this.timeZone); } if (this.annotationIntrospector != null) { objectMapper.setAnnotationIntrospector(this.annotationIntrospector); } if (this.propertyNamingStrategy != null) { objectMapper.setPropertyNamingStrategy(this.propertyNamingStrategy); } if (this.defaultTyping != null) { objectMapper.setDefaultTyping(this.defaultTyping); } if (this.serializationInclusion != null) { objectMapper.setSerializationInclusion(this.serializationInclusion); } if (this.filters != null) { objectMapper.setFilterProvider(this.filters); } for (Class<?> target : this.mixIns.keySet()) { objectMapper.addMixIn(target, this.mixIns.get(target)); } if (!this.serializers.isEmpty() || !this.deserializers.isEmpty()) { SimpleModule module = new SimpleModule(); addSerializers(module); addDeserializers(module); objectMapper.registerModule(module); } customizeDefaultFeatures(objectMapper); for (Object feature : this.features.keySet()) { configureFeature(objectMapper, feature, this.features.get(feature)); } if (this.handlerInstantiator != null) { objectMapper.setHandlerInstantiator(this.handlerInstantiator); } else if (this.applicationContext != null) { objectMapper.setHandlerInstantiator( new SpringHandlerInstantiator(this.applicationContext.getAutowireCapableBeanFactory())); } }