On Thu, Apr 20, 2017 at 1:28 PM, Andrew Joseph <ap.joseph1...@gmail.com> wrote:
>
> I'm looking to simply return null and add errors to a
> Map<JsonPointer,Exception> from all JsonDeserializer<?> instances when a
> JsonMappingException is encountered (since the JSON parser can continue). In
> each of my beans, I plan to use @JacksonInject to inject the JsonPointer for
> the given bean which I can then use to access the exceptions from the map
> once the deserialization is fully complete.

I understand the idea (and it makes sense), but unfortunately this
approach is all but guaranteed not to work for many cases: if
exception is thrown, state of databinding is unlikely to be such that
further processing would succeed.

However, if you want to try it and see how far you can get, I think I
can help with specific problem here:

> I know this functionality is somewhere on it's way to being incorporated
> into DeserializationProblemHandler as stated here
> https://github.com/FasterXML/jackson-databind/issues/1196
>
> ...but in the meantime I'm looking for an interim solution catch and recover
> from all JsonMappingExceptions that can work with Jackson as it stands.
>
> My first attempt was using BeanDeserializerModifier figuring that I could
> simply wrap all deserializers like so:
>
>
>  import com.fasterxml.jackson.core.JsonParser;
>     import com.fasterxml.jackson.core.JsonProcessingException;
>     import com.fasterxml.jackson.databind.BeanDescription;
>     import com.fasterxml.jackson.databind.DeserializationConfig;
>     import com.fasterxml.jackson.databind.DeserializationContext;
>     import com.fasterxml.jackson.databind.JsonDeserializer;
>     import com.fasterxml.jackson.databind.JsonMappingException;
>     import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
>
>     import java.io.IOException;
>
>     public class ErrorHandlingDeserializerModifier extends
> BeanDeserializerModifier {
>
>       @Override
>       public JsonDeserializer<?> modifyDeserializer(final
> DeserializationConfig config, final BeanDescription beanDesc, final
> JsonDeserializer<?> deserializer) {
>         JsonDeserializer<?> jdesc = super.modifyDeserializer(config,
> beanDesc, deserializer);
>
>         return new JsonDeserializer<Object>() {
>           @Override
>           public Object deserialize(final JsonParser p, final
> DeserializationContext ctxt) throws IOException, JsonProcessingException {
>             try {
>               return jdesc.deserialize(p,ctxt);
>             } catch (JsonMappingException jme) {
>               //add errors to map
>              ...
>               return null;
>             }
>           }
>         };
>       }
>     }
>
>
> However when I go to desalinize a bean property I run into the following
> error:
>
> com.fasterxml.jackson.databind.exc.MismatchedInputException: No
> _valueDeserializer assigned
>
>  at [Source: UNKNOWN; line: -1, column: -1] (through reference chain:
> Jurisdiction["id"])
>  at
> com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:62)
>  at
>

Most likely this is because you really need to delegate
`createContextual()` call of `ContextualDeserializer` (and possibly
`resolve()` of `ResolvableDeserializer`).
You may want to have a look at `StdDelegatingDeserializer` to see what
it does: basically some of initialization is deferred to resolve
problems like cyclic types.
And you probably want to extend `StdDeserializer` instead of directly
implementing `JsonDeserializer`.

Now... as to what I think might be better approach (or complementary
one): registering `DeserializationProblemHandler` would allow you to
handle subset of problems in much safer way.
Callbacks allow you to return `null` (or some other valid
placeholder), and denote information about type of failure.

If you do this I would also be interested in learning about
experiences, and if there are other types of recoverable (to the
degree errors are collected at least) failures that do not yet go
through that handler abstraction: work for adding new methods started
in 2.8 (before that, only "unknown property" handler existed) and
continues with 2.9.

-+ Tatu +-

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to