-
-
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
Add DeserializationContext.readTreeAsValue()
methods for more convenient conversions for deserializers to use
#3002
Comments
JsonNode
to class (like "valueToTree") via DeserializationContext
JsonNode
to class (like treeToValue()
) via DeserializationContext
Bit torn on naming here; and while |
JsonNode
to class (like treeToValue()
) via DeserializationContext
DeserializationContext.readTreeAsValue()
methods for more convenient conversions for deserializers to use
hi @cowtowncoder , so is it possible to write a fully fluent style oneliner to extract one property and bind as pojo ? Otherwise, do you plan to add it ? mapper.XXreadTree(in).XXget("foo").XXasValue(Pojo.class) // is it possible ?
mapper.treeToValue(mapper.readTree(in).get("foo"), Pojo.class) // not fluent Thanks in advance |
It’s been more than two years since completion and there’ve been patches since then. i think at this point there’s more chance to get answers from documentations and resources out on the internet 👍🏼 |
@jonenst Issue tracker is not a good place for discussions not directly related to specific issue. As to your specific example, no, that will not be possible to support -- |
Hi, I chose this issue because it felt like the base of all the related work surrounding this. sorry if that's not appropriate. I have indeed searched extensively the docs and other internet resources (I'm looking at you stackoverflow) for answers regarding this but never found anything better than the first message of this issue:
I think it's safe to say that people are getting confused by this (for example this top voted answers recommending to create (cheap) configurations objects (writerFor) https://stackoverflow.com/a/39237947/ ), instead of using the simpler treeToValue (I guess because the treeToValue override with java generics was added only later, and the one with a typereference that doesn't require the user to call mapper.constructType even later in 2.16) I think having a fluent api to do this would reduce the confusion because fluent apis give the feeling that the use case it totally natural and supported. Otherwise, adding it to the docs of jackson in N minutes ( https://github.com/FasterXML/jackson-databind/ ) (maybe in the 3 minutes or the 5 minutes section) would be a good alternative. Because as you said, it's "A very common use pattern" Finally, I understand that If you want me to do anything else on this subject, please feel free to ask, otherwise I'll leave it at that. |
@jonenst Thank you for your thorough explanation; it makes sense wrt your motivations and background. And I can see how such fluent operations would be convenient if feasible. So I am not against achieving what you suggest at all. But as to linking There may be other ways to achieve fluent style operations, of course; builder (or such) type that could contain transient reference to mapper as well JsonNode(s). |
@cowtowncoder Hi, I went ahead and wrote #4135 , feel free to take the parts you like from it and discard the rest ! |
(note: inspired by #3000)
A very common use pattern for deserializers is to read content as a tree (
JsonNode
) and then extract properties, either simple scalar values (Strings, numbers, boolean), or to further bind as POJOs via delegation.But while there is a way to achieve that, the "proper" way is somewhat complicated, by:
JsonParser
out ofJsonNode
(methodtraverse()
)find[Non]ContextualValueDeserializer()
) and calling it; or using short-cut ofreadValue()
(or readPropertyValue()`)Because of this, many users instead use construct like:
which has been available since Jackson 2.0, but has some issue regarding not retaining current deserialization context and things like Attributes set.
So it would make sense to add a convenience method that does what is intended starting with existing
JsonNode
(first checking fornull
and "missing node"?), locating deserializer & reading.Naming TBD, could be:
The text was updated successfully, but these errors were encountered: