diff options
author | Tatu Saloranta <tatu.saloranta@iki.fi> | 2016-05-03 23:03:34 -0700 |
---|---|---|
committer | Tatu Saloranta <tatu.saloranta@iki.fi> | 2016-05-03 23:03:34 -0700 |
commit | 6ed7caab6700210ab65746fc12a45be0044974d1 (patch) | |
tree | 77f3af68260a8ca9195b2d6deedce791237c47b8 | |
parent | 02faabc79b8cd69efdd8683a1755b6025d5f643e (diff) | |
download | jackson-databind-6ed7caab6700210ab65746fc12a45be0044974d1.tar.gz |
Fix #903; add `JsonGenerator` ref to `SerializerProvider` and change `JsonMappingException` construction to use it
9 files changed, 61 insertions, 33 deletions
diff --git a/release-notes/VERSION b/release-notes/VERSION index c30948956..6f555bb0d 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -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`) diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java index 20928b2fc..d60e01bef 100644 --- a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java +++ b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java @@ -300,11 +300,7 @@ public class JsonMappingException * @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); } /** @@ -314,8 +310,7 @@ public class JsonMappingException /* 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); } /** diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java b/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java index 6d7e0cd69..3506cd2c1 100644 --- a/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/JsonSerializer.java @@ -157,7 +157,7 @@ public abstract class JsonSerializer<T> 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()); } diff --git a/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java b/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java index 51435e93d..53d8ea9fe 100644 --- a/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java +++ b/src/main/java/com/fasterxml/jackson/databind/SerializerProvider.java @@ -1096,19 +1096,62 @@ public abstract class SerializerProvider */ /** + * 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 diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java index 035b14736..2891d440a 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/AnyGetterWriter.java @@ -45,7 +45,7 @@ public class AnyGetterWriter 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) @@ -68,7 +68,7 @@ public class AnyGetterWriter 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? diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java b/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java index 0d6a9190b..81c9ba888 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/BeanPropertyWriter.java @@ -907,7 +907,7 @@ public class BeanPropertyWriter extends PropertyWriter // which extends // (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; diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java b/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java index 21fdcbe49..ed8e965c2 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/DefaultSerializerProvider.java @@ -136,17 +136,6 @@ public abstract class DefaultSerializerProvider 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 @@ -252,6 +241,7 @@ public abstract class DefaultSerializerProvider * * @since 2.8 */ + @Override public JsonGenerator getGenerator() { return _generator; } @@ -343,7 +333,6 @@ public abstract class DefaultSerializerProvider 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(); @@ -352,7 +341,7 @@ public abstract class DefaultSerializerProvider } } 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(); @@ -370,7 +359,7 @@ public abstract class DefaultSerializerProvider if (msg == null) { msg = "[no message for "+e.getClass().getName()+"]"; } - throw JsonMappingException.from(gen, msg, e); + reportMappingProblem(e, msg); } } @@ -435,7 +424,7 @@ public abstract class DefaultSerializerProvider if (msg == null) { msg = "[no message for "+e.getClass().getName()+"]"; } - throw JsonMappingException.from(gen, msg, e); + reportMappingProblem(e, msg); } } @@ -499,7 +488,7 @@ public abstract class DefaultSerializerProvider if (msg == null) { msg = "[no message for "+e.getClass().getName()+"]"; } - throw JsonMappingException.from(gen, msg, e); + reportMappingProblem(e, msg); } } @@ -531,7 +520,7 @@ public abstract class DefaultSerializerProvider if (msg == null) { msg = "[no message for "+e.getClass().getName()+"]"; } - throw JsonMappingException.from(gen, msg, e); + reportMappingProblem(e, msg); } } /* diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java index eb4e0645b..6c1530b70 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnknownSerializer.java @@ -66,7 +66,7 @@ public class UnknownSerializer 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()); } } diff --git a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java index 8005263b2..834e03ac3 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/ser/impl/UnwrappingBeanSerializer.java @@ -126,7 +126,7 @@ public class UnwrappingBeanSerializer 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) { |