question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add `ObjectMapper.updateValue()` method to update instance with given overrides

See original GitHub issue

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.

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:closed
  • Created 7 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
cowtowncodercommented, Mar 21, 2017

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.

0reactions
cowtowncodercommented, Aug 27, 2019

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found