mardi 26 janvier 2021

How to deserialize a complex json-object?

Good day. Please help. Let's say there is such a complex object

Map<String, List<Map<Integer, Map<String, List<String>>>> complex;

It's easy to serialize it:

String json = objectMapper.writeValueAsString(complex)

but how do I deserialize it? How can I save all the types specified.

Is something like that possible? There is a class "Foo" with the method "getComplexObject"

class Foo {
    public Map<String, List<Map<Integer, Map<String, List<String>>>> getComplexObject() {
        return null;
    }
}

Then I can get the method object and the return type of the method, for further saving and deserialization:

ParameterizedType genericReturnType = (ParameterizedType)Foo.getClass().getMethod().getGenericReturnType();
Type actualTypeArgument = genericReturnType.getActualTypeArguments()[0];

and then I can expand the type a bit:

JavaType javaType = objectMapper.getTypeFactory().constructType(actualTypeArgument);

which I will use during deserialization.

Can I somehow save the detailed type of the object so that the result is the same as when using the getComplexObject method?





Aucun commentaire:

Enregistrer un commentaire