Jackson序列化JAXB对象会产生奇怪的结果

Jackson序列化JAXB对象会产生奇怪的结果,jaxb,jackson,Jaxb,Jackson,我有一个JAXB对象。当我序列化它时,结果很有趣!像这样=> {"formData":{ "preConditions":{ "acceptTermsAndConditions":"<?xml version=\"1.0\" encoding=\"UTF-16\"?>\n<acceptTermsAndConditions>true</acceptTermsAndConditions>", "receivePromoEmail":"<?xm

我有一个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...
其中,上面的Premissions类由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文档。 在反序列化时,它不会工作得太好,只会变成java.util.Map

因此,您可能需要更改模式以生成更具体的类型:使用java.lang.Object的任何内容都可能导致问题


您可能希望在序列化之前查看对象的实际Java类型。这应该可以解释奇怪的输出从何而来。我不认为它可以是简单的布尔值。

这还不够信息:您可以添加应用程序类型的定义吗?我猜acceptTermsAndConditions和receivePromoEmail的类型是DOM文档之类的-这些不是POJO类型,只能序列化为原始字符串。根据请求使用有关应用程序类型的信息进行更新。啊。现在,问题出在Application.FormData中,奇怪的JSON就在这里。你能再加一点吗?我仍然认为类型被声明为DOM元素或其他特定于XML的类型,除了按要求转储XML.Added Application.FormData之外,无法以其他方式处理这些类型。您看看您的数据,了解我为什么要求这些类型如何。。。答案就在这里,匹配嵌入XML.StaxMan的字段-感谢您的见解。不幸的是,我无法更改模式。我查看了运行时类型,它是org.apache.xerces.dom.ElementNSImpl,因此我通过为这种类型编写自定义序列化程序解决了我的问题。我有点惊讶,Jackson实现没有包含这个序列化程序,因为这是XJC在模式中没有指定类型时生成的默认类型。Jackson不是JAXB实现,所以它能做的最好的事情是将它序列化为字符串,它确实包含DOM元素的序列化程序。基本上,DOM元素只是另一种外来数据类型。如果您想将自定义序列化程序转换为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();
    }
@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;
        }

    }