java - Trying to migrate custom generic gson deserializer to jackson -


currently deserializing gson , retrofit using retrofits gsonconverterfactory:

gsonbuilder gsonbuilder = new gsonbuilder(); gsonbuilder.registertypeadapter(new typetoken<map<book, collection<author>>>(){}.gettype(), new booksdeserializer(context)); gson gson = gsonbuilder.create();  retrofit retrofit = new retrofit.builder()     .baseurl(url)     .addconverterfactory(gsonconverterfactory.create(gson))     .build();  bookservice service = retrofit.create(bookservice.class); response<map<book, collection<author>>> response = service.getbooks().execute();     

i use jacksonconverterfactory provided retrofit? need provide jackson mapper. there way provide type information mapper did gson?

simplemodule simplemodule = new simplemodule(); // todo provide mapper info needed deserialize  // map<book, collection<author>> mapper.registermodule(simplemodule);  retrofit retrofit = new retrofit.builder()     .baseurl(url)     .addconverterfactory(jacksonconverterfactory.create(mapper))     .build();  bookservice service = retrofit.create(bookservice.class); response<map<book, collection<author>>> response = service.getbooks().execute(); 

looking @ todo can tell mapper use deserializer?

public class booksdeserializer extends jsondeserializer<map<book, collection<author>>> {      @override     public map<book, collection<author>> deserialize(jsonparser parser, deserializationcontext context) throws ioexception, jsonprocessingexception {         // deserialize here     } } 

according api, simplemodule.adddeserializer(java.lang.class, com.fasterxml.jackson.databind.jsonserializer) requires instance of jsonserializer<t> whereby t supertype of class supply argument, i.e. deserializer needs able deserialize objects subclass of supplied class; typereference<map<book, collection<author>>> not subtype of map<book, collection<author>>.

however, writing serializer maps not easy java's type erasure. 1 way of doing write wrapper class map, such as

@xmlrootelement public class serializablebookmapwrapper {      private map<book, collection<author>> wrapped;      public serializablebookmapwrapper(final map<book, collection<author>> wrapped) {         this.wrapped = wrapped;     }      public map<book, collection<author>> getwrapped() {         return wrapped;     }      public void setwrapped(final map<book, collection<author>> wrapped) {         this.wrapped = wrapped;     } } 

with kind of wrapper class, can implement jsondeserializer<serializablebookmapwrapper> , use instead. however, if haven't used jackson annotations in definition of book , author, need supply custom deserializers them well.

alternatively, can try supplying type information while using objectmapper instance deserialization.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -