java - GSON generics serialization -


possible duplicate:
deserializing generics gson

so need do:

type fluenttype = new typetoken<bruteforcefluentimpl<gtldigestor.data>>() {}.gettype(); 

instead of

type fluenttype = new typetoken<fluent<t>>() {}.gettype(); // <-- want able this.  string json = gson.tojson(fluent, fluenttype); 

this means every time have specify different type parameter fluent class, need change code in order specify it. right now, type parameter gtldigestor.data.

how do this? (the second line of code won't work)

you need tell gson actual parameterized type (e.g. bruteforcefluentimpl<gtldigestor.data>, including actual runtime raw type , actual type parameter value) @ runtime, because gson needs save information.

using typetoken easiest way type, use must hard-code exact type in source (no type parameters t). if put code in method re-used different types, perhaps method should accept type parameter, caller needs pass in. type can hard-coded @ caller's location, each of presumably uses fixed type.

or, if need generate type dynamically @ runtime based on raw type , type parameters, see answer question here.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -