@SuppressWarnings("unused") public static ObjectMapper buildObjectMapperForDeserialization() { final ObjectMapper mapper = new ObjectMapper(); InventoryJacksonConfig.configure(mapper); mapper.addMixIn(CanonicalPath.class, CanonicalPathMixin.class); return mapper; }
@Override protected ObjectMapper buildObjectMapperForSerialization() { final ObjectMapper mapper = new ObjectMapper(); InventoryJacksonConfig.configure(mapper); mapper.addMixIn(CanonicalPath.class, CanonicalPathMixin.class); return mapper; }
@Test public void testSerializationWithTypeInfo02() throws Exception { ObjectMapper mapper = newMapper(); // need new to add mix-ins: mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); Duration duration = Duration.ofSeconds(13498L, 837481723); String value = mapper.writeValueAsString(duration); assertNotNull("The value should not be null.", value); assertEquals( "The value is not correct.", "[\"" + Duration.class.getName() + "\",13498837]", value); }
// [Issue#28]: ObjectMapper.copy() public void testCopy() throws Exception { ObjectMapper m = new ObjectMapper(); assertTrue(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); m.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); InjectableValues inj = new InjectableValues.Std(); m.setInjectableValues(inj); assertFalse(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS)); m.enable(JsonParser.Feature.ALLOW_COMMENTS); assertTrue(m.isEnabled(JsonParser.Feature.ALLOW_COMMENTS)); // // First: verify that handling of features is decoupled: ObjectMapper m2 = m.copy(); assertFalse(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); m2.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); assertTrue(m2.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); assertSame(inj, m2.getInjectableValues()); // but should NOT change the original assertFalse(m.isEnabled(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)); // nor vice versa: assertFalse(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); m.enable(DeserializationFeature.UNWRAP_ROOT_VALUE); assertTrue(m.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); assertFalse(m2.isEnabled(DeserializationFeature.UNWRAP_ROOT_VALUE)); // // Also, underlying JsonFactory instances should be distinct assertNotSame(m.getFactory(), m2.getFactory()); // [Issue#122]: Need to ensure mix-ins are not shared assertEquals(0, m.getSerializationConfig().mixInCount()); assertEquals(0, m2.getSerializationConfig().mixInCount()); assertEquals(0, m.getDeserializationConfig().mixInCount()); assertEquals(0, m2.getDeserializationConfig().mixInCount()); m.addMixIn(String.class, Integer.class); assertEquals(1, m.getSerializationConfig().mixInCount()); assertEquals(0, m2.getSerializationConfig().mixInCount()); assertEquals(1, m.getDeserializationConfig().mixInCount()); assertEquals(0, m2.getDeserializationConfig().mixInCount()); // [Issue#913]: Ensure JsonFactory Features copied assertTrue(m2.isEnabled(JsonParser.Feature.ALLOW_COMMENTS)); }
public void testDateMidnightDeserWithTypeInfo() throws IOException { ObjectMapper mapper = jodaMapper(); mapper.addMixIn(DateMidnight.class, MixInForTypeId.class); // couple of acceptable formats, so: DateMidnight date = mapper.readValue("[\"org.joda.time.DateMidnight\",[2001,5,25]]", DateMidnight.class); assertEquals(2001, date.getYear()); assertEquals(5, date.getMonthOfYear()); assertEquals(25, date.getDayOfMonth()); DateMidnight date2 = mapper.readValue("[\"org.joda.time.DateMidnight\",\"2005-07-13\"]", DateMidnight.class); assertEquals(2005, date2.getYear()); assertEquals(7, date2.getMonthOfYear()); assertEquals(13, date2.getDayOfMonth()); }
public EurekaJsonJacksonCodec(final KeyFormatter keyFormatter, boolean compact) { // JSON SimpleModule jsonModule = new SimpleModule(); jsonModule.setSerializerModifier( EurekaJacksonJsonModifiers.createJsonSerializerModifier(keyFormatter, compact)); jsonModule.setDeserializerModifier( EurekaJacksonJsonModifiers.createJsonDeserializerModifier(keyFormatter, compact)); jsonMapper.registerModule(jsonModule); jsonMapper.setSerializationInclusion(Include.NON_NULL); jsonMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); jsonMapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, false); jsonMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); jsonMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); if (compact) { addMiniConfig(jsonMapper); } else { jsonMapper.addMixIn(InstanceInfo.class, InstanceInfoJsonMixIn.class); } }
protected void writeJson(ResponseWrapper response, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); ObjectMapper mapper = new ObjectMapper(); // Add support for jackson mixins JsonMixin[] jsonMixins = response.getJsonResponse().mixins(); for (JsonMixin jsonMixin : jsonMixins) { mapper.addMixIn(jsonMixin.target(), jsonMixin.mixin()); } JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(outputMessage.getBody(), encoding); try { mapper.writeValue(jsonGenerator, response.getOriginalResponse()); } catch (IOException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
@Before public void setUp() { mapper = new ObjectMapper(); mapper.addMixIn(CronTrigger.class, CronTriggerMixin.class); }
public void testEnumsWithJsonValueUsingMixin() throws Exception { // can't share, as new mix-ins are added ObjectMapper m = new ObjectMapper(); m.addMixIn(TestEnum.class, ToStringMixin.class); assertEquals("\"b\"", m.writeValueAsString(TestEnum.B)); }