문제

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)

도움이 되었습니까?

해결책

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

다른 팁

Yes, it could be done. Have another structure that contains a pair of Map<String, Class> and link the key name of the first structure (Map<String, Object>) to the second one.

For example

Map map = new HashMap<String, Object>();`

map.put("desc", "something really important");

Map deser = new HashMap<String, Class>();

deser.put("desc", StringDeserializer.class);

You could also use a deser<String, String>, where the value would be the class name, then you just do a class loading by name http://www.tutorialspoint.com/java/lang/class_forname_loader.htm

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top