Monday 15 September 2014

java - Guava TypeToken for resolving raw type warnings -



java - Guava TypeToken for resolving raw type warnings -

i trying solve java raw-type warnings because of using non-typed library. e.g.

list dataaslist = response.getdatalist();

i know list of type

list<map<string,object>>

but don't know how convert untyped "response.getdatalist()" typed version.

i read guava typetoken used type-problems, not sure how utilize , attempts not working.

if list's generic argument raw type, i.e., not have generic arguments itself, please view my reply different question uses guava solution.

otherwise, serialize/deserialize gson. relatively slow compared other solutions, cleanest implement. assuming not doing many times per request should not worse making i/o phone call such database request. illustration below optimized modification of illustration usage in gson api.

this illustration should work simple classes not need specialized serialization.

private final static gson gson = new gson(); public static <t> t cast(object object, typetoken<t> token){ string json = gson.tojson(object); t result = gson.fromjson(json, token.gettype()); homecoming result; }

usage:

list<map<string,string>> dataaslist = cast( response.getdatalist(), new typetoken<list<map<string,string>>>(){});

disclaimer:

in exact situation describe solution not work because maps value type object , gson not know type want convert to. not sure if throw exception or if leave jsonobject or other type. seek research more.

java types guava

No comments:

Post a Comment