@Override public void afterPropertiesSet() { if (this.objectMapper != null) { this.builder.configure(this.objectMapper); } else { this.objectMapper = this.builder.build(); } }
/** * Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration * provided by {@link Jackson2ObjectMapperBuilder}. */ public MappingJackson2HttpMessageConverter() { this(Jackson2ObjectMapperBuilder.json().build()); }
/** * Construct a new {@code MappingJackson2CborHttpMessageConverter} using default configuration * provided by {@code Jackson2ObjectMapperBuilder}. */ public MappingJackson2CborHttpMessageConverter() { this(Jackson2ObjectMapperBuilder.cbor().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 // SPR-14435 public void smile() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.smile().build(); assertNotNull(objectMapper); assertEquals(SmileFactory.class, objectMapper.getFactory().getClass()); }
@Test // SPR-14435 public void cbor() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.cbor().build(); assertNotNull(objectMapper); assertEquals(CBORFactory.class, objectMapper.getFactory().getClass()); }
@Test(expected = IllegalArgumentException.class) public void wrongTimeZoneStringSetter() { String zoneId = "foo"; Jackson2ObjectMapperBuilder.json().timeZone(zoneId).build(); }
@Test public void xmlMapper() { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().build(); assertNotNull(objectMapper); assertEquals(XmlMapper.class, objectMapper.getClass()); }
@Test // SPR-13975 public void defaultUseWrapper() throws JsonProcessingException { ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.xml().defaultUseWrapper(false).build(); assertNotNull(objectMapper); assertEquals(XmlMapper.class, objectMapper.getClass()); ListContainer<String> container = new ListContainer<>(Arrays.asList("foo", "bar")); String output = objectMapper.writeValueAsString(container); assertThat(output, containsString("<list>foo</list><list>bar</list></ListContainer>")); }
@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 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 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()); }
@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 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 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()); }