Zyle Moore
05/11/2025, 3:02 AMAny
and getting an error that no serializer for Any
was found. I understand why, I'm just not quite sure how to get what I want with that being true. By default, something like
@Serializable data class Point(val x: Int, val y: Int)
will eventually make a call to beginStructure
, two calls to encodeInt
, and a call to endStructure
. If I replace Int
with any other primitive type, like Short
, Double
, Float
, Long
, the two calls in the middle will be the appropriate method for that type, encodeShort
, encodeFloat
, etc. I'm looking for something that behaves exactly the same way, but the encode method it calls is encodeSerializableValue
instead of encodeInt
. Or maybe even encodeInline
. This example though doesn't use generics, so perhaps it's easier for the generator to map confidently.
In my generic wrapper example, I have a
@Serializable @JvmInline value class FieldValue<T : Any>(val value: T) : FieldToken
The idea being a very thin wrapper around some serializable value, that I can treat as a subtype of FieldToken
. I have a containing class
@Serializable data class Record(val header: RecordHeader, val fields: List<FieldValue<Any>>)
which is where my trouble comes in. I want to be able to serialize, roughly, something that looks like
[ "TES4", [ flags, formId, timestamp, versionControl, recordVersion, unknown ] ],// RecordHeader
[ "HEDR", [ 1.7, 0, 0 ] ],// // FieldValue<(Float, Short, Short)>
[ "CNAM", "Zymus" ],// FieldValue<NullTerminatedString>
[ "SNAM", "TES4 JSON Tuple Example" ],// FieldValue<NullTerminatedString>
[ "MAST", "Skyrim.esm" ],// FieldValue<NullterminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "MAST", "Update.esm" ],// FieldValue<NullTerminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "MAST", "Hearthfires.esm" ],// FieldValue<NullTerminatedString>
[ "DATA", 0 ],// FieldValue<Long>
[ "ONAM", [ 0 ] ],// FieldValue<List<Int>>
[ "INTV", 0 ],// FieldValue<Int>
[ "INCC", 0 ]// FieldValue<Short>
Zyle Moore
05/11/2025, 3:28 AM.toString
implementations in Kotlin; every primitive type supported returns effectively a JsonUnquotedLiteral
with the default .toString
implementation, and it's correct. The value stops being Any
in the constructor, at which point the body: String
(or content
) becomes the source of truth. Writing the value also doesn't seem to actually rely on the provided type, but which type it doesn't freak out at while trying to write it as every type possible.CLOVIS
05/12/2025, 12:30 PM