Jackson xml 使用自定义布尔格式的(反)序列化
Jackson xml (de)serialization with custom boolean format
我有xml个文件:
<?xml version="1.0" encoding="UTF-8"?>
<ADDRESSOBJECTS>
<OBJECT ID="1802267" NAME="SomeName" ISACTIVE="1" />
</ADDRESSOBJECTS>
和 kotlin 中对应的 类:
@JacksonXmlRootElement(localName = "ADDRESSOBJECTS")
class AddressingObjectCollection {
@JacksonXmlProperty(localName = "OBJECT")
@JacksonXmlElementWrapper(useWrapping = false)
open lateinit var objects: List<AddressingObject>
}
和
class AddressingObject : Serializable {
@JacksonXmlProperty(isAttribute = true, localName = "ID")
open var id: Long = 0
@JacksonXmlProperty(isAttribute = true, localName = "NAME")
open lateinit var name: String
@JacksonXmlProperty(isAttribute = true, localName = "ISACTIVE")
open var isActive: Boolean = false
}
当我尝试反序列化时出现错误:
val deserialized = mapper.readValue(File(file).readText(), AddressingObjectCollection::class.java)
错误:
Cannot deserialize value of type `boolean` from String "1": only "true"/"True"/"TRUE" or "false"/"False"/"FALSE" recognized
如何告诉杰克逊正确地(反)序列化这种格式?
为此,我使用 Json 属性:
@JsonProperty("ISACTIVE")
@JacksonXmlProperty(isAttribute = true, localName = "ISACTIVE")
@JsonDeserialize(using = CustomBooleanDeserializer::class)
open var isActive: Boolean = false
和 CustomBooleanDeserializer:
class CustomBooleanDeserializer : JsonDeserializer<Boolean>() {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Boolean {
if (p?.currentTokenId() == JsonTokenId.ID_STRING){
var text = p.text
if (text == "1") return true
}
return false;
}
}
对我有用。
我有xml个文件:
<?xml version="1.0" encoding="UTF-8"?>
<ADDRESSOBJECTS>
<OBJECT ID="1802267" NAME="SomeName" ISACTIVE="1" />
</ADDRESSOBJECTS>
和 kotlin 中对应的 类:
@JacksonXmlRootElement(localName = "ADDRESSOBJECTS")
class AddressingObjectCollection {
@JacksonXmlProperty(localName = "OBJECT")
@JacksonXmlElementWrapper(useWrapping = false)
open lateinit var objects: List<AddressingObject>
}
和
class AddressingObject : Serializable {
@JacksonXmlProperty(isAttribute = true, localName = "ID")
open var id: Long = 0
@JacksonXmlProperty(isAttribute = true, localName = "NAME")
open lateinit var name: String
@JacksonXmlProperty(isAttribute = true, localName = "ISACTIVE")
open var isActive: Boolean = false
}
当我尝试反序列化时出现错误:
val deserialized = mapper.readValue(File(file).readText(), AddressingObjectCollection::class.java)
错误:
Cannot deserialize value of type `boolean` from String "1": only "true"/"True"/"TRUE" or "false"/"False"/"FALSE" recognized
如何告诉杰克逊正确地(反)序列化这种格式?
为此,我使用 Json 属性:
@JsonProperty("ISACTIVE")
@JacksonXmlProperty(isAttribute = true, localName = "ISACTIVE")
@JsonDeserialize(using = CustomBooleanDeserializer::class)
open var isActive: Boolean = false
和 CustomBooleanDeserializer:
class CustomBooleanDeserializer : JsonDeserializer<Boolean>() {
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): Boolean {
if (p?.currentTokenId() == JsonTokenId.ID_STRING){
var text = p.text
if (text == "1") return true
}
return false;
}
}
对我有用。