Skip to content

Commit

Permalink
Bit more work on #2828, trying to reduce coupling to JsonMappingExcep…
Browse files Browse the repository at this point in the history
…tion even in 2.x
  • Loading branch information
cowtowncoder committed Jan 23, 2021
1 parent 516c91f commit ef34d08
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ protected DatabindException(String msg, JsonLocation loc) {
protected DatabindException(String msg, Throwable rootCause) {
this(msg, null, rootCause);
}

/**
* Method called to prepend a reference information in front of
* current path
*/
public abstract void prependPath(Object referrer, String fieldName);

/**
* Method called to prepend a reference information in front of
* current path
*/
public abstract void prependPath(Object referrer, int index);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class JsonMappingException
final static int MAX_REFS_TO_LIST = 1000;

/*
/**********************************************************
/**********************************************************************
/* Helper classes
/**********************************************************
/**********************************************************************
*/

/**
Expand Down Expand Up @@ -173,9 +173,9 @@ Object writeReplace() {
}

/*
/**********************************************************
/**********************************************************************
/* State/configuration
/**********************************************************
/**********************************************************************
*/

/**
Expand All @@ -193,11 +193,11 @@ Object writeReplace() {
* @since 2.7
*/
protected transient Closeable _processor;

/*
/**********************************************************
/**********************************************************************
/* Life-cycle
/**********************************************************
/**********************************************************************
*/

/**
Expand Down Expand Up @@ -326,7 +326,7 @@ public static JsonMappingException from(SerializerProvider ctxt, String msg, Thr
*<p>
* NOTE: since 2.9 should usually NOT be used on input-side (deserialization)
* exceptions; instead use method(s) of <code>InputMismatchException</code>
*
*
* @since 2.1
*/
public static JsonMappingException fromUnexpectedIOE(IOException src) {
Expand Down Expand Up @@ -394,9 +394,9 @@ public static JsonMappingException wrapWithPath(Throwable src, Reference ref)
}

/*
/**********************************************************
/**********************************************************************
/* Accessors/mutators
/**********************************************************
/**********************************************************************
*/

/**
Expand Down Expand Up @@ -430,19 +430,18 @@ public StringBuilder getPathReference(StringBuilder sb)
* Method called to prepend a reference information in front of
* current path
*/
public void prependPath(Object referrer, String fieldName)
{
Reference ref = new Reference(referrer, fieldName);
prependPath(ref);
@Override
public void prependPath(Object referrer, String fieldName) {
prependPath(new Reference(referrer, fieldName));
}

/**
* Method called to prepend a reference information in front of
* current path
*/
public void prependPath(Object referrer, int index)
{
Reference ref = new Reference(referrer, index);
prependPath(ref);
@Override
public void prependPath(Object referrer, int index) {
prependPath(new Reference(referrer, index));
}

public void prependPath(Reference r)
Expand All @@ -458,14 +457,14 @@ public void prependPath(Reference r)
_path.addFirst(r);
}
}

/*
/**********************************************************
/**********************************************************************
/* Overridden methods
/**********************************************************
/**********************************************************************
*/

@Override // since 2.7.5
@Override // since 2.8
@JsonIgnore // as per [databind#1368]
public Object getProcessor() { return _processor; }

Expand Down Expand Up @@ -509,9 +508,9 @@ public String toString()
}

/*
/**********************************************************
/**********************************************************************
/* Internal methods
/**********************************************************
/**********************************************************************
*/

