hfhbd
09/21/2023, 11:35 AM<?xml version="1.0" encoding="UTF-8" ?>
<documentation>
<foo>A</foo>
bar
</documentation>
How do I declare the class to get bar
?pdvrieze
09/21/2023, 11:39 AMpdvrieze
09/21/2023, 11:42 AMCompactFragment
), but I don't think that an inline value class to the string works (it might, I don't remember testing/implementing that - the challenge is that this needs a special case in deserialization)hfhbd
09/21/2023, 12:14 PM@Serializable
data class Documentation(
@XmlValue
val elements: List<@Polymorphic Any>,
) {
companion object {
fun module() = SerializersModule {
polymorphic(Any::class, String::class, String.serializer())
polymorphic(Any::class, Foo::class, Foo.serializer())
}
}
}
@Serializable
@XmlSerialName("foo")
private data class Foo(
val a: String
)
fun main() {
val x = XML(serializersModule = Test.module()).decodeFromString(
Documentation.serializer(), myFile
)
println(x)
}
What do I miss?pdvrieze
09/21/2023, 12:41 PMDocumentation
not Test
- this could be a copy-paste-edit problem though)pdvrieze
09/21/2023, 12:42 PMhfhbd
09/25/2023, 3:50 PMautoPolymorphic = true
, but not with recommended()
?
fun main() {
//language=xml
val xml = """
<?xml version="1.0" encoding="UTF-8" ?>
<MixedValueContainer>
foo
<address houseNumber="10" street="Downing Street" city="London"/>
bar
</MixedValueContainer>
""".trimIndent()
val x = XML(SerializersModule {
include(MixedValueContainer.serializerModule())
}) {
autoPolymorphic = true
// recommended()
}.decodeFromString(
MixedValueContainer.serializer(), xml
)
println(x)
}
@Serializable
@XmlSerialName("address")
data class Address(
val houseNumber: String,
val street: String,
val city: String,
)
@Serializable
data class MixedValueContainer(
@XmlValue
val data: List<@Polymorphic Any>
) {
companion object {
fun serializerModule() = SerializersModule {
polymorphic(Any::class, String::class, String.serializer())
polymorphic(Any::class, Address::class, Address.serializer())
}
}
}
pdvrieze
09/25/2023, 7:53 PM@XmlValue
for some types (primitives could work)