I'd like to sort elements of collections by different properties on serialization, Is there some built-in way / annotation argument I'm missing to do that, or would I have to implement e.g. a
JsonTransformingSerializer
that takes a
Comparator
?
Sebastian Schuberth
05/26/2023, 10:03 AM
Basically, I'm looking for an equivalent of Jackon's
StdConverter
.
Sebastian Schuberth
05/26/2023, 10:16 AM
Something like this does not seem to work, the set remains unsorted:
Copy code
@Serializable(SetSortedSerializer::class)
val licenses: Set<String>
with
Copy code
object SetSortedSerializer : JsonTransformingSerializer<Set<String>>(SetSerializer(String.serializer())) {
override fun transformSerialize(element: JsonElement): JsonElement {
if (element is JsonArray) {
return element.sortedBy { it.jsonPrimitive.content } as JsonArray
}
return element
}
}
Sebastian Schuberth
05/26/2023, 10:39 AM
Very weird, the problem seems to be something else, as my serializer does not seem to be called at all...
Sebastian Schuberth
05/26/2023, 10:57 AM
Nevermind, I was still using the wrong object mapper 😾