Search in sources :

Example 71 with IonObjectMapper

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) {
    }
}
Also used : DatabindException(com.fasterxml.jackson.databind.DatabindException) IonObjectMapper(com.fasterxml.jackson.dataformat.ion.IonObjectMapper) Test(org.junit.Test)

Example 72 with IonObjectMapper

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);
}
Also used : IonSystem(com.amazon.ion.IonSystem) IonObjectMapper(com.fasterxml.jackson.dataformat.ion.IonObjectMapper) Test(org.junit.Test)

Aggregations

IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)72 Test (org.junit.Test)70 Temporal (java.time.temporal.Temporal)24 Instant (java.time.Instant)20 OffsetDateTime (java.time.OffsetDateTime)20 ZonedDateTime (java.time.ZonedDateTime)20 Timestamp (com.amazon.ion.Timestamp)8 IonTimestamp (com.amazon.ion.IonTimestamp)4 IonValue (com.amazon.ion.IonValue)4 IonDecimal (com.amazon.ion.IonDecimal)1 IonInt (com.amazon.ion.IonInt)1 IonSystem (com.amazon.ion.IonSystem)1 DatabindException (com.fasterxml.jackson.databind.DatabindException)1 IOException (java.io.IOException)1 BigDecimal (java.math.BigDecimal)1 BeforeClass (org.junit.BeforeClass)1 Result (software.amazon.qldb.Result)1