Skip to content

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

Closed
@syncer

Description

@syncer

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions