Ktor ContentNegotiation with multiple serializers configured serializes everything to whichever configuration is declared first.

So this is my ContentNegotiation configuration:

fun Application.configureSerialization() { 

install(ContentNegotiation) { serialization(ContentType.Application.Json, Json) serialization(ContentType.Application.Xml, XML { xmlDeclMode = XmlDeclMode.Minimal }) } }

The problem is, I have some endpoints that I want to send XML in the response, and some that I want to send JSON. I have some data classes that are configured to serialize XML using various annotations like @XmlSerialName and @XmlElement, but when I return the data classes as responses in call.respond(), the get serialized to JSON. If I declare my XML serializer first, however, it correctly gets serialized to XML but all my JSON endpoints try to get serialized to XML as well.

Is there a way to ensure that my data classes that are configured to serialize XML will be serialzed to XML, and my data classes with the standard @Serializable on them get serialized to JSON?

Here is an example of one of my data classes that is configured to serialize to XML:

@Serializable @XmlSerialName("Response") data class Response( @XmlSerialName("Name") @XmlAttribute(true) val name: String, @XmlSerialName("xmlns") val idAttribute: String ) 

and my data classes I want serialized to JSON just have the standard @Serializable on top of them.

Thanks in advance!

submitted by /u/LobsterCoordinates
[link] [comments]