/** * Set a complete list of modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #setModulesToInstall}, not both. * @since 4.0 * @see com.fasterxml.jackson.databind.Module */ public void setModules(List<Module> modules) { this.builder.modules(modules); }
/** * Specify one or more modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #modulesToInstall}, not both. * @since 4.1.5 * @see #modules(List) * @see com.fasterxml.jackson.databind.Module */ public Jackson2ObjectMapperBuilder modules(Module... modules) { return modules(Arrays.asList(modules)); }
/** * Set a complete list of modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #setModulesToInstall}, not both. * @since 4.0 * @see com.fasterxml.jackson.databind.Module */ public void setModules(List<Module> modules) { this.builder.modules(modules); }
/** * Specify one or more modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #modulesToInstall}, not both. * @since 4.1.5 * @see #modules(List) * @see com.fasterxml.jackson.databind.Module */ public Jackson2ObjectMapperBuilder modules(Module... modules) { return modules(Arrays.asList(modules)); }
@Test public void modules() { NumberSerializer serializer1 = new NumberSerializer(Integer.class); SimpleModule module = new SimpleModule(); module.addSerializer(Integer.class, serializer1); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().modules(module).build(); Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next(); assertSame(serializer1, serializers.findSerializer(null, SimpleType.construct(Integer.class), null)); }
@Test public void mixIns() { Class<?> target = String.class; Class<?> mixInSource = Object.class; Map<Class<?>, Class<?>> mixIns = new HashMap<>(); mixIns.put(target, mixInSource); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modules().mixIns(mixIns) .build(); assertEquals(1, objectMapper.mixInCount()); assertSame(mixInSource, objectMapper.findMixInClassFor(target)); }
@Test public void serializerByType() { JsonSerializer<Number> serializer = new NumberSerializer(Integer.class); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modules(new ArrayList<>()) // Disable well-known modules detection .serializerByType(Boolean.class, serializer) .build(); assertTrue(getSerializerFactoryConfig(objectMapper).hasSerializers()); Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next(); assertSame(serializer, serializers.findSerializer(null, SimpleType.construct(Boolean.class), null)); }
@Test public void deserializerByType() throws JsonMappingException { JsonDeserializer<Date> deserializer = new DateDeserializers.DateDeserializer(); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modules(new ArrayList<>()) // Disable well-known modules detection .deserializerByType(Date.class, deserializer) .build(); assertTrue(getDeserializerFactoryConfig(objectMapper).hasDeserializers()); Deserializers deserializers = getDeserializerFactoryConfig(objectMapper).deserializers().iterator().next(); assertSame(deserializer, deserializers.findBeanDeserializer(SimpleType.construct(Date.class), null, null)); }
@Test public void mixIn() { Class<?> target = String.class; Class<?> mixInSource = Object.class; ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modules().mixIn(target, mixInSource) .build(); assertEquals(1, objectMapper.mixInCount()); assertSame(mixInSource, objectMapper.findMixInClassFor(target)); }
.modules(new ArrayList<>()) // Disable well-known modules detection .serializers(serializer1) .serializersByType(Collections.singletonMap(Boolean.class, serializer2))
/** * Set a complete list of modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #setModulesToInstall}, not both. * @since 4.0 * @see com.fasterxml.jackson.databind.Module */ public void setModules(List<Module> modules) { this.builder.modules(modules); }
/** * Set a complete list of modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #setFindModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #setModulesToInstall}, not both. * @since 4.0 * @see com.fasterxml.jackson.databind.Module */ public void setModules(List<Module> modules) { this.builder.modules(modules); }
/** * Specify one or more modules to be registered with the {@link ObjectMapper}. * <p>Note: If this is set, no finding of modules is going to happen - not by * Jackson, and not by Spring either (see {@link #findModulesViaServiceLoader}). * As a consequence, specifying an empty list here will suppress any kind of * module detection. * <p>Specify either this or {@link #modulesToInstall}, not both. * @since 4.1.5 * @see #modules(List) * @see com.fasterxml.jackson.databind.Module */ public Jackson2ObjectMapperBuilder modules(Module... modules) { return modules(Arrays.asList(modules)); }
@Bean public Jackson2ObjectMapperBuilderCustomizer dataflowObjectMapperBuilderCustomizer() { return (builder) -> { builder.dateFormat(new ISO8601DateFormatWithMilliSeconds(TimeZone.getDefault(), Locale.getDefault(), true)); // apply SCDF Batch Mixins to // ignore the JobExecution in StepExecution to prevent infinite loop. // https://github.com/spring-projects/spring-hateoas/issues/333 builder.mixIn(StepExecution.class, StepExecutionJacksonMixIn.class); builder.mixIn(ExecutionContext.class, ExecutionContextJacksonMixIn.class); builder.modules(new JavaTimeModule()); }; }
@Bean public ObjectMapper objectMapper() { return Jackson2ObjectMapperBuilder.json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).modules(new JavaTimeModule()).build(); }
@Bean public ObjectMapper objectMapper() { return Jackson2ObjectMapperBuilder.json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).modules(new JavaTimeModule()).build(); } }
@Bean public ObjectMapper objectMapper() { return Jackson2ObjectMapperBuilder.json() .serializationInclusion(JsonInclude.Include.NON_NULL) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).modules(new JavaTimeModule()).build(); }
return modules(Arrays.asList(modules));
@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; }
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; }