I need some help. I am trying to set up a custom a...
# serialization
t
I need some help. I am trying to set up a custom annotation that will allow me to set a data format value for the my external parser to use. Something like seen here, but with a customizable format (https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/custom_serializers.md#external-serializers-for-library-classes). My goal data declaration would be something like:
Copy code
class MyObject {
   @Format("mm/dd/yyyy")
   @Serializable(with = MyDateSerializer::class)
  val poorlyFormattedDate: Date
}
Any way to get the Format data from within the MyDateSerializer (without writing a whole custom parsing class for every model in my app/library? Thank you!
I would even accept:
Copy code
class MyObject {
   @Serializable(with = MyDateSerializer::class, format = "mm/dd/yyy")
  val poorlyFormattedDate: Date
}
if I knew how…
l
An alternative is to make subclasses for the formats you need, passing the format to the constructor of parent class.
t
Interesting idea. Though I like to minimize classes, so I will stick with what I have now - parsing the date as a String, and then adding a getter to do the conversion when needed. Thanks!