Add `ObjectMapper.updateValue()` method to update instance with given overrides
See original GitHub issueObjectMapper 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.
public void convertValue(Object fromValue, Object toValue)
throws IllegalArgumentException {
if (fromValue == null || toValue == null) {
return;
}
JavaType toValueType = _typeFactory.constructType(toValue.getClass());
TokenBuffer buf = new TokenBuffer(this, false);
if (isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
buf = buf.forceUseOfBigDecimal(true);
}
try {
SerializationConfig config = getSerializationConfig().
without(SerializationFeature.WRAP_ROOT_VALUE);
_serializerProvider(config).serializeValue(buf, fromValue);
JsonParser jp = buf.asParser();
DeserializationConfig deserConfig = getDeserializationConfig();
JsonToken t = _initForReading(jp);
if (t != JsonToken.VALUE_NULL
&& t != JsonToken.END_ARRAY
&& t != JsonToken.END_OBJECT) {
DeserializationContext ctxt = createDeserializationContext(jp, deserConfig);
JsonDeserializer<Object> deser = _findRootDeserializer(ctxt, toValueType);
deser.deserialize(jp, ctxt, toValue);
}
jp.close();
} catch (IOException e) { // should not occur, no real i/o...
throw new IllegalArgumentException(e.getMessage(), e);
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (7 by maintainers)
Top Results From Across the Web
Add `ObjectMapper.updateValue()` method to update instance ...
ObjectMapper already have method to convertValue by doing two-step ... updateValue() method to update instance with given overrides #1556.
Read more >Jackson JSON - Updating Existing objects with JSON input ...
The following method of ObjectMapper creates ObjectReader instance that can be used to update an existing Object while deserializing an incoming ...
Read more >ObjectMapper (jackson-databind 2.9.0 API) - FasterXML
Convenience conversion method that will bind data given JSON tree contains into specific value (usually bean) type. <T> T, updateValue(T valueToUpdate, Object ...
Read more >Working with structured data in Java - Advanced Web Machinery
Updating entries in a Map is not a big deal, but the updateValue method can also use Java classes to supply the override...
Read more >How to force Jackson object mapper to ignore not full fields ...
The idea behind the readerForUpdating method is not to create a new instance of the object,just to replace the values of the passed...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
updateValue()
on an array will succeed, but has to create a new instance and return that. But type should be the same so usage should be convenient and allow chaining.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.
@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
readValue()
. No special handling is applied aside from skipping construction of value. Type parameter is used for finding deserializer to use.