Skip to content

Commit

Permalink
Try to reduce usage of JsonMappingException (to prepare for #2828)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jan 21, 2021
1 parent 0793e87 commit 3d845a3
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public StringValue deserialize(JsonParser jp, DeserializationContext ctxt) throw
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
throws JsonMappingException
{
String name = (property == null) ? "NULL" : property.getName();
return new MyContextualDeserializer(name);
Expand Down Expand Up @@ -137,7 +136,6 @@ public StringValue deserialize(JsonParser jp, DeserializationContext ctxt) throw
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
throws JsonMappingException
{
Name ann = property.getAnnotation(Name.class);
if (ann == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void serialize(String value, JsonGenerator jgen, SerializerProvider provi

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException
{
return new ContextualKeySerializer(_prefix+":");
}
Expand All @@ -70,7 +69,7 @@ public Object deserializeKey(String key, DeserializationContext ctxt)

@Override
public KeyDeserializer createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException
BeanProperty property)
{
return new ContextualDeser((property == null) ? "ROOT" : property.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public void serialize(String value, JsonGenerator jgen, SerializerProvider provi

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException
{
String prefix = "UNKNOWN";
Prefix ann = null;
Expand Down Expand Up @@ -174,7 +173,6 @@ public void serialize(String value, JsonGenerator jgen, SerializerProvider provi

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException
{
return new ContextualAndResolvable(isResolved, isContextual+1);
}
Expand Down Expand Up @@ -205,7 +203,6 @@ public void serialize(String value, JsonGenerator g, SerializerProvider provider

@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException
{
if (property == null) {
return new AccumulatingContextual(desc+"/ROOT");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public StringValue deserialize(JsonParser p, DeserializationContext ctxt) throws
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property)
throws JsonMappingException
{
Name ann = property.getAnnotation(Name.class);
if (ann == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

import com.fasterxml.jackson.databind.BaseMapTest;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.cfg.CoercionInputShape;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
Expand Down Expand Up @@ -99,7 +94,7 @@ public void testEmptyStringFailForBooleanPrimitive() throws IOException
try {
reader.readValue(aposToQuotes("{'booleanValue':''}"));
fail("Expected failure for boolean + empty String");
} catch (JsonMappingException e) {
} catch (MismatchedInputException e) {
verifyException(e, "Cannot coerce `null` to `boolean`");
verifyException(e, "FAIL_ON_NULL_FOR_PRIMITIVES");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Issue476Deserializer(BeanDeserializer src) {

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException {
BeanProperty property) {
propCount++;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
*/
public class TestConcurrency extends BaseMapTest
{
/*
/**********************************************
/* Helper beans
/**********************************************
*/

@JsonDeserialize(using=CustomBeanDeserializer.class)
static class Bean
{
Expand Down Expand Up @@ -51,7 +45,7 @@ public Bean deserialize(JsonParser jp, DeserializationContext ctxt) throws IOExc
}

@Override
public void resolve(DeserializationContext ctxt) throws JsonMappingException
public void resolve(DeserializationContext ctxt)
{
try {
Thread.sleep(100L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Bean375Outer deserialize(JsonParser p, DeserializationContext ctxt)
}
@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
{
return new Bean375OuterDeserializer(property);
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ public Bean375Inner deserialize(JsonParser p, DeserializationContext ctxt)

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property)
throws JsonMappingException {
{
if (property != null) {
Negative n = property.getAnnotation(Negative.class);
if (n != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.Deserializers;
import com.fasterxml.jackson.databind.deser.std.EnumDeserializer;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.module.SimpleModule;

Expand Down Expand Up @@ -103,7 +104,8 @@ protected enum TestEnum324
static class DelegatingDeserializers extends Deserializers.Base
{
@Override
public JsonDeserializer<?> findEnumDeserializer(final Class<?> type, final DeserializationConfig config, final BeanDescription beanDesc) throws JsonMappingException {
public JsonDeserializer<?> findEnumDeserializer(final Class<?> type, final DeserializationConfig config, final BeanDescription beanDesc)
{
final Collection<AnnotatedMethod> factoryMethods = beanDesc.getFactoryMethods();
if (factoryMethods != null) {
for (AnnotatedMethod am : factoryMethods) {
Expand Down Expand Up @@ -255,7 +257,7 @@ public void testExceptionFromCreator() throws Exception
try {
/*TestEnum324 e =*/ MAPPER.readValue(quote("xyz"), TestEnum324.class);
fail("Should throw exception");
} catch (JsonMappingException e) {
} catch (ValueInstantiationException e) {
verifyException(e, "foobar");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;

public class NullValueViaCreatorTest extends BaseMapTest
{
Expand Down Expand Up @@ -41,7 +42,6 @@ protected static class ContainerDeserializerResolver extends Deserializers.Base
@Override
public JsonDeserializer<?> findBeanDeserializer(JavaType type,
DeserializationConfig config, BeanDescription beanDesc)
throws JsonMappingException
{
if (!Contained.class.isAssignableFrom(type.getRawClass())) {
return null;
Expand Down Expand Up @@ -108,7 +108,7 @@ public void testCreatorReturningNull() throws IOException {
try {
objectMapper.readValue(json, JsonEntity.class);
fail("Should not have succeeded");
} catch (JsonMappingException e) {
} catch (ValueInstantiationException e) {
verifyException(e, "JSON creator returned null");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
* and {@link PropertyValueBuffer#getParameter(SettableBeanProperty)} methods.
*/
@SuppressWarnings("serial")
public class TestCustomValueInstDefaults extends BaseTest
public class TestCustomValueInstDefaults
extends BaseMapTest
{
static class Bucket
{
Expand Down Expand Up @@ -119,10 +120,9 @@ static class BucketInstantiator extends StdValueInstantiator
}

@Override
public Object createFromObjectWith(
DeserializationContext ctxt,
SettableBeanProperty[] props,
PropertyValueBuffer buffer) throws JsonMappingException
public Object createFromObjectWith(DeserializationContext ctxt,
SettableBeanProperty[] props, PropertyValueBuffer buffer)
throws IOException
{
int a = Bucket.DEFAULT_A;
int b = Bucket.DEFAULT_B;
Expand Down Expand Up @@ -153,10 +153,9 @@ static class BigBucketInstantiator extends StdValueInstantiator
}

@Override
public Object createFromObjectWith(
DeserializationContext ctxt,
SettableBeanProperty[] props,
PropertyValueBuffer buffer) throws JsonMappingException
public Object createFromObjectWith(DeserializationContext ctxt,
SettableBeanProperty[] props, PropertyValueBuffer buffer)
throws IOException
{
int i01 = BigBucket.DEFAULT_I;
int i02 = BigBucket.DEFAULT_I;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ public void testIgnorals() throws Exception
}

// [databind#1368]
public void testJsonMappingExceptionSerialization() throws IOException {
public void testDatabindExceptionSerialization() throws IOException {
Exception e = null;
// cant deserialize due to unexpected constructor
try {
MAPPER.readValue( "{ \"val\": \"foo\" }", NoSerdeConstructor.class );
fail("Should not pass");
} catch (JsonMappingException e0) {
} catch (MismatchedInputException e0) {
verifyException(e0, "cannot deserialize from Object");
e = e0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

import java.io.IOException;

public class TestExceptionHandlingWithDefaultDeserialization extends BaseMapTest
public class TestExceptionHandlingWithDefaultDeserialization
extends BaseMapTest
{
static class Foo {
private Bar bar;
Expand Down Expand Up @@ -38,7 +39,7 @@ public String getQux() {
}
}

public void testShouldThrowJsonMappingExceptionWithPathReference() throws IOException {
public void testShouldThrowExceptionWithPathReference() throws IOException {
// given
ObjectMapper mapper = new ObjectMapper();
String input = "{\"bar\":{\"baz\":{qux:\"quxValue\"))}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import java.io.IOException;

public class TestExceptionHandlingWithJsonCreatorDeserialization extends BaseMapTest
public class TestExceptionHandlingWithJsonCreatorDeserialization
extends BaseMapTest
{
static class Foo {
private Bar bar;
Expand Down Expand Up @@ -49,7 +50,7 @@ public String getQux() {
}
}

public void testShouldThrowJsonMappingExceptionWithPathReference() throws IOException {
public void testShouldThrowExceptionWithPathReference() throws IOException {
// given
ObjectMapper mapper = new ObjectMapper();
String input = "{\"bar\":{\"baz\":{qux:\"quxValue\"))}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void optionalProperty(BeanProperty prop) throws JsonMappingException {

@Override
public void optionalProperty(String name, JsonFormatVisitable handler,
JavaType propertyTypeHint) throws JsonMappingException { }
JavaType propertyTypeHint) { }

private void _visit(BeanProperty prop) throws JsonMappingException
{
Expand Down Expand Up @@ -315,7 +315,7 @@ public void optionalProperty(BeanProperty prop) throws JsonMappingException {
ser.acceptJsonFormatVisitor(new JsonFormatVisitorWrapper.Base() {
@Override
public JsonNumberFormatVisitor expectNumberFormat(
JavaType t) throws JsonMappingException {
JavaType t) {
return new JsonNumberFormatVisitor() {
@Override
public void format(JsonValueFormat format) {
Expand All @@ -333,7 +333,7 @@ public void numberType(NumberType numberType) {
}

@Override
public JsonIntegerFormatVisitor expectIntegerFormat(JavaType t) throws JsonMappingException {
public JsonIntegerFormatVisitor expectIntegerFormat(JavaType t) {
return new JsonIntegerFormatVisitor() {
@Override
public void format(JsonValueFormat format) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ public void testDeserializationMissing() throws Exception {
checkOrangeBoxNull(BOX_READER_PASS, orangeBoxMissingJson);
checkAppleBoxNull(BOX_READER_PASS, appleBoxMissingJson);

checkBoxJsonMappingException(BOX_READER_FAIL, orangeBoxMissingJson);
checkBoxJsonMappingException(BOX_READER_FAIL, appleBoxMissingJson);
checkBoxException(BOX_READER_FAIL, orangeBoxMissingJson);
checkBoxException(BOX_READER_FAIL, appleBoxMissingJson);
}

private void checkOrangeBox(ObjectReader reader) throws Exception {
Expand Down Expand Up @@ -233,7 +233,7 @@ private void checkAppleBoxNull(ObjectReader reader, String json) throws Exceptio
assertNull(deserAppleBox.getFruit());
}

private void checkBoxJsonMappingException(ObjectReader reader, String json) throws Exception {
private void checkBoxException(ObjectReader reader, String json) throws Exception {
try {
reader.readValue(json);
fail("Should not pass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public void testDeserializationMissing() throws Exception {
checkAppleBoxNull(r, appleBoxMissingJson);

r = READER.with(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
checkBoxJsonMappingException(r, orangeBoxMissingJson);
checkBoxJsonMappingException(r, appleBoxMissingJson);
checkBoxDatabindException(r, orangeBoxMissingJson);
checkBoxDatabindException(r, appleBoxMissingJson);
}

/**
Expand All @@ -177,12 +177,12 @@ public void testDeserializationMissing() throws Exception {
@Test
public void testDeserializationMissingRequired() throws Exception {
ObjectReader r = READER.without(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
checkReqBoxJsonMappingException(r, orangeBoxMissingJson);
checkReqBoxJsonMappingException(r, appleBoxMissingJson);
checkReqBoxDatabindException(r, orangeBoxMissingJson);
checkReqBoxDatabindException(r, appleBoxMissingJson);

r = READER.with(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
checkReqBoxJsonMappingException(r, orangeBoxMissingJson);
checkReqBoxJsonMappingException(r, appleBoxMissingJson);
checkReqBoxDatabindException(r, orangeBoxMissingJson);
checkReqBoxDatabindException(r, appleBoxMissingJson);
}

private void checkOrangeBox(ObjectReader r) throws Exception {
Expand Down Expand Up @@ -237,7 +237,7 @@ private void checkAppleBoxNull(ObjectReader r, String json) throws Exception {
assertNull(deserAppleBox.fruit);
}

private void checkBoxJsonMappingException(ObjectReader r, String json) throws Exception {
private void checkBoxDatabindException(ObjectReader r, String json) throws Exception {
try {
r.forType(Box.class).readValue(json);
fail("Should not pass");
Expand All @@ -246,7 +246,7 @@ private void checkBoxJsonMappingException(ObjectReader r, String json) throws Ex
}
}

private void checkReqBoxJsonMappingException(ObjectReader r, String json) throws Exception {
private void checkReqBoxDatabindException(ObjectReader r, String json) throws Exception {
try {
r.forType(ReqBox.class).readValue(json, ReqBox.class);
fail("Should not pass");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void serialize(CustomBean value, JsonGenerator jgen, SerializerProvider p
}

@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return null;
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ public void serialize(SimpleEnum value, JsonGenerator jgen, SerializerProvider p
}

@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint) throws JsonMappingException {
public JsonNode getSchema(SerializerProvider provider, Type typeHint) {
return null;
}
}
Expand Down
Loading

0 comments on commit 3d845a3

Please sign in to comment.