Search code examples
javajsonserializationjacksonjson-deserialization

Jackson: Map<String, Object> serialization and deserialization


Is it possible to serialize/deserialize Map<String, Object> where type of object (mapped value) would be determined by its key.

{
    "number":42,
    "desc":"something really important",
    "location":{
        "x":9999,
        "y":9999,
        "wkid":9999
    },
    "shape":{
        "type":"polyline",
        "data":"dGhpcyBnZW9tZXRyeSBpcyBmYWtlIDpQ"
    }
}

In this particular case I know that desc is always going to be string, location and shape - 3rd party classes (I have serializers and deserializers for them) and other values are boxed primitives.

Is it possible to do something like this with jackson? Could such thing be done by using MapSerializer/MapDeserializer? (I couldn't find any examples)


Solution

  • Yes, it's possible. I would recommend you to try jsonschema2pojo. Just paste you JSON and select source type JSON.