Skip to content

Variant of ObjectMapper.convertValue() that does "Deep Copy" (Clone) #2007

@akefirad

Description

@akefirad

cc: @cowtowncoder
I saw people using Jackson to do it, but in a not-so-efficient way:

MyPojo myPojo = new MyPojo();
ObjectMapper mapper = new ObjectMapper();
MyPojo newPojo = mapper.readValue(mapper.writeValueAsString(myPojo), MyPojo.class);

I know about ObjetMapper.convertValue method, but the problem is that it returns the original value is of the given type:

...
            Class<?> targetType = toValueType.getRawClass();
            if (targetType != Object.class
                    && !toValueType.hasGenericTypes()
                    && targetType.isAssignableFrom(fromValue.getClass())) {
                return fromValue;
            }
...

And also the note is the documentation which reads:

Note that it is possible that in some cases behavior does differ from full serialize-then-deserialize cycle

So can we have (an overloaded version of) convertValue (or a new method) to behave totally as deep copy?

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.0Issue planned for initial 3.0 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions