https://kotlinlang.org logo
#serialization
Title
# serialization
r

Richard

01/24/2023, 3:42 PM
Is there a recommended approach to serialize/deserialize inline value classes that are not based of a primitive? Example:
Copy code
@JvmInline
value class UserId(value: UUID)
It serializes to a nice UUID string, but turning that back into a class seems to involve substantial boiler plate using Jackson or kotlin serialization libs
e

Emil Kantis

01/24/2023, 4:00 PM
There's a UUID serializer in https://github.com/Kantis/ks3 if you want to give it a try. I notice that it's missing a typealias but you could use it either by adding
Copy code
typealias UuidAsString = @Serializable(with = UuidSerializer::class) UUID

@JvmInline
value class UserId(value: UuidAsString)
(the typealias should be added to the lib, at which point you'd just need to import it instead) Or just specifying it directly: (IIRC)
Copy code
@JvmInline
value class UserId(@Serializable(with = UuidSerializer::class) value: UUID)
r

Richard

01/24/2023, 5:50 PM
thanks i will take a look
4 Views