pawel.rybicki
10/08/2019, 12:28 PMMoshi moshi = new Moshi.Builder().build();
JsonAdapter<BlackjackHand> jsonAdapter = moshi.adapter(BlackjackHand.class);
String json = jsonAdapter.toJson(blackjackHand);
and I've been thinking its a bit of a boiler plate to write it for each class, so I came up with the idea of this:
fun Any.toJson() = Moshi.Builder().build().adapter(Any::class.java).toJson(this)
this way I can simply call AnyObj.toJson()
, but I wonder if there is any downside of such solution?jw
10/08/2019, 12:29 PMjw
10/08/2019, 12:29 PMjw
10/08/2019, 12:30 PMAny
as the lookup type is not recommendedpawel.rybicki
10/08/2019, 12:32 PMmoshiInstance.adapter(Any::class.java).toJson(this)
would be a little improvement, but using Any
will slow down parsing object or...?jw
10/08/2019, 1:14 PMinline fun <reified T> Moshi.adapter(): JsonAdapter<T> {
return adapter(typeOf<T>())
}