dave08
02/18/2024, 4:27 PMAny
can either contain a String
a Set<String>
or a Map<String, Set<String>>
?Adam S
02/18/2024, 5:26 PM@Serializable
value class implementations for the String, SetString, and Map<> subtypes.
@Serializable
sealed interface CustomValue
@Serializable
value class StringValue(val value: String): CustomValue, CharSequence by value
@Serializable
value class StringSetValue(val value: Set<String>): CustomValue, Set<String> by value
@Serializable
value class MapStringToSetStringValue(val value: Map<String, Set<String>>): CustomValue, Map<String, Set<String>> by value
Then instead of Map<String, Any>
, use Map<String, CustomValue>
dave08
02/19/2024, 10:18 AMAdam S
02/19/2024, 10:26 AMAdam S
02/19/2024, 10:27 AMdave08
02/19/2024, 10:28 AMAdam S
02/19/2024, 10:28 AMAdam S
02/19/2024, 10:30 AMdave08
02/19/2024, 10:34 AMdave08
02/19/2024, 11:21 AMdave08
02/19/2024, 11:30 AMobject RestrictionSerializer : JsonContentPolymorphicSerializer<Any>(Any::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<Any> {
return when (element) {
is JsonPrimitive -> String.serializer()
is JsonArray -> ListSerializer(String.serializer())
is JsonObject -> MapSerializer(String.serializer(), ListSerializer(String.serializer()))
}
}
}
but how to I use it to serialize/deserialize this:
data class Foo(private val restrictions: Map<String, Any>) // where the Any is supposed to use that serializer?
@Adam Sdave08
02/19/2024, 11:34 AM@Serializable(with = RestrictionsSerializer::class)
...dave08
02/19/2024, 4:07 PMAdam S
02/19/2024, 4:18 PMAdam S
02/19/2024, 4:19 PMwondering if using a regular customserializer might be more efficient?I'm pretty sure that KxS will dynamically convert the content to JsonElements on demand, in a performance friendly way. But I'm not 100% sure. But it's usually only worth worrying about performance if you've written performance tests :)