I'd like to sort elements of collections by differ...
# serialization
s
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
?
Basically, I'm looking for an equivalent of Jackon's
StdConverter
.
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
    }
}
Very weird, the problem seems to be something else, as my serializer does not seem to be called at all...
Nevermind, I was still using the wrong object mapper 😾