-
-
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 ObjectMapper.updateValue()
method to update instance with given overrides
#1556
Comments
Seems potentially useful. Couple of questions:
Also curious as to what use cases you might have; since unit tests are needed, would be interesting to use something realistic. |
Our use case; A response object which loaded from two different data sources one of the data sources have multiple format, therefore I need to load it and transform it to the current format then merge it with the response object. |
@syncer Ok let me have a look. I think this could play nicely with other new 2.9 features which allow (f.ex) deep merging. |
Ok implementation is fine, but there is one open issue I am wondering about: how to detect non-updateable Objects, and what to do about them. On one hand, there is a method added in 2.9 to detect types for which "merge" does not work:
which rules out a class of types, most notable scalars. However, here we are returning exact first argument. So, in case of arrays, we could do one of following choices:
I guess I could see either (1) or (3) working. If we went with (3), however, maybe name "updateValue()" would not make sense after all. We could consider some other name, which would reflect the fact that operation may either update given Object, or create a new Object that is combination of two input Objects. What do you think? In the meantime, I think I'll check in my work so far, even if it is likely to change. |
ObjectMapper.updateValue()
method to update instance with given overrides
Decided to make semantics such that whatever the result of logical merge is is returned: this is usually first argument, but not always -- Java arrays, for example, can not be changed (with respect to length), so Testing so far is limited to shallow updates, but will try to find time for deep merge: this will, however, only work via #1399 that is, configuration and/or annotations need to indicate the need for merging. |
I tried the deep merge with |
@syncer If so, I would appreciate a failing unit test to show the problem, along with a new issue (just so we can keep track of what work goes into which version). For what it is worth, I did some work on deep merge of "untyped" values (java.lang.Object) yesterday, so it may be good to try with |
@cowtowncoder Thank you for your time. After I did some digging I found a conflict with lombok |
@syncer Ah. Yes, those can be problematic. Thank you for verifying. |
What if the values to be overridden from, are with different name, like firstName to be overridden with fName in another object, for example? Can we make a use of @JsonAlias here? And is it mandatory for updateValue to have same types as parameter? |
@cvc-saksoft Usage questions should generally be sent to mailing list (https://groups.google.com/forum/#!forum/jackson-user) or Gitter (https://gitter.im/FasterXML/jackson-databind). But as to naming: property names need to match, so incoming JSON just has to map to type, same as if just using |
ObjectMapper already have method to convertValue by doing two-step conversion from given value, into instance of given value type.
It will be great if ObjectMapper can update already created instance.
The text was updated successfully, but these errors were encountered: