Skip to content

Commit

Permalink
Fix #903; add JsonGenerator ref to SerializerProvider and change …
Browse files Browse the repository at this point in the history
…`JsonMappingException` construction to use it
  • Loading branch information
cowtowncoder committed May 4, 2016
1 parent 02faabc commit 6ed7caa
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 33 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project: jackson-databind

#621: Allow definition of "ignorable types" without annotation (using
`Mapper.configOverride(type).setIsIgnoredType(true)`
#903: Add `JsonGenerator` reference to `SerializerProvider`
#931: Add new method in `Deserializers.Base` to support `ReferenceType`
#990: Allow failing on `null` values for creator (add
`DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,7 @@ public static JsonMappingException from(DeserializationContext ctxt, String msg,
* @since 2.7
*/
public static JsonMappingException from(SerializerProvider ctxt, String msg) {
/* 17-Aug-2015, tatu: As per [databind#903] this is bit problematic as
* SerializerProvider instance does not currently hold on to generator...
*/
JsonGenerator g = null;
return new JsonMappingException(g, msg);
return new JsonMappingException(ctxt.getGenerator(), msg);
}

/**
Expand All @@ -314,8 +310,7 @@ public static JsonMappingException from(SerializerProvider ctxt, String msg, Thr
/* 17-Aug-2015, tatu: As per [databind#903] this is bit problematic as
* SerializerProvider instance does not currently hold on to generator...
*/
JsonGenerator g = null;
return new JsonMappingException(g, msg, problem);
return new JsonMappingException(ctxt.getGenerator(), msg, problem);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void serializeWithType(T value, JsonGenerator gen, SerializerProvider ser
if (clz == null) {
clz = value.getClass();
}
serializers.reportMappingException("Type id handling not implemented for type %s (by serializer of type %s)",
serializers.reportMappingProblem("Type id handling not implemented for type %s (by serializer of type %s)",
clz.getName(), getClass().getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,19 +1096,62 @@ public final void defaultSerializeNull(JsonGenerator gen) throws IOException
*/

/**
* Factory method for constructing a {@link JsonMappingException};
* usually only indirectly used by calling
* {@link #reportMappingProblem(String, Object...)}.
*
* @since 2.6
*/
public abstract JsonMappingException mappingException(String message, Object... args);
public JsonMappingException mappingException(String message, Object... args) {
if (args != null && args.length > 0) {
message = String.format(message, args);
}
return JsonMappingException.from(getGenerator(), message);
}

/**
* Helper method called to indicate problem
* Factory method for constructing a {@link JsonMappingException};
* usually only indirectly used by calling
* {@link #reportMappingProblem(Throwable, String, Object...)}
*
* @since 2.8
*/
protected JsonMappingException mappingException(Throwable t, String message, Object... args) {
if (args != null && args.length > 0) {
message = String.format(message, args);
}
return JsonMappingException.from(getGenerator(), message, t);
}

/**
* Helper method called to indicate problem; default behavior is to construct and
* throw a {@link JsonMappingException}, but in future may collect more than one
* and only throw after certain number, or at the end of serialization.
*
* @since 2.8
*/
public void reportMappingException(String message, Object... args) throws JsonMappingException {
public void reportMappingProblem(String message, Object... args) throws JsonMappingException {
throw mappingException(message, args);
}

/**
* Helper method called to indicate problem; default behavior is to construct and
* throw a {@link JsonMappingException}, but in future may collect more than one
* and only throw after certain number, or at the end of serialization.
*
* @since 2.8
*/
public void reportMappingProblem(Throwable t, String message, Object... args) throws JsonMappingException {
throw mappingException(t, message, args);
}

/**
* @since 2.8
*/
public JsonGenerator getGenerator() {
return null;
}

/*
/********************************************************
/* Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void getAndSerialize(Object bean, JsonGenerator gen, SerializerProvider p
return;
}
if (!(value instanceof Map<?,?>)) {
provider.reportMappingException("Value returned by 'any-getter' %s() not java.util.Map but %s",
provider.reportMappingProblem("Value returned by 'any-getter' %s() not java.util.Map but %s",
_accessor.getName(), value.getClass().getName());
}
// 23-Feb-2015, tatu: Nasty, but has to do (for now)
Expand All @@ -68,7 +68,7 @@ public void getAndFilter(Object bean, JsonGenerator gen, SerializerProvider prov
return;
}
if (!(value instanceof Map<?,?>)) {
provider.reportMappingException("Value returned by 'any-getter' (%s()) not java.util.Map but %s",
provider.reportMappingProblem("Value returned by 'any-getter' (%s()) not java.util.Map but %s",
_accessor.getName(), value.getClass().getName());
}
// 19-Oct-2014, tatu: Should we try to support @JsonInclude options here?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ protected boolean _handleSelfReference(Object bean, JsonGenerator gen,
// (something
// OTHER than {@link BeanSerializerBase}
if (ser instanceof BeanSerializerBase) {
prov.reportMappingException("Direct self-reference leading to cycle");
prov.reportMappingProblem("Direct self-reference leading to cycle");
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,6 @@ public JsonSerializer<Object> serializerInstance(Annotated annotated, Object ser
return (JsonSerializer<Object>) _handleResolvable(ser);
}

@Override
public JsonMappingException mappingException(String message, Object... args) {
if (args != null && args.length > 0) {
message = String.format(message, args);
}
if (_generator != null) {
return JsonMappingException.from(_generator, message);
}
return JsonMappingException.from(this, message);
}

/*
/**********************************************************
/* Object Id handling
Expand Down Expand Up @@ -252,6 +241,7 @@ public boolean hasSerializerFor(Class<?> cls, AtomicReference<Throwable> cause)
*
* @since 2.8
*/
@Override
public JsonGenerator getGenerator() {
return _generator;
}
Expand Down Expand Up @@ -343,7 +333,6 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) t
final boolean wrap;
PropertyName rootName = _config.getFullRootName();
if (rootName == null) { // not explicitly specified
// [JACKSON-163]
wrap = _config.isEnabled(SerializationFeature.WRAP_ROOT_VALUE);
if (wrap) {
gen.writeStartObject();
Expand All @@ -352,7 +341,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) t
}
} else if (rootName.isEmpty()) {
wrap = false;
} else { // [JACKSON-764]
} else {
// empty String means explicitly disabled; non-empty that it is enabled
wrap = true;
gen.writeStartObject();
Expand All @@ -370,7 +359,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType) t
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw JsonMappingException.from(gen, msg, e);
reportMappingProblem(e, msg);
}
}

Expand Down Expand Up @@ -435,7 +424,7 @@ public void serializeValue(JsonGenerator gen, Object value, JavaType rootType,
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw JsonMappingException.from(gen, msg, e);
reportMappingProblem(e, msg);
}
}

Expand Down Expand Up @@ -499,7 +488,7 @@ public void serializePolymorphic(JsonGenerator gen, Object value, JavaType rootT
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw JsonMappingException.from(gen, msg, e);
reportMappingProblem(e, msg);
}
}

Expand Down Expand Up @@ -531,7 +520,7 @@ protected void _serializeNull(JsonGenerator gen) throws IOException
if (msg == null) {
msg = "[no message for "+e.getClass().getName()+"]";
}
throw JsonMappingException.from(gen, msg, e);
reportMappingProblem(e, msg);
}
}
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType t

protected void failForEmpty(SerializerProvider prov, Object value)
throws JsonMappingException {
prov.reportMappingException("No serializer found for class %s and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
prov.reportMappingProblem("No serializer found for class %s and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)",
value.getClass().getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider
TypeSerializer typeSer) throws IOException
{
if (provider.isEnabled(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS)) {
provider.reportMappingException("Unwrapped property requires use of type information: can not serialize without disabling `SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS`");
provider.reportMappingProblem("Unwrapped property requires use of type information: can not serialize without disabling `SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS`");
}
gen.setCurrentValue(bean); // [databind#631]
if (_objectIdWriter != null) {
Expand Down

0 comments on commit 6ed7caa

Please sign in to comment.