use of com.fasterxml.jackson.dataformat.ion.IonObjectMapper in project jackson-dataformats-binary by FasterXML.
the class PolymorphicRoundtripTest method testSelectivePolymorphism.
@Test
public void testSelectivePolymorphism() throws IOException {
// preferredTypeId is a crude testing mechanism of choosing among several serialized type ids.
// setting non-null so that multiple type ids get serialized
preferredTypeId = "no match";
Bean original = new Bean("parent_field", new ChildBeanSub("child_field", "extended_field"));
IonObjectMapper mapper = IonObjectMapper.builder().addModule(new IonAnnotationModule()).build();
String serialized = mapper.writeValueAsString(original);
// first, try deserializing with no preferred type id (no matching one, anyway). We expect the first type id
// to be chosen (and we expect that first id to be the most narrow type, ChildBeanSub).
Bean deserialized = mapper.readValue(serialized, Bean.class);
assertTrue(deserialized.child.getClass().equals(ChildBeanSub.class));
assertEquals(((ChildBeanSub) original.child).extraField, ((ChildBeanSub) deserialized.child).extraField);
// second, try deserializing with the wider type (ChildBean). We're losing data (extraField)
preferredTypeId = getClass().getCanonicalName() + "$ChildBean";
deserialized = mapper.readValue(serialized, Bean.class);
assertTrue(deserialized.child.getClass().equals(ChildBean.class));
assertEquals(original.child.someField, deserialized.child.someField);
// third, try deserializing into an Object. The child node should deserialize, but immediately fail mapping.
preferredTypeId = "java.lang.Object";
try {
deserialized = mapper.readValue(serialized, Bean.class);
Assert.fail("Expected jackson to complain about casting a (concrete) Object into a ChildBean.");
} catch (DatabindException e) {
}
}
use of com.fasterxml.jackson.dataformat.ion.IonObjectMapper in project jackson-dataformats-binary by FasterXML.
the class IonValueDeserializerTest method testWithMissingProperty.
@Test
public void testWithMissingProperty() throws IOException {
IonSystem ionSystem = IonSystemBuilder.standard().build();
IonObjectMapper ionObjectMapper = IonObjectMapper.builder(ionSystem).addModule(new IonValueModule()).build();
String input1 = "{required:{}, optional:{}}";
MyBean deserializedBean1 = ionObjectMapper.readValue(input1, MyBean.class);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean1.required);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean1.optional);
// This deserialization should not fail with missing property
String input2 = "{required:{}}";
MyBean deserializedBean2 = ionObjectMapper.readValue(input2, MyBean.class);
assertEquals(ionSystem.newEmptyStruct(), deserializedBean2.required);
assertEquals(null, deserializedBean2.optional);
}
Aggregations