[英]Jackson serializing JAXB object gives strange results
我有一個JAXB對象。 當我序列化它時,結果很有趣! 像這樣=>
{"formData":{
"preConditions":{
"acceptTermsAndConditions":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<acceptTermsAndConditions>true</acceptTermsAndConditions>",
"receivePromoEmail":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<receivePromoEmail>false</receivePromoEmail>"
}, etc...
而源XML僅具有true和false作為值:
<formData>
<preConditions>
<acceptTermsAndConditions>true</acceptTermsAndConditions>
<receivePromoEmail>false</receivePromoEmail>
</preConditions> etc...
我的生成JSON的代碼如下:-
Application application = (Application) JAXBUtil.getXMLAsApplication();
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
// make deserializer use JAXB annotations (only)
mapper.getDeserializationConfig().with(introspector);
// make serializer use JAXB annotations (only)
mapper.getSerializationConfig().with(introspector);
try {
mapper.writeValue(new File("application.json"), application);
} catch (IOException e) {
e.printStackTrace();
}
其中前提條件類以上由JAXB2(XJC)生成。 以下是代碼段:-
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"acceptTermsAndConditions",
"receivePromoEmail"
})
public static class PreConditions {
@XmlElement(required = true)
protected Object acceptTermsAndConditions;
@XmlElement(required = true)
protected Object receivePromoEmail;
/**
* Gets the value of the acceptTermsAndConditions property.
*
* @return
* possible object is
* {@link Object }
*
*/
public Object getAcceptTermsAndConditions() {
return acceptTermsAndConditions;
}
/**
* Sets the value of the acceptTermsAndConditions property.
*
* @param value
* allowed object is
* {@link Object }
*
*/
public void setAcceptTermsAndConditions(Object value) {
this.acceptTermsAndConditions = value;
}
/**
* Gets the value of the receivePromoEmail property.
*
* @return
* possible object is
* {@link Object }
*
*/
public Object getReceivePromoEmail() {
return receivePromoEmail;
}
/**
* Sets the value of the receivePromoEmail property.
*
* @param value
* allowed object is
* {@link Object }
*
*/
public void setReceivePromoEmail(Object value) {
this.receivePromoEmail = value;
}
}
關於JSON為什么如此瘋狂的任何線索?
由於acceptTermsAndConditions
的名義類型是java.lang.Object
,所以很難知道到底發生了什么。 序列化器將基於序列化時的運行時類型進行選擇-我的猜測是它將是DOM Document
。 在反序列化時,它不能很好地工作,而只是成為java.util.Map
。
因此,您可能需要更改架構以生成更特定的類型:帶有java.lang.Object
任何內容都可能引起問題。
您可能需要查看序列化之前對象具有的實際Java類型。 那應該解釋奇怪的輸出是從哪里來的。 我認為這不是簡單的布爾值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.