jeudi 29 mai 2014

Java - désérialisation d'objets personnalisés génériques imbriqués - Stack Overflow


I was trying to implement the Map interface in Java utils, but instead I'll store the data in Mongo instead of in-memory. I also included generics in it for type safety. I wanted to handle all possible types - wrapper classes, custom classes which in turn can have multiple custom classes inside them and also potentially support something like:


Map<String, Set<Map<String, SampleObject>>> or Map<String, Set<SampleObject>>

I am using GSON to serialize the key and the values in JSONs and storing them and when fetching, deserialize them and return. This will work out for simple generic type parameters. But when it is nested, I have no idea how it will work out. Serialization will still work because I have the object at hand. But how do I know which class to return while deserializing?


Is there something else I can do?




I think it will be decided from your JSON key. For example, {"users" : {...}} means a collection of users and U can map it to Set<User> in your Java class. Also, here you can consider using JAXB.



I was trying to implement the Map interface in Java utils, but instead I'll store the data in Mongo instead of in-memory. I also included generics in it for type safety. I wanted to handle all possible types - wrapper classes, custom classes which in turn can have multiple custom classes inside them and also potentially support something like:


Map<String, Set<Map<String, SampleObject>>> or Map<String, Set<SampleObject>>

I am using GSON to serialize the key and the values in JSONs and storing them and when fetching, deserialize them and return. This will work out for simple generic type parameters. But when it is nested, I have no idea how it will work out. Serialization will still work because I have the object at hand. But how do I know which class to return while deserializing?


Is there something else I can do?



I think it will be decided from your JSON key. For example, {"users" : {...}} means a collection of users and U can map it to Set<User> in your Java class. Also, here you can consider using JAXB.


0 commentaires:

Enregistrer un commentaire