我有一个 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();
}
上面的PreConditions类是由 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 为何如此疯狂的任何线索?