Jackson: Map serialization and deserialization

后端 未结 2 1050
有刺的猬
有刺的猬 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<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

    0 讨论(0)
  • 2021-01-20 12:24

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

    0 讨论(0)
提交回复
热议问题