android - retrofit and orm library throw StackOverflow -
i try use 2 libraries:
- square/retrofit - rest client
- satyan/sugar - db orm
retrofit use gson, class
public class book{ string name; public book(string name) { this.name = name; } }
ok, retrofit succesfully data server , put in our book class.
now want save data. use orm need extend
parent class
public class book extends sugarrecord<book>{ string name; public book(string name) { this.name = name; } }
but after extend parent class, retrofit cannot parse json.
so error:
java.lang.runtimeexception: error occured while executing doinbackground() ... caused by: retrofit.retrofiterror: java.lang.stackoverflowerror @ retrofit.restadapter$resthandler.invokerequest(restadapter.java:390) ... caused by: java.lang.stackoverflowerror @ com.google.gson.internal.$gson$types.resolve($gson$types.java:375) ...
how make friends 2 libraries use 1 object class?
or how specify retrofit, did not touch book's class parent?
error happens when gson tries resolve type information object have deserialize. gets infinite recursion due cyclic reference book
class in extends
declaration.
however if gson cope class, not recommend using these libs combination.
you see, gson alike standard java serialization in json format. mean gson takes internal state of object , performs serialization. , when parses json creates object state specified in json.
if take @ sugarrecord
class, you'll see has field named "tablename". if passed book
object gson
instance, you'd
{name: "book name", tablename: "table_book"}
.
moreover, if got response server
{name: "another book name", tablename: "something absolutely unrelated"}
,
you instance of book
state matching described in response. meaning, tablename
being not equal like...
you workaround issue using exclusion strategies in gson, overall you'll yet problem.
p.s. after quick @ sugarrecord
class on github not understand why has type parameter @ all. it's not used really. technically think you'll able combine these 2 libraries, if skip type parameter in extends
declaration making class class book extends sugarrecod { }
, , use exclusion strategy. yet, wouldn't myself in practice :).
Comments
Post a Comment