protected void _appendPathDesc(StringBuilder sb)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.BitSet;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonDeserializer;
Expand Down Expand Up @@ -209,7 +210,7 @@ protected Object _findMissing(SettableBeanProperty prop) throws JsonMappingExcep
// Fourth: default value
JsonDeserializer<Object> deser = prop.getValueDeserializer();
return deser.getNullValue(_context);
} catch (JsonMappingException e) {
} catch (DatabindException e) {
// [databind#2101]: Include property name, if we have it
AnnotatedMember member = prop.getMember();
if (member != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ public Object deserializeFromObject(JsonParser p, DeserializationContext ctxt) t
// Sanity check: did we find "message"?
if (throwable == null) {
/* 15-Oct-2010, tatu: Can't assume missing message is an error, since it may be
* suppressed during serialization, as per [JACKSON-388].
* suppressed during serialization.
*
* Should probably allow use of default constructor, too...
*/
//throw new JsonMappingException("No 'message' property found: could not deserialize "+_beanType);
//throw new XxxException("No 'message' property found: could not deserialize "+_beanType);
if (hasStringCreator) {
throwable = _valueInstantiator.createFromString(ctxt, null);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ public Object findSerializationContentConverter(AnnotatedMember a) {

@Override
public JavaType refineSerializationType(final MapperConfig<?> config,
final Annotated a, final JavaType baseType) throws JsonMappingException
final Annotated a, final JavaType baseType)
throws JsonMappingException
{
JavaType type = baseType;
final TypeFactory tf = config.getTypeFactory();
Expand Down Expand Up @@ -818,15 +819,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
type = type.withStaticTyping();
} else {
throw new JsonMappingException(null,
throw _databindException(
String.format("Cannot refine serialization type %s into %s; types not related",
type, serClass.getName()));
}
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Failed to widen type %s with annotation (value %s), from '%s': %s",
type, serClass.getName(), a.getName(), iae.getMessage()),
iae);
type, serClass.getName(), a.getName(), iae.getMessage()));
}
}
}
Expand All @@ -853,15 +853,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
keyType = keyType.withStaticTyping();
} else {
throw new JsonMappingException(null,
throw _databindException(
String.format("Cannot refine serialization key type %s into %s; types not related",
keyType, keyClass.getName()));
}
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Failed to widen key type of %s with concrete-type annotation (value %s), from '%s': %s",
type, keyClass.getName(), a.getName(), iae.getMessage()),
iae);
type, keyClass.getName(), a.getName(), iae.getMessage()));
}
}
type = ((MapLikeType) type).withKeyType(keyType);
Expand Down Expand Up @@ -889,15 +888,14 @@ public JavaType refineSerializationType(final MapperConfig<?> config,
// 27-Apr-2017, tatu: [databind#1592] ignore primitive<->wrapper refinements
contentType = contentType.withStaticTyping();
} else {
throw new JsonMappingException(null,
throw _databindException(
String.format("Cannot refine serialization content type %s into %s; types not related",
contentType, contentClass.getName()));
}
} catch (IllegalArgumentException iae) { // shouldn't really happen
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Internal error: failed to refine value type of %s with concrete-type annotation (value %s), from '%s': %s",
type, contentClass.getName(), a.getName(), iae.getMessage()),
iae);
type, contentClass.getName(), a.getName(), iae.getMessage()));
}
}
type = type.withContentType(contentType);
Expand Down Expand Up @@ -1200,10 +1198,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
try {
type = tf.constructSpecializedType(type, valueClass);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Failed to narrow type %s with annotation (value %s), from '%s': %s",
type, valueClass.getName(), a.getName(), iae.getMessage()),
iae);
type, valueClass.getName(), a.getName(), iae.getMessage()));
}
}
// Then further processing for container types
Expand All @@ -1218,10 +1215,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
keyType = tf.constructSpecializedType(keyType, keyClass);
type = ((MapLikeType) type).withKeyType(keyType);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Failed to narrow key type of %s with concrete-type annotation (value %s), from '%s': %s",
type, keyClass.getName(), a.getName(), iae.getMessage()),
iae);
type, keyClass.getName(), a.getName(), iae.getMessage()));
}
}
}
Expand All @@ -1235,10 +1231,9 @@ public JavaType refineDeserializationType(final MapperConfig<?> config,
contentType = tf.constructSpecializedType(contentType, contentClass);
type = type.withContentType(contentType);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(null,
throw _databindException(iae,
String.format("Failed to narrow value type of %s with concrete-type annotation (value %s), from '%s': %s",
type, contentClass.getName(), a.getName(), iae.getMessage()),
iae);
type, contentClass.getName(), a.getName(), iae.getMessage()));
}
}
}
Expand Down Expand Up @@ -1565,4 +1560,14 @@ private boolean _primitiveAndWrapper(JavaType baseType, Class<?> refinement)
}
return false;
}

// @since 2.12
private JsonMappingException _databindException(String msg) {
return new JsonMappingException(null, msg);
}

// @since 2.12
private JsonMappingException _databindException(Throwable t, String msg) {
return new JsonMappingException(null, msg, t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,9 @@ protected final void serializeAsArray(Object bean, JsonGenerator gen, Serializer
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
wrapAndThrow(provider, e, bean, name);
} catch (StackOverflowError e) {
JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
DatabindException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
mapE.prependPath(bean, name);
throw mapE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,10 +782,10 @@ protected void serializeFields(Object bean, JsonGenerator gen, SerializerProvide

// 10-Dec-2015, tatu: and due to above, avoid "from" method, call ctor directly:
//JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);

String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(bean, name);
throw mapE;
}
}
Expand Down Expand Up @@ -830,9 +830,9 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator gen,
} catch (StackOverflowError e) {
// Minimize call depth since we are close to fail:
//JsonMappingException mapE = JsonMappingException.from(gen, "Infinite recursion (StackOverflowError)", e);
JsonMappingException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
DatabindException mapE = new JsonMappingException(gen, "Infinite recursion (StackOverflowError)", e);
String name = (i == props.length) ? "[anySetter]" : props[i].getName();
mapE.prependPath(new JsonMappingException.Reference(bean, name));
mapE.prependPath(bean, name);
throw mapE;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,10 @@ public void writeObject(Object value) throws IOException
return;
}
if (_objectCodec == null) {
/* 28-May-2014, tatu: Tricky choice here; if no codec, should we
* err out, or just embed? For now, do latter.
*/
// throw new JsonMappingException("No ObjectCodec configured for TokenBuffer, writeObject() called");
// 28-May-2014, tatu: Tricky choice here; if no codec, should we
// err out, or just embed? For now, do latter.

// throw new XxxException("No ObjectCodec configured for TokenBuffer, writeObject() called");
_appendValue(JsonToken.VALUE_EMBEDDED_OBJECT, value);
} else {
_objectCodec.writeValue(this, value);
Expand Down

0 comments on commit ef34d08

Please sign in to comment.