Is there some type to represent anything that's Se...
# serialization
z
Is there some type to represent anything that's Serializable? For context, this isn't about an existing format like Json or Cbor. I'm trying to make a generic wrapper, but keep ending up at making the type
Any
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
Copy code
[ "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>
It looks like the Json serializer might achieve its polymorphism by grace of default
.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.
c
> Is there some type to represent anything that's Serializable? No, there isn't. It's not possible to know which types are serializable at compile-time because some serializers are registered only at runtime.