Override any type with string
type like standard @Stringable
#230
Labels
enhancement
New feature or request
string
type like standard @Stringable
#230
Is your feature request related to a problem? Please describe.
In the standard library we can override any type to a
string
when annotating it with@Stringable
.Describe the solution you'd like
The idea would be to allow defining any field to be string'ified using a similar annotation.
There is still technical limitation: kotlinx-serialization doesn't allow a data class to be serialized as a string (or any other scalar type) as it uses
beginStructure
and other more complex handling. To allow usingencodeString
from a standard data class, the user have to provide a customKSerializer
of the given data class, declare its descriptor as aPrimitiveKind.STRING
and then callencodeString
anddecodeString
according to the desired format.Finally, there will be 2 ways to set something to a string:
AvroScalarString
annotation to change the inferred type to a string only for properties. Note that no logical type would be applied just using the annotation. If the annotated property is not compatible, the generated schema will remain to the inferred one to avoid runtime errors.PrimitiveSerialDescriptor("type name", STRING)
, and where its encode and decode methods handles the string type.PrimitiveSerialDescriptor("type name", STRING).withSchema { /* handle here the different annotations */ }
The text was updated successfully, but these errors were encountered: