@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder = new Jackson2ObjectMapperBuilder(); jackson2ObjectMapperBuilder.indentOutput(true); converters.add(new MappingJackson2HttpMessageConverter(jackson2ObjectMapperBuilder.build())); converters.add(new StringHttpMessageConverter()); }
private void configureFeatures(Jackson2ObjectMapperBuilder builder, Map<?, Boolean> features) { for (Entry<?, Boolean> entry : features.entrySet()) { if (entry.getValue() != null && entry.getValue()) { builder.featuresToEnable(entry.getKey()); } else { builder.featuresToDisable(entry.getKey()); } } }
private void configureDateFormat(Jackson2ObjectMapperBuilder builder) { // We support a fully qualified class name extending DateFormat or a date // pattern string value String dateFormat = this.jacksonProperties.getDateFormat(); if (dateFormat != null) { try { Class<?> dateFormatClass = ClassUtils.forName(dateFormat, null); builder.dateFormat((DateFormat) BeanUtils.instantiateClass(dateFormatClass)); } catch (ClassNotFoundException ex) { builder.dateFormat(new SimpleDateFormat(dateFormat)); } } }
@Bean @Primary public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); return objectMapper; }
/** * @param json A valid JSON stream, may be <code>null</code>. * @return The {@link DataSetMetadata} instance parsed from stream or <code>null</code> if * parameter is null. If stream is empty, also returns <code>null</code>. */ public DataSet from(InputStream json) { try { final ObjectMapper mapper = builder.build(); JsonParser parser = mapper.getFactory().createParser(json); return mapper.readerFor(DataSet.class).readValue(parser); } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e); } }
/** * Writes the current {@link DataSetMetadata} to <code>writer</code> as JSON format. * * @param writer A non-null writer. */ public void to(DataSet dataSet, Writer writer) { if (writer == null) { throw new IllegalArgumentException("Writer cannot be null."); } try { builder.build().writer().writeValue(writer, dataSet); } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_SERIALIZE_TO_JSON, e); } }
@Bean @ConditionalOnMissingBean(Jackson2ObjectMapperBuilder.class) public Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.applicationContext(this.applicationContext); if (this.jacksonProperties.getSerializationInclusion() != null) { builder.serializationInclusion(this.jacksonProperties.getSerializationInclusion()); } if (this.jacksonProperties.getTimeZone() != null) { builder.timeZone(this.jacksonProperties.getTimeZone()); } configureFeatures(builder, this.jacksonProperties.getDeserialization()); configureFeatures(builder, this.jacksonProperties.getSerialization()); configureFeatures(builder, this.jacksonProperties.getMapper()); configureFeatures(builder, this.jacksonProperties.getParser()); configureFeatures(builder, this.jacksonProperties.getGenerator()); configureDateFormat(builder); configurePropertyNamingStrategy(builder); configureModules(builder); return builder; }
private void configurePropertyNamingStrategyField( Jackson2ObjectMapperBuilder builder, String fieldName) { // Find the field (this way we automatically support new constants // that may be added by Jackson in the future) Field field = ReflectionUtils.findField( PropertyNamingStrategy.class, fieldName, PropertyNamingStrategy.class); Assert.notNull( field, "Constant named '" + fieldName + "' not found on " + PropertyNamingStrategy.class.getName()); try { builder.propertyNamingStrategy((PropertyNamingStrategy) field.get(null)); } catch (Exception ex) { throw new IllegalStateException(ex); } }
@Test public void should_iterate_row_with_metadata() throws IOException { // given String[] columnNames = new String[] { "id", "firstname", "lastname", "state", "registration", "city", "birth", "nbCommands", "avgAmount" }; final InputStream input = this.getClass().getResourceAsStream("dataSetRowMetadata.json"); final ObjectMapper mapper = builder.build(); try (JsonParser parser = mapper.getFactory().createParser(input)) { final DataSet dataSet = mapper.readerFor(DataSet.class).readValue(parser); final Iterator<DataSetRow> iterator = dataSet.getRecords().iterator(); List<ColumnMetadata> actualColumns = new ArrayList<>(); int recordCount = 0; while (iterator.hasNext()) { final DataSetRow next = iterator.next(); actualColumns = next.getRowMetadata().getColumns(); assertThat(actualColumns, not(empty())); recordCount++; } // then assertEquals(10, recordCount); for (int i = 0; i < actualColumns.size(); i++) { final ColumnMetadata column = actualColumns.get(i); assertEquals(columnNames[i], column.getId()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_PARSE_JSON, e); } }
private void configurePropertyNamingStrategyClass( Jackson2ObjectMapperBuilder builder, Class<?> propertyNamingStrategyClass) { builder.propertyNamingStrategy( (PropertyNamingStrategy) BeanUtils.instantiateClass(propertyNamingStrategyClass)); }
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); converters.add(new MappingJackson2HttpMessageConverter(builder.build())); }
@Bean public Jackson2ObjectMapperBuilder objectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.serializationInclusion(JsonInclude.Include.NON_NULL); return builder; }
@Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder(); b.indentOutput(true).dateFormat(new SimpleDateFormat("yyyy-MM-dd")); return b; }
@Bean public ElasticsearchTemplate elasticsearchTemplate( Client client, Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder) { return new ElasticsearchTemplate( client, new CustomEntityMapper(jackson2ObjectMapperBuilder.createXmlMapper(false).build())); }
private void configureModules(Jackson2ObjectMapperBuilder builder) { Collection<Module> moduleBeans = getBeans(this.applicationContext, Module.class); builder.modulesToInstall(moduleBeans.toArray(new Module[moduleBeans.size()])); }
@Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { return builder.createXmlMapper(false).build(); }
@Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.indentOutput(true); return builder; }