Battling Jackson
This week at work I spent far too much time battling the FasterXML Jackson JSON library that I feel the need to write about the struggle.
What I wanted to do was to be able to deserialize my models through multiple levels of polymorphic inheritance working off of different property values. In this case I was developing in Scala. When trying to find a solution, I kept seeing people use the example of a zoo, So I will use the Zoo example in my work here.
The more I need to do custom JSON parsing with Jackson, the more that I have become familiar with their annotations. And this task would have been really easy if I could use the JsonTypeInfo and JsonSubTypes annotations, but I discovered a limitation that has been set in Jackson's Parser.
Eventually this Stack Overflow question led me to this Jackson Github issue where it explains:
But then we run into a problem when we want to deserialize off of more than just the property "kind". What if our animals had morality that defined them, and our Dogs and Cats can either be good or evil. Adding a second set of JsonTypeInfo and JsonSubTypes annotations that key off of "morality" will be ignored. Our only option is define a custom deserializer:
There we have it! A custom Jackson JSON Deserializer that will deserialize off of multiple properties. I fought the Jackson and I won!
What I wanted to do was to be able to deserialize my models through multiple levels of polymorphic inheritance working off of different property values. In this case I was developing in Scala. When trying to find a solution, I kept seeing people use the example of a zoo, So I will use the Zoo example in my work here.
The more I need to do custom JSON parsing with Jackson, the more that I have become familiar with their annotations. And this task would have been really easy if I could use the JsonTypeInfo and JsonSubTypes annotations, but I discovered a limitation that has been set in Jackson's Parser.
Eventually this Stack Overflow question led me to this Jackson Github issue where it explains:
No, full inheritance is supported, but you can not use separate type discriminator properties: it is not possible to use, say "type" AND "subtype" -- you must use just one like "type". Subtype dependencies may be defined in chaining fashion as you mentioned (from parent to intermediate subtype; from intermediate subtype to further subtypes).See, the zoo example is very simple when the subtypes are described by a single "type" property:
Loading GitHub Gist...
But then we run into a problem when we want to deserialize off of more than just the property "kind". What if our animals had morality that defined them, and our Dogs and Cats can either be good or evil. Adding a second set of JsonTypeInfo and JsonSubTypes annotations that key off of "morality" will be ignored. Our only option is define a custom deserializer:
Loading GitHub Gist...
There we have it! A custom Jackson JSON Deserializer that will deserialize off of multiple properties. I fought the Jackson and I won!
Comments
Post a Comment