/** * Specify features to enable. * @see com.fasterxml.jackson.core.JsonParser.Feature * @see com.fasterxml.jackson.core.JsonGenerator.Feature * @see com.fasterxml.jackson.databind.SerializationFeature * @see com.fasterxml.jackson.databind.DeserializationFeature * @see com.fasterxml.jackson.databind.MapperFeature */ public void setFeaturesToEnable(Object... featuresToEnable) { this.builder.featuresToEnable(featuresToEnable); }
/** * Specify features to enable. * @see com.fasterxml.jackson.core.JsonParser.Feature * @see com.fasterxml.jackson.core.JsonGenerator.Feature * @see com.fasterxml.jackson.databind.SerializationFeature * @see com.fasterxml.jackson.databind.DeserializationFeature * @see com.fasterxml.jackson.databind.MapperFeature */ public void setFeaturesToEnable(Object... featuresToEnable) { this.builder.featuresToEnable(featuresToEnable); }
@Test(expected = FatalBeanException.class) public void unknownFeature() { Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build(); }
.deserializersByType(deserializerMap) .annotationIntrospector(annotationIntrospector) .featuresToEnable(SerializationFeature.FAIL_ON_EMPTY_BEANS, DeserializationFeature.UNWRAP_ROOT_VALUE, JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER,
@Test public void booleanSetters() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .featuresToEnable(MapperFeature.DEFAULT_VIEW_INCLUSION, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, SerializationFeature.INDENT_OUTPUT) .featuresToDisable(MapperFeature.AUTO_DETECT_FIELDS, MapperFeature.AUTO_DETECT_GETTERS, MapperFeature.AUTO_DETECT_SETTERS, SerializationFeature.FAIL_ON_EMPTY_BEANS).build(); assertNotNull(objectMapper); assertTrue(objectMapper.isEnabled(MapperFeature.DEFAULT_VIEW_INCLUSION)); assertTrue(objectMapper.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_FIELDS)); assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_GETTERS)); assertFalse(objectMapper.isEnabled(MapperFeature.AUTO_DETECT_SETTERS)); assertTrue(objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)); assertFalse(objectMapper.isEnabled(SerializationFeature.FAIL_ON_EMPTY_BEANS)); }
/** * Specify features to enable. * @see com.fasterxml.jackson.core.JsonParser.Feature * @see com.fasterxml.jackson.core.JsonGenerator.Feature * @see com.fasterxml.jackson.databind.SerializationFeature * @see com.fasterxml.jackson.databind.DeserializationFeature * @see com.fasterxml.jackson.databind.MapperFeature */ public void setFeaturesToEnable(Object... featuresToEnable) { this.builder.featuresToEnable(featuresToEnable); }
/** * Specify features to enable. * @see com.fasterxml.jackson.core.JsonParser.Feature * @see com.fasterxml.jackson.core.JsonGenerator.Feature * @see com.fasterxml.jackson.databind.SerializationFeature * @see com.fasterxml.jackson.databind.DeserializationFeature * @see com.fasterxml.jackson.databind.MapperFeature */ public void setFeaturesToEnable(Object... featuresToEnable) { this.builder.featuresToEnable(featuresToEnable); }
@Bean public Jackson2ObjectMapperBuilder objectMapperBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.featuresToEnable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); return builder; }
private List<HttpMessageConverter<?>> getMessageConverters() { List<HttpMessageConverter<?>> xs = new ArrayList<HttpMessageConverter<?>>(); final Jackson2ObjectMapperBuilder bldr = Jackson2ObjectMapperBuilder .json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); xs.add(new MappingJackson2HttpMessageConverter(bldr.build())); return xs; }
private List<HttpMessageConverter<?>> getMessageConverters() { List<HttpMessageConverter<?>> xs = new ArrayList<HttpMessageConverter<?>>(); final Jackson2ObjectMapperBuilder bldr = Jackson2ObjectMapperBuilder .json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT); xs.add(new MappingJackson2HttpMessageConverter(bldr.build())); return xs; }
public RestTemplateBuilder withSpecificJsonMessageConverter() { isUseDefaultJsonMessageConverter = false; AbstractHttpMessageConverter<?> jsonMessageConverter = new MappingJackson2HttpMessageConverter( Jackson2ObjectMapperBuilder .json() .featuresToEnable( DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT) .build()); jsonMessageConverter.setSupportedMediaTypes(Lists.newArrayList( MediaType.TEXT_HTML, MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON)); withMessageConverter(jsonMessageConverter); return this; }
@Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json(); builder.indentOutput(false) .dateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")) .failOnUnknownProperties(false) .featuresToEnable(ALLOW_NON_NUMERIC_NUMBERS) .featuresToEnable(ALLOW_SINGLE_QUOTES) .serializationInclusion(JsonInclude.Include.NON_NULL) .modules(new JodaModule(), new Hibernate5Module()); return builder; }
public Client(String context, String baseUrl, String username, String password, boolean usePermalinkEndpoint, boolean debug, ClientHttpRequestFactory requestFactory) { this.context = context; this.baseUrl = baseUrl; this.username = username; this.password = password; this.debug = debug; this.permalinkEndpoint = usePermalinkEndpoint; final ObjectMapper emptyArrayAsNullObjectMapper = Jackson2ObjectMapperBuilder.json() .featuresToEnable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT) .featuresToEnable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY).build(); List<HttpMessageConverter<?>> messageConverters = new ArrayList<>(); messageConverters.add(new ByteArrayHttpMessageConverter()); messageConverters.add(new StringHttpMessageConverter()); messageConverters.add(new ResourceHttpMessageConverter()); messageConverters.add(new SourceHttpMessageConverter<>()); messageConverters.add(new AllEncompassingFormHttpMessageConverter()); messageConverters.add(new MappingJackson2HttpMessageConverter(emptyArrayAsNullObjectMapper)); //messageConverters.add(new MappingJackson2HttpMessageConverter()); restTemplate = new RestTemplate(messageConverters); if (requestFactory != null) { restTemplate.setRequestFactory(requestFactory); } }
private static Jackson2ObjectMapperBuilder getDefaultBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() .indentOutput(false) .createXmlMapper(false) .dateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")) .failOnUnknownProperties(false) .featuresToEnable(ALLOW_NON_NUMERIC_NUMBERS) .featuresToEnable(ALLOW_SINGLE_QUOTES) .serializationInclusion(JsonInclude.Include.NON_NULL) .modules(new JodaModule()); return builder; }
@Bean public Jackson2ObjectMapperBuilder objectMapperBuilder() { return new Jackson2ObjectMapperBuilder() .serializationInclusion(JsonInclude.Include.NON_EMPTY) .featuresToDisable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES ) .featuresToEnable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .indentOutput(true); } }
@Bean public Jackson2ObjectMapperBuilder objectMapperBuilder(JsonComponentModule jsonComponentModule) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder // .serializerByType(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() { // @Override // public void serialize(ZonedDateTime zonedDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { // jsonGenerator.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime)); // } // }) .serializationInclusion(JsonInclude.Include.NON_EMPTY) .featuresToDisable( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES ) .featuresToEnable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY) .indentOutput(true) .modulesToInstall(jsonComponentModule); return builder; }
@Bean public ObjectMapper objectMapper(){ Jackson2ObjectMapperBuilder j2omb = new Jackson2ObjectMapperBuilder(); return j2omb.autoDetectFields(true) .autoDetectGettersSetters(true) .createXmlMapper(false) .failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToEnable( JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, // 无引号json 非json规约 默认false JsonParser.Feature.ALLOW_SINGLE_QUOTES, // 单引号json JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN, // bigdecimal.toPlainString SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS, // ['a','b','c'] not "abc" DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, // unknown enum -> null DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, // [] -> null DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT // "" -> null ) .build(); }