serialization - XStream canConvert issue for paramterised generics -
i have requirement have write custom xstream mapconverter converts specific type of map. give example, want custom converter work map(string, date) maps. need achieve overriding canconvert method. of now, canconvert method have written :
@override public boolean canconvert(class clazz) { return (!clazz.equals(object.class) && map.class.isassignablefrom(clazz)); } but work type of maps. since "java.lang.class" not expose method gives information type of parameters, generic collections, unable achieve desired result in canconvert method.
one workaround think of creating dummy wrapper class around map(string, date) , using implement canconvert method. suggest better way of tackling issue in canconvert method ?
you can't generics here since they're not present @ runtime. try grabbing first key , entry value , checking types. wouldn't work empty map, if map empty don't need convert anyways.
map<string, date> m = new hashmap<string, date>(); m.put("test", new date()); object key = m.keyset().iterator().next(); object value = m.get(key); system.out.println(key.getclass().isassignablefrom(string.class)); system.out.println(value.getclass().isassignablefrom(date.class)); other it's say, pretty have make own wrapper class or roll custom implementation of map accepts string keys , date values.
Comments
Post a Comment