-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Expanding capabilities of @JsonRootName #662
Comments
One possibility would be to make use of JSONPointer notation; I have been thinking of adding |
I had no idea that existed. Personally, i think i prefer the array since there's no need to learn a new pointer syntax and it feels cleaner to me. However, if you're looking for a common solution for other areas it may be best to go with a standard with a spec. Is pointer notation used elsewhere in jackson already? |
JSON pointer is minimally simple, really. Just separate names with slashes. But there is also the other question of wrapping outside of root names. So perhaps one possibility would be to add String-array notation for |
That sounds like a good option if you're okay using two approaches, at least that way |
Disclaimer
I'm not sure where to put something like this, so if it should be elsewhere let me know.
Proposal
I would like to propose expansion of the
@JsonRootName
annotation's usage to allow specifying more than a single level of nesting. In order to avoid creating a custom syntax and remain valid among json keys, I propose the following interface:public String[] value() default "";
Which would be used like:
With the idea that it would allow the deserialization of:
Naive Implementation
The approach I've implemented for my own purposes to extract the relevant piece before reading as a particular class (I'm not suggesting this be the implementation, just giving an example):
Why?
I recently started consuming an API that provides some meta-data around each of the responses that it provides. This information may be useful for them and some more advanced API's, but in reality I'm only interested in consuming the inner contents. I got around this by definining a custom annotation and custom deserializer (that unfortunately couldn't extend JsonDeserializer, until this is released: #165).
There's a strong possibility that there already exists a preferred way to handle this situation and I'm just not aware of it. If that's the case, feel free to direct me to some documentation and close this ticket.
I think this particular approach has the added bonus of being entirely backwards compatible and (I think) pretty simple to implement once these questions are answered:
Questions
ex:
@JsonRootName( value = {"root", "results[0]"} )
, what happens if the element doesn't exist in the array?Suggestions
The question crossed my mind as to whether this should be extended to field annotations and the like. Common sense quickly said "NO", this should only be used for classes for this kind of usecase, not arbitrary deserialization mappings since that just invites problems.
Anticipated Concerns:
Concern:
it may be considered a deviation from the generally agreed upon best-practice that a model in your application should be the same as the data model being consumed.
Response:
JSON is often used to represent nested object relationships, this just encourages consumption in that manner while decreasing the amount of overhead involved with narrowing a verbose response to the relevant information.
Concern:
@JsonRootName
might be a confusing name since using this method, you could technically point to something that isn't really a "root node" of the response, instead it is the "root node" upon which the object should be deserialized.Response:
Uh, I agree it might be unclear. The reason I suggested extending this interface is because the single-element use case would be no different than the existing behavior. I think it is probably best to add it here.
The text was updated successfully, but these errors were encountered: