Kotlin / JacksonXML - 反序列化 XML 偶尔 属性 (来自 Qt TS 文件)
Kotlin / JacksonXML - Deserialize XML occasional property (from Qt TS files)
我有以下 XML 结构(基于 Qt TS 文件格式)来反序列化:
<context>
<name>General</name>
<message>
<source>One</source>
<translation>One</translation>
</message>
<message>
<source>Two</source>
<translation>Two</translation>
</message>
<message numerus="yes">
<source>Thing</source>
<translation>
<numerusform>Thing</numerusform>
<numerusform>Things</numerusform>
</translation>
</message>
</context>
用于解析的 Kotlin 代码:
data class Context(@JacksonXmlProperty(localName = "name")
val name: String = "",
private val mMap: MutableList<Message> = mutableListOf()) {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "message")
var messages = mMap
set(value) {
field.addAll(value); return
}
}
data class Message(@JacksonXmlProperty(localName = "numerus", isAttribute = true)
val numerus: String? = null,
@JacksonXmlProperty(localName = "source")
val source: String? = null,
@JacksonXmlProperty(localName = "comment")
val comment: String? = null,
@JacksonXmlProperty(localName = "translation")
val translation: Translation? = null)
data class Translation(@JacksonXmlProperty(localName = "type", isAttribute = true)
val type: String? = null,
@JacksonXmlProperty(localName = "innerText")
@JacksonXmlText
val text: String? = null,
private val numerusMap: MutableList<NumerusForm> = mutableListOf()) {
// custom setter because otherwise the list isn't assembled correctly
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "numerusform")
var numerusForm = numerusMap
set(value) {
field.addAll(value); return
}
}
data class NumerusForm(@JacksonXmlProperty(localName = "innerText")
@JacksonXmlText
val text: String? = null
)
如果只有
,此代码有效
<translation
<numerusform>Thing</numerusform>
</translation>
XML 中可用的属性。只要我有 <translation>One</translation>
,它就会崩溃。
关于如何解析这个 XML 结构中偶尔出现的 属性 而不会在文件的其余部分崩溃的想法?
通过从 Jackson 创建我自己的解串器 class 找到了解决方案。
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
class MyOwnDeserizalizer : StdDeserializer<TSTransformerService.Translation>(Translation::class.java) {
override fun deserialize(jP: JsonParser, ctxt: DeserializationContext): Translation {
//Gets the Translation element
val rootNode: JsonNode = jP.getCodec().readTree<JsonNode>(jP)
// Get and set the value from the rootnode
return Translation(type, text, numerus)
}
}
一些有帮助的来源:
- jackson custom deserializer only getting last value in list xml
- Jackson Kotlin - Deserialize JsonNode
我有以下 XML 结构(基于 Qt TS 文件格式)来反序列化:
<context>
<name>General</name>
<message>
<source>One</source>
<translation>One</translation>
</message>
<message>
<source>Two</source>
<translation>Two</translation>
</message>
<message numerus="yes">
<source>Thing</source>
<translation>
<numerusform>Thing</numerusform>
<numerusform>Things</numerusform>
</translation>
</message>
</context>
用于解析的 Kotlin 代码:
data class Context(@JacksonXmlProperty(localName = "name")
val name: String = "",
private val mMap: MutableList<Message> = mutableListOf()) {
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "message")
var messages = mMap
set(value) {
field.addAll(value); return
}
}
data class Message(@JacksonXmlProperty(localName = "numerus", isAttribute = true)
val numerus: String? = null,
@JacksonXmlProperty(localName = "source")
val source: String? = null,
@JacksonXmlProperty(localName = "comment")
val comment: String? = null,
@JacksonXmlProperty(localName = "translation")
val translation: Translation? = null)
data class Translation(@JacksonXmlProperty(localName = "type", isAttribute = true)
val type: String? = null,
@JacksonXmlProperty(localName = "innerText")
@JacksonXmlText
val text: String? = null,
private val numerusMap: MutableList<NumerusForm> = mutableListOf()) {
// custom setter because otherwise the list isn't assembled correctly
@JacksonXmlElementWrapper(useWrapping = false)
@JacksonXmlProperty(localName = "numerusform")
var numerusForm = numerusMap
set(value) {
field.addAll(value); return
}
}
data class NumerusForm(@JacksonXmlProperty(localName = "innerText")
@JacksonXmlText
val text: String? = null
)
如果只有
,此代码有效<translation
<numerusform>Thing</numerusform>
</translation>
XML 中可用的属性。只要我有 <translation>One</translation>
,它就会崩溃。
关于如何解析这个 XML 结构中偶尔出现的 属性 而不会在文件的其余部分崩溃的想法?
通过从 Jackson 创建我自己的解串器 class 找到了解决方案。
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
class MyOwnDeserizalizer : StdDeserializer<TSTransformerService.Translation>(Translation::class.java) {
override fun deserialize(jP: JsonParser, ctxt: DeserializationContext): Translation {
//Gets the Translation element
val rootNode: JsonNode = jP.getCodec().readTree<JsonNode>(jP)
// Get and set the value from the rootnode
return Translation(type, text, numerus)
}
}
一些有帮助的来源:
- jackson custom deserializer only getting last value in list xml
- Jackson Kotlin - Deserialize JsonNode