-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing untyped object deserialization #89
Comments
What you're running into is the fact that the underlying Jackson system processes values in a narrow context. At the top level, you've told the deserializer you want a Scala Map[String, Any], and so it obliges. Buy when it process the values, all it has is the "Any" type, and the fact that it's reading a JSON Object. It doesn't "remember" that you wanted a particular type at the root that would work. Those that need untyped data will serialize instead to JsonNode, which has some advantages over maps. Those that want type checking will define (case) classes that model the data set they are trying to parse. |
So, the main value for me is being able to write code that operates on Maps generically and not have to worry about JMapWrapper[String, Any] vs Map[String, Any] or even worse, writing code that assumes everything is going to be MapLike[String, Any, MapLike[String, Any]]. Imagine a function that handles some data structure that can either be inside a list or standalone from a client. Right now, that function needs to handle both JMapWrapper (if it comes from the list) and Map[String, Any] if it comes directly. It also exposes an implementation detail to generic code that can take data from a database (which is going to come back as a Map) vs from JSON. The solution may be instead (or perhaps additionally to) assuming the root object should be used for any map-like Anys that there is some way in the ObjectMapper to specify what type you would like for array-like and map-like objects. Does that make sense? |
(btw, switching to using collection.Map as the common descendant made the code nicer, but it would still be nice to fix this generically) |
Not sure if this helps, but it is possible to instruct Jackson with respect to which |
Oh, how do you do that? |
@cowtowncoder, the feature being requested isn't exactly what you're describing. IIUC, you are describing the feature that, given a Bean class with a property of type To put it in Java terms, what @tberman is asking for is, when deserializing arbitrary JSON to As far as it goes, I feel that those facts are an implementation detail, and that the only guarantees (albeit undocumented) the module gives is to return some implementation of Given that, and given that Jackson has other, better methods to support stronger typing, I'm probably not going to implement any configuration for this in the near future, unless the databind module were to add similar support for Java. But I'll leave the issue open for the sake of documentation, in case someone wants to submit a patch. :) |
Right, I realize that it is not exact match; but what I was suggesting was that perhaps ability to change the default impl could resolve the issue. However, I am guessing it wouldn't since it has to do with wrapping. |
Well, it might; I hadn't yet searched the code for the functionality you're describing, and I (incorrectly, it seems) guessed that it was a specific override setting for JDK Collection/Map types. But now I suspect you're talking about The Scala module could check the AbstractTypeResolvers to find a mapping for |
You are right, I was specifically referring to And I think your thinking along lines of Scala module registering its own resolver could make sense. If all goes well, generic types should work too (that is, not losing parameterization of Map(Like)s, Collection(Like)s). UntypedObjectDeserializer... good point, that isn't connected, but it really should. |
I've added support for recognizing AbstractTypeResolvers. The test added in 8421eba should demonstrate how to use it. |
So, I am using the scala module, and parsing json that looks like:
{ "type": "blah", "data": { "key": "value" } }
The outer object I am parsing directly into a Map[String, Any] which is working fine. However, the "data" object ends up being a JMapWrapper instead of a Map[String, Any] as I would expect.
What am I missing?
The text was updated successfully, but these errors were encountered: