public TicketsResponse checkTickets(TicketsRequest request) { String jsonResp = null; TicketsResponse response = null; TicketsResponseError responseError = null; try { jsonResp = sendRequest(request); try { ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); response = mapper.readValue(jsonResp, TicketsResponse.class); } catch (JsonMappingException e) { // logger.error("Could not parse JSON, trying to parse an error. Original exception: ", // e); responseError = new ObjectMapper().readValue(jsonResp, TicketsResponseError.class); TicketsResponse invaildResponse = new TicketsResponse(); invaildResponse.setError(true); invaildResponse.setErrorDescription(responseError.getErrorDescription()); logger.error("UzGovUa error description: " + responseError.getErrorDescription()); return invaildResponse; } } catch (HttpException e) { e.printStackTrace(); logger.error(e.getMessage(), e); } catch (SocketTimeoutException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } return response; }
/** * Configure the Jackson {@link ObjectMapper}. Use the {@link ISO8601DateFormatWithMilliSeconds} * to set a custom {@link DateFormat} ensuring that JSON Data are serialized using the {@code * ISO8601} format. */ @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper objectMapper = super.objectMapper(); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.setDateFormat(new ISO8601DateFormatWithMilliSeconds()); return objectMapper; }
@Bean public ObjectMapper objectMapper() { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.setDateFormat(new ISO8601DateFormat()); objectMapper.registerModule(new JodaModule()); return objectMapper; }
@Bean public ObjectMapper jacksonObjectMapper() { DateFormat df = new SimpleDateFormat(timeformat); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setDateFormat(df); return objectMapper; }
@Test public void whenDeserializingDateWithJackson_thenCorrect() throws JsonProcessingException, IOException { final String json = "{\"name\":\"party\",\"eventDate\":\"20-12-2014 02:30:00\"}"; final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss"); final ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(df); final Event event = mapper.reader(Event.class).readValue(json); assertEquals("20-12-2014 02:30:00", df.format(event.eventDate)); }
@Bean public ObjectMapper jacksonObjectMapper() { ObjectMapper om = new ObjectMapper(); om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); AnnotationIntrospector primary = new JacksonAnnotationIntrospector(); // JaxbAnnotationIntrospector jai = new JaxbAnnotationIntrospector(om.getTypeFactory()); // AnnotationIntrospector pair = AnnotationIntrospectorPair.create(primary, secondary); om.setAnnotationIntrospector(primary); om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); om.setSerializationInclusion(Include.NON_NULL); return om; }
private static ObjectMapper createMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); mapper.setDateFormat(dateFormat); mapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance())); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true); mapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true); addQueryMapper(mapper); return mapper; }
@Test public void whenSettingObjectMapperDateFormat_thenCorrect() throws JsonProcessingException, ParseException { final SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy hh:mm"); final String toParse = "20-12-2014 02:30"; final Date date = df.parse(toParse); final Event event = new Event("party", date); final ObjectMapper mapper = new ObjectMapper(); mapper.setDateFormat(df); final String result = mapper.writeValueAsString(event); assertThat(result, containsString(toParse)); }
public ObjectMapper createBaseObjectMapper() { ObjectMapper mapper = new ObjectMapper(); JSONDateFormat jdf = new JSONDateFormat(); mapper.setDateFormat(jdf); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); if (enableAfterburner) { AfterburnerModule module = new AfterburnerModule(); module.setUseOptimizedBeanDeserializer(useOptimizedBeanDeserializer); module.setUseValueClassLoader(useValueClassLoader); mapper.registerModule(module); } applyNumericRangeBugfixes(mapper); return mapper; }
@Override protected void doSave(OutputStream outputStream, Map<?, ?> options) throws IOException { if (options == null) { options = Collections.<String, Object>emptyMap(); } if (outputStream instanceof URIConverter.Saveable) { ((URIConverter.Saveable) outputStream).saveResource(this); } else { final ObjectMapper mapper = new ObjectMapper(); final JacksonOptions jacksonOptions = getOptions(options); mapper.setDateFormat(jacksonOptions.dateFormat); mapper.configure(SerializationFeature.INDENT_OUTPUT, jacksonOptions.indentOutput); mapper.registerModule(new EMFModule(this.getResourceSet(), jacksonOptions)); outputStream.write(mapper.writeValueAsBytes(this)); } }
static { final SimpleModule module = new SimpleModule("SwfModule", new Version(1, 0, 0, null, null, null)) .addSerializer(Date.class, new EpochSecondsDateSerializer()) .addDeserializer(Date.class, new EpochSecondsDateDeserializer()); mapper.registerModule(module); mapper.setDateFormat(new EpochSecondsDateFormat()); mapper.addMixInAnnotations(SimpleWorkflowMessage.class, BindingMixIn.class); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); mapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS); mapper.setVisibilityChecker( new VisibilityChecker.Std(VisibilityChecker.Std.class.getAnnotation(JsonAutoDetect.class)) { private static final long serialVersionUID = 1L; @Override public boolean isSetterVisible(final Method m) { return !(m.getParameterCount() == 1 && m.getParameterTypes()[0].isEnum()) && super.isSetterVisible(m); } }); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); }
/** * Retrieves the Jackson object mapper. Internally, Object mapper is initialized at first call. */ static ObjectMapper getObjectMapper() { if (objectMapper == null) { objectMapper = new ObjectMapper(); objectMapper .setPropertyNamingStrategy( PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) // let's be tolerant .configure( MapperFeature.USE_GETTERS_AS_SETTERS, false) // not good for unmodifiable collections, if we will ever use any .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); // When reading dates, Jackson defaults to using GMT for all processing unless specifically // told otherwise, see http://wiki.fasterxml.com/JacksonFAQDateHandling // When writing dates, Jackson would add a Z for timezone by CKAN doesn't use it, i.e. // "2013-11-11T04:12:11.110868" so we removed it here objectMapper.setDateFormat( new SimpleDateFormat(CKAN_DATE_PATTERN)); // but this only works for Java Dates... objectMapper.registerModule(new GuavaModule()); } return objectMapper; }
@Override protected void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException { if (options == null) { options = Collections.<String, Object>emptyMap(); } if (inputStream instanceof URIConverter.Loadable) { ((URIConverter.Loadable) inputStream).loadResource(this); } else { final ObjectMapper mapper = new ObjectMapper(); final JacksonOptions jacksonOptions = getOptions(options); mapper.setDateFormat(jacksonOptions.dateFormat); mapper.registerModule(new EMFModule(this.getResourceSet(), jacksonOptions)); ContextAttributes attributes = ContextAttributes.getEmpty().withSharedAttribute("resource", this); mapper.reader().with(attributes).forType(Resource.class).readValue(inputStream); } }
static { mapper = new ObjectMapper(); mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")); mapper.enable(SerializationFeature.INDENT_OUTPUT); }
/** * 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 */ @SuppressWarnings("deprecation") 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.instantiate(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.serializationInclusion != null) { objectMapper.setSerializationInclusion(this.serializationInclusion); } if (this.filters != null) { // Deprecated as of Jackson 2.6, but just in favor of a fluent variant. objectMapper.setFilters(this.filters); } for (Class<?> target : this.mixIns.keySet()) { // Deprecated as of Jackson 2.5, but just in favor of a fluent variant. objectMapper.addMixInAnnotations(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())); } }