Search in sources :

Example 1 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException in project jackson-dataformats-binary by FasterXML.

the class WriteErrorsTest method testUnknownProperties.

public void testUnknownProperties() throws Exception {
    ProtobufSchema schema = ProtobufSchemaLoader.std.parse(PROTOC_BOX, "Point");
    final ObjectWriter w = MAPPER.writerFor(Point3D.class).with(schema);
    // First: if disabled, should get an error
    try {
        /*byte[] bytes =*/
        w.without(StreamWriteFeature.IGNORE_UNKNOWN).writeValueAsBytes(new Point3D(1, 2, 3));
    } catch (DatabindException e) {
        verifyException(e, "Unrecognized field 'z'");
    }
    // but then not, if we disable checks
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    JsonGenerator g = MAPPER.writer().with(StreamWriteFeature.IGNORE_UNKNOWN).with(schema).createGenerator(bytes);
    g.writeStartObject();
    g.writeName("x");
    g.writeNumber((short) 290);
    // unknown field
    g.writeName("foo");
    g.writeNumber(1.0f);
    // also, should be fine to let generator close the object...
    g.close();
    byte[] b = bytes.toByteArray();
    assertEquals(3, b.length);
    Point3D result = MAPPER.readerFor(Point3D.class).with(schema).readValue(b);
    assertEquals(290, result.x);
}
Also used : ProtobufSchema(com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabindException(com.fasterxml.jackson.databind.DatabindException)

Example 2 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException in project jackson-dataformats-binary by FasterXML.

the class SimpleGenerationTest method testIgnoringOfUnknownScalar.

@SuppressWarnings("resource")
public void testIgnoringOfUnknownScalar() throws Exception {
    ObjectMapper mapper = newMapper();
    // we can repurpose "Binary" from above for schema
    BinaryAndNumber input = new BinaryAndNumber("Bob", 15);
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    try {
        mapper.writer(SCHEMA_WITH_BINARY_JSON).writeValue(b, input);
        fail("Should have thrown exception");
    } catch (DatabindException e) {
        verifyException(e, "no field named");
    }
    // But should be fine if (and only if!) we enable support for skipping
    b = new ByteArrayOutputStream();
    mapper.writer(SCHEMA_WITH_BINARY_JSON).with(StreamWriteFeature.IGNORE_UNKNOWN).writeValue(b, input);
    byte[] bytes = b.toByteArray();
    assertEquals(6, bytes.length);
    // and should be able to get it back too
    BinaryAndNumber output = mapper.reader(SCHEMA_WITH_BINARY_JSON).forType(BinaryAndNumber.class).readValue(bytes);
    assertEquals("Bob", output.name);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DatabindException(com.fasterxml.jackson.databind.DatabindException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException in project jackson-dataformats-binary by FasterXML.

the class RecordWithComplexTest method testRecordWithNullRequiredFields.

@Test
public void testRecordWithNullRequiredFields() {
    RecursiveDummyRecord original = new RecursiveDummyRecord(null, 12353, new DummyRecord("World", 234));
    // 
    try {
        roundTrip(RecursiveDummyRecord.class, original);
        fail("Should throw an exception");
    } catch (DatabindException e) {
    // 28-Feb-2017, tatu: Sometimes we get this (probably when using ObjectWriter)
    } catch (NullPointerException e) {
    // 28-Feb-2017, tatu: Should NOT just pass NPE, but for now nothing much we can do
    // !!! 22-Feb-2021, tatu: Really should figure out how to improve...
    }
}
Also used : DummyRecord(com.fasterxml.jackson.dataformat.avro.interop.DummyRecord) DatabindException(com.fasterxml.jackson.databind.DatabindException) Test(org.junit.Test)

Example 4 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException in project jackson-dataformats-binary by FasterXML.

the class SimpleGenerationTest method testIgnoringOfUnknownObject.

public void testIgnoringOfUnknownObject() throws Exception {
    AvroFactory af = new AvroFactory();
    ObjectMapper mapper = new ObjectMapper(af);
    BinaryAndArray input = new BinaryAndArray("Bob");
    try {
        mapper.writer(SCHEMA_WITH_BINARY_JSON).writeValueAsBytes(input);
        fail("Should have thrown exception");
    } catch (DatabindException e) {
        verifyException(e, "no field named 'stuff'");
    }
    // But should be fine if (and only if!) we enable support for skipping
    af = af.rebuild().enable(StreamWriteFeature.IGNORE_UNKNOWN).build();
    mapper = new ObjectMapper(af);
    byte[] bytes = mapper.writer(SCHEMA_WITH_BINARY_JSON).writeValueAsBytes(input);
    assertEquals(6, bytes.length);
    // and should be able to get it back too
    BinaryAndNumber output = mapper.reader(SCHEMA_WITH_BINARY_JSON).forType(BinaryAndNumber.class).readValue(bytes);
    assertEquals("Bob", output.name);
}
Also used : DatabindException(com.fasterxml.jackson.databind.DatabindException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with DatabindException

use of com.fasterxml.jackson.databind.DatabindException 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)

Aggregations

DatabindException (com.fasterxml.jackson.databind.DatabindException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Test (org.junit.Test)2 FormatSchema (com.fasterxml.jackson.core.FormatSchema)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 DummyRecord (com.fasterxml.jackson.dataformat.avro.interop.DummyRecord)1 IonObjectMapper (com.fasterxml.jackson.dataformat.ion.IonObjectMapper)1 ProtobufSchema (com.fasterxml.jackson.dataformat.protobuf.schema.ProtobufSchema)1 Schema (org.apache.avro.Schema)1 SchemaPairCompatibility (org.apache.avro.SchemaCompatibility.SchemaPairCompatibility)1