-
Notifications
You must be signed in to change notification settings - Fork 43
Cannot decode map #274
Copy link
Copy link
Closed
Labels
indevThe issue is fixed/implemented in the dev branchThe issue is fixed/implemented in the dev branch
Description
I wanted to try @XmlKeyName after seeing the update. I don't thiink I've every tried loading Map with this library.
I was having trouble with my own code, so I copied some from the tests in this library.
This code works:
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.serializer
import nl.adaptivity.xmlutil.ExperimentalXmlUtilApi
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlKeyName
import nl.adaptivity.xmlutil.serialization.XmlSerialName
import org.intellij.lang.annotations.Language
fun main() {
@Language("XML")
val encoded =
"""
<MapContainer>
<MapOuter name="a">
<MapElement name="valueOfA"/>
</MapOuter>
<MapOuter name="b">
<MapElement name="valueOfB"/>
</MapOuter>
</MapContainer>
""".trim()
val o = XML.decodeFromString(serializer<MapContainer>(), encoded)
o.map.entries.forEach { (key, value) ->
println("key=$key,value=$value")
}
}
@Serializable
class MapContainer(
@XmlElement(true)
@XmlSerialName("MapOuter", "", "")
@XmlKeyName("name")
val map: Map<String, MapElement> = mapOf()
)
@Serializable
data class MapElement(val name: String)
Output:
key=a,value=MapElement(name=valueOfA)
key=b,value=MapElement(name=valueOfB)
However, then when I try to simplify it by removing the name property of MapElement, it no longer loads. The error message suggests that the parser might be mixing up the name attribute from MapOuter and MapElement.
Modifed code (expected to work):
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.serializer
import nl.adaptivity.xmlutil.ExperimentalXmlUtilApi
import nl.adaptivity.xmlutil.serialization.XML
import nl.adaptivity.xmlutil.serialization.XmlElement
import nl.adaptivity.xmlutil.serialization.XmlKeyName
import nl.adaptivity.xmlutil.serialization.XmlSerialName
import org.intellij.lang.annotations.Language
fun main() {
@Language("XML")
val encoded =
"""
<MapContainer>
<MapOuter name="a">
<MapElement/>
</MapOuter>
<MapOuter name="b">
<MapElement/>
</MapOuter>
</MapContainer>
""".trim()
val o = XML.decodeFromString(serializer<MapContainer>(), encoded)
o.map.entries.forEach { (key, value) ->
println("key=$key,value=$value")
}
}
@Serializable
class MapContainer(
@XmlElement(true)
@XmlSerialName("MapOuter", "", "")
@XmlKeyName("name")
val map: Map<String, MapElement> = mapOf()
)
@Serializable
class MapElement()
Error:
nl.adaptivity.xmlutil.serialization.UnknownXmlFieldException: Could not find a field for name (MapContainer) MapContainer/MapOuter (Element)
candidates: MapElement (Element) at position 2:12
at nl.adaptivity.xmlutil.serialization.XmlConfig.DEFAULT_UNKNOWN_CHILD_HANDLER$lambda$9(XmlConfig.kt:693)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
indevThe issue is fixed/implemented in the dev branchThe issue is fixed/implemented in the dev branch