java - GSON and Generic types -


i've come across problem of using gson library , generic types(my types , collections). have answer how solve problem, don't think it's appropriate write specific message converter every type i've implemented , i'll implement.

what did is:

implemented own message converter:

public class superhttpmessageconverter extends abstracthttpmessageconverter<object> {  private final charset charset; private final gson gson;  public costomhttpmc_1(mediatype mediatype, string charset) {     super(mediatype);     this.charset = charset.forname(charset);     gson = new gsonbuilder().excludefieldswithoutexposeannotation().create(); }  @override protected object readinternal(class clazz, httpinputmessage inputmessage) throws ioexception {     string jsonstring = filecopyutils.copytostring(new inputstreamreader(inputmessage.getbody(), charset));     return gson.fromjson(jsonstring, clazz); }  @override protected long getcontentlength(object obj, mediatype contenttype) {     try {         string jsonstring = gson.tojson(obj);         return (long) jsonstring.getbytes(charset.name()).length;     } catch (unsupportedencodingexception ex) {         throw new internalerror(ex.getmessage());     } }  @override protected void writeinternal(object obj, httpoutputmessage outputmessage) throws ioexception {     string jsonstring = gson.tojson(obj);     filecopyutils.copy(jsonstring, new outputstreamwriter(outputmessage.getbody(), charset)); }  @override public boolean supports(class<?> clazz) {     return true; } 

}

it works until try send collection list<string> or some type<t>.

gson has solutions here: http://sites.google.com/site/gson/gson-user-guide

also tried json-lib library yesterday. don't in-depth scanning of objects have in hierarchy. tried change cycle detection strategy cycledetectionstrategy.strict cycledetectionstrategy.lenient, didn't @ all!

    @override protected void writeinternal(object obj, httpoutputmessage outputmessage) throws ioexception {     jsonconfig jsonconfig = new jsonconfig();       jsonconfig.setcycledetectionstrategy(cycledetectionstrategy.lenient);     string jsonstring = jsonobject.fromobject( obj ).tostring();     filecopyutils.copy(jsonstring, new outputstreamwriter(outputmessage.getbody(), charset)); } 

finally, work-around generic collection's problem found out: changing arraylist simple array helps serialization , deserialization. more specific have in web-service, use in application.

   @requestmapping(value = "/country/info/{code}") public void info(@pathvariable("code") string code, model model) {  //list     stuffimpl[] stufflist= new stuffimpl[0]; <-- array used! stufflist= resttemplate.getforobject("http://localhost:8084/yourapp/restservice/stuff", stufflist.getclass()); model.addattribute("stufflist", stufflist); } 

so approach working good.

i failed found out solution generic type is. hate idea write new converter every time implement new generic type.

if know possible solution i'd appreciate lot!

i'd on cloud 9 if me :)

l.

there methods can pass java.lang.reflect.type. these methods useful if specified object generic type, e.g.:

gson gson = new gsonbuilder().create();  list<string> names = new arraylist<string>(); names.add("foo"); names.add("bar");  // marshal string jsonliteral = gson.tojson(names); system.out.println(jsonliteral);  // unmarshal list<string> names2; type type = new typetoken<list<string>>() { }.gettype(); names2 = gson.fromjson(jsonliteral, type); system.out.println(names2.get(0)); system.out.println(names2.get(1)); 

this output:

["foo","bar"] foo bar 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -