Ryan Rolnicki
11/17/2021, 8:53 AMBig Chungus
11/17/2021, 8:55 AMRyan Rolnicki
11/17/2021, 8:56 AMresults: Array<PlacesSearchResult>
as a param; out of the box, kotlin serialization won't know how to turn that into jsonRyan Rolnicki
11/17/2021, 8:56 AMBig Chungus
11/17/2021, 8:57 AMBig Chungus
11/17/2021, 8:57 AMBig Chungus
11/17/2021, 8:57 AMRyan Rolnicki
11/17/2021, 9:03 AMException in thread "main" kotlinx.serialization.SerializationException: Serializer for class 'PlacesSearchResponse' is not found.
Ryan Rolnicki
11/17/2021, 9:05 AM@Serializable
annotation says "at compile time, reflect over this object and create all the serializer logic, so at runtime we don't need to do this reflection", which seems to me that I should be able to apply this to types from other modules (i.e., why can't the plugin do the same thing for typed defined outside my module, ideally recursively)Ryan Rolnicki
11/17/2021, 9:06 AMBig Chungus
11/17/2021, 9:09 AMRyan Rolnicki
11/17/2021, 9:12 AMBig Chungus
11/17/2021, 9:13 AMBig Chungus
11/17/2021, 9:14 AMRyan Rolnicki
11/17/2021, 9:14 AMRyan Rolnicki
11/17/2021, 9:15 AMBig Chungus
11/17/2021, 9:15 AMRyan Rolnicki
11/17/2021, 9:16 AMRyan Rolnicki
11/17/2021, 9:17 AMBig Chungus
11/17/2021, 9:17 AMephemient
11/17/2021, 9:26 AM@Serializer(forClass = )
Ryan Rolnicki
11/17/2021, 9:27 AMBig Chungus
11/17/2021, 9:28 AMBig Chungus
11/17/2021, 9:29 AMRyan Rolnicki
11/17/2021, 9:29 AMRyan Rolnicki
11/17/2021, 9:30 AMephemient
11/17/2021, 9:33 AM@file:UseSerializers
sorta does that, in a file-local wayephemient
11/17/2021, 9:33 AM@Serializer
will let you use the auto-generated serializer for external classesephemient
11/17/2021, 9:33 AMephemient
11/17/2021, 9:34 AMholgerbrandl
11/17/2021, 1:34 PMSerializable
Ryan Rolnicki
11/18/2021, 4:43 AM// This could be shorter - I think this uses an older interface which was simplified
class UrlAdapter : JsonAdapter<URL>(){
@ToJson
override fun toJson(writer: JsonWriter, value: URL?) {
value?.let {
writer.value(it.toString())
}
}
@FromJson
override fun fromJson(reader: JsonReader): URL? {
return if (reader.peek() != JsonReader.Token.NULL) {
return URL(reader.nextString())
} else {
reader.nextNull<Any>()
null
}
}
}
class LocalTimeAdapter {
@ToJson
fun toJson(value: LocalTime): String {
return value.toString()
}
@FromJson
fun fromJson(value: String): LocalTime {
return LocalTime.parse(value)
}
}
val moshi = Moshi
.Builder()
.add(UrlAdapter())
.add(LocalTimeAdapter())
.addLast(KotlinJsonAdapterFactory())
.build()
val adapter = moshi.adapter(PlacesSearchResponse::class.java)
val json = adapter.toJson(resp)
Not overhead free, but I think getting this setup in kotlinx serialization would require a lot more preparation for custom type serializers. If this is doable in kotlinx serialization with a similar number of steps, I'd love to see it 🙂ephemient
11/19/2021, 12:48 AMyou could probably use a similar approach to generate your own custom serializers via kapt thoughdone, with a test to prove that it works. not too hard, just needed a few special cases for the few model classes that don't follow the same structure as all the others (edited)