jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

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

Open akefirad opened this issue 6 years ago • 0 comments

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?

akefirad avatar Apr 23 '18 09:04 akefirad