Ofir Bar
04/18/2020, 7:52 PMinline fun<reified T> Any.deepCopy(): T {
val JSON = Gson().toJson(this)
return Gson().fromJson(JSON, T::class.java)
}
However, when I try to perform this Deep Copy on a List, the items on the list are LinkedTreeMap
.
Can anyone correct me as what can I possible do to resolve it?David Eriksson
04/18/2020, 8:07 PMjw
04/18/2020, 8:08 PMOfir Bar
04/18/2020, 8:23 PMOfir Bar
04/18/2020, 8:29 PMDavid Eriksson
04/18/2020, 8:29 PMOfir Bar
04/18/2020, 8:36 PMbezrukov
04/18/2020, 8:40 PMbezrukov
04/18/2020, 8:42 PMreturn Gson().fromJson(JSON, object : TypeToken<T>() {}.type)
bezrukov
04/18/2020, 8:48 PMsomeObject.deepCopy<ArrayList<Applicant>>
?
this ref isn't what David asked, he asked you to enter into fromJson method, and examine what is passed as T::class.javabezrukov
04/18/2020, 8:50 PM<>
Ofir Bar
04/18/2020, 9:08 PMT::class.java
, something seems not right
Sorry if I seem confused, I am a junior dev
As for the TypeToken, it works perfectly, thank you! I just need to research and understand why hahahajw
04/18/2020, 11:56 PMT::class.java
is ArrayList.class
and there's no type information for the contained items. You can use typeOf<T>
to get a KType and then get a Type from that