/** * Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration * provided by {@link Jackson2ObjectMapperBuilder}. */ public MappingJackson2HttpMessageConverter() { this(Jackson2ObjectMapperBuilder.json().build()); }
/** * Construct a new {@code MappingJackson2JsonView} using default configuration * provided by {@link Jackson2ObjectMapperBuilder} and setting the content type * to {@code application/json}. */ public MappingJackson2JsonView() { super(Jackson2ObjectMapperBuilder.json().build(), DEFAULT_CONTENT_TYPE); }
@Test // SPR-12634 public void customizeWellKnownModulesWithModule() throws JsonProcessingException, UnsupportedEncodingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modulesToInstall(new CustomIntegerModule()) .build(); DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC); assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8")); assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid")); }
@Test(expected = IllegalArgumentException.class) public void wrongTimeZoneStringSetter() { String zoneId = "foo"; Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build(); }
@Test(expected = FatalBeanException.class) public void unknownFeature() { Jackson2ObjectMapperBuilder.json().featuresToEnable(Boolean.TRUE).build(); }
@Test public void visibility() throws JsonProcessingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .visibility(PropertyAccessor.GETTER, Visibility.NONE) .visibility(PropertyAccessor.FIELD, Visibility.ANY) .build(); String json = objectMapper.writeValueAsString(new JacksonVisibilityBean()); assertThat(json, containsString("property1")); assertThat(json, containsString("property2")); assertThat(json, not(containsString("property3"))); }
@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 @SuppressWarnings("unchecked") public void modulesToInstallByClass() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modulesToInstall(CustomIntegerModule.class) .build(); Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next(); assertSame(CustomIntegerSerializer.class, serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass()); }
@Test public void modulesToInstallByInstance() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json() .modulesToInstall(new CustomIntegerModule()) .build(); Serializers serializers = getSerializerFactoryConfig(objectMapper).serializers().iterator().next(); assertSame(CustomIntegerSerializer.class, serializers.findSerializer(null, SimpleType.construct(Integer.class), null).getClass()); }
@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 setNotEmptySerializationInclusion() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build(); assertSame(JsonInclude.Include.ALWAYS, objectMapper.getSerializationConfig().getSerializationInclusion()); objectMapper = Jackson2ObjectMapperBuilder.json().serializationInclusion(JsonInclude.Include.NON_EMPTY).build(); assertSame(JsonInclude.Include.NON_EMPTY, objectMapper.getSerializationConfig().getSerializationInclusion()); }
@Test public void createXmlMapper() { Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.json().indentOutput(true); ObjectMapper jsonObjectMapper = builder.build(); ObjectMapper xmlObjectMapper = builder.createXmlMapper(true).build(); assertTrue(jsonObjectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)); assertTrue(xmlObjectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)); assertTrue(xmlObjectMapper.getClass().isAssignableFrom(XmlMapper.class)); }
@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)); }
@Before public void setup() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("capitalizer", new RootBeanDefinition(Capitalizer.class)); instantiator = new SpringHandlerInstantiator(bf); objectMapper = Jackson2ObjectMapperBuilder.json().handlerInstantiator(instantiator).build(); }
@Test public void simpleDateFormatStringSetter() { SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().simpleDateFormat(DATE_FORMAT).build(); assertEquals(dateFormat, objectMapper.getSerializationConfig().getDateFormat()); assertEquals(dateFormat, objectMapper.getDeserializationConfig().getDateFormat()); }
@Test public void dateTimeFormatSetter() { SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().dateFormat(dateFormat).build(); assertEquals(dateFormat, objectMapper.getSerializationConfig().getDateFormat()); assertEquals(dateFormat, objectMapper.getDeserializationConfig().getDateFormat()); }
@Test public void timeZoneStringSetter() { String zoneId = "Europe/Paris"; ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build(); TimeZone timeZone = TimeZone.getTimeZone(zoneId); assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone()); assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone()); }
@Test public void timeZoneSetter() { TimeZone timeZone = TimeZone.getTimeZone("Europe/Paris"); ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().timeZone(timeZone).build(); assertEquals(timeZone, objectMapper.getSerializationConfig().getTimeZone()); assertEquals(timeZone, objectMapper.getDeserializationConfig().getTimeZone()); }
@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)); }
@Test public void localeSetter() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().locale(Locale.FRENCH).build(); assertEquals(Locale.FRENCH, objectMapper.getSerializationConfig().getLocale()); assertEquals(Locale.FRENCH, objectMapper.getDeserializationConfig().getLocale()); }