You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a "ParentType" (interface or abstract class)
I have several subtypes of ParentType
I am using @JsonTypeInfo to add a "type" property to differentiate between different types
ParentType is typically, but not always found in a List<ParentType>.
Everything so far works fine, but what I want to add is:
If deserialization of a particular item fails, replace it with a "FailedType" (still a subtype of ParentType) that captures either the JsonNode or just a string representation of the JSON that failed to deserialize.
This can be shown to the user in a list of items alongside the items that successfully deserialized, allowing the user to see the JSON that failed to deserialize.
I need to be able to persist the bad items along with the good items, so that the user can fix the issues later, or correct the issue causing it to not deserialize correctly (e.g. missing something on the classpath).
My first thought was to use a DeserializationProblemHandler, however, this really only catches common issues at the same level. That is, if the issue occurs in a nested object, it's not of much help. I want it to be a catch-all for anything that happens below this level (though there is a possibility of having a ParentType within another ParentType, in which case I would want it to be caught by the deepest ParentType).
I tried playing around with a custom ValueDeserializer, or extending StdDeserializer, but the problem is that no matter what I tried, any attempt to get it to perform the "normal" deserialization logic (so that I can catch an error) would result in a stack overflow, as my custom deserializer would effectively just call itself. I also tried things with createContextual() to no avail (findRootValueDeserializer/findContextualValueDeserializer produces the stack overflow instead). I haven't been able to make this work on Jackson 2 nor 3. I also played around with custom TypeIdResolvers, but this also lead to either stack overflows, or other breakages.
What I'm looking for is:
Is this possible at all, or should I feature request this?
Am I using the right approach, or is there a better way to do this?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I have a fairly typical implementation:
@JsonTypeInfoto add a "type" property to differentiate between different typesList<ParentType>.Everything so far works fine, but what I want to add is:
JsonNodeor just a string representation of the JSON that failed to deserialize.My first thought was to use a
DeserializationProblemHandler, however, this really only catches common issues at the same level. That is, if the issue occurs in a nested object, it's not of much help. I want it to be a catch-all for anything that happens below this level (though there is a possibility of having aParentTypewithin anotherParentType, in which case I would want it to be caught by the deepestParentType).I tried playing around with a custom
ValueDeserializer, or extendingStdDeserializer, but the problem is that no matter what I tried, any attempt to get it to perform the "normal" deserialization logic (so that I can catch an error) would result in a stack overflow, as my custom deserializer would effectively just call itself. I also tried things withcreateContextual()to no avail (findRootValueDeserializer/findContextualValueDeserializer produces the stack overflow instead). I haven't been able to make this work on Jackson 2 nor 3. I also played around with customTypeIdResolvers, but this also lead to either stack overflows, or other breakages.What I'm looking for is:
Here is an example of what I asm trying to accomplish. The two serialization tests work fine, but none of the deserialization test cases work - they all stack overflow: https://gist.github.com/xpdota/83e9c1f9d9a36ba8b1576f40ba489e27
Thank you for any help.
Beta Was this translation helpful? Give feedback.
All reactions