Jackson: Map serialization and deserialization

后端 未结 2 1072
有刺的猬
有刺的猬 2021-01-20 11:42

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

{
    \"nu         


        
2条回答
  •  死守一世寂寞
    2021-01-20 12:22

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

    For example

    Map map = new HashMap();`
    
    map.put("desc", "something really important");
    
    Map deser = new HashMap();
    
    deser.put("desc", StringDeserializer.class);
    

    You could also use a deser, 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

提交回复
热议问题