Serialize POJO To Typed Map

Viewed 40

I have a use-case where I would like to serialize an Object to an instance of Map<String, Object>. I would also like to store type information so as to support polymorphic deserialization.

I can think of the following way to achieve it using Jackson -

objectMapper.readValue(
    typedObjectMapper.writeValueAsString(object), new TypeReference<Map<String, Object>>() {}
)

The above code is working correctly. Is there a better way to do so?

If I am using objectMapper.convertValue(object, new TypeReference<Map<String, Object>>() {}), I am loosing the type information.

convertValue internally does the same things as my first code sample (as per the Javadocs), but its optimized.

If there are no other options, what is the overhead of the first approach?

Thank you!

0 Answers
Related