杰克逊序列化JAXB对象给出了奇怪的结果

时间:2013-04-16 12:51:58

标签: jaxb jackson

我有一个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如此疯狂的任何线索?

1 个答案:

答案 0 :(得分:0)

由于acceptTermsAndConditions的名义类型为java.lang.Object,因此很难知道到底发生了什么。将根据序列化的运行时类型选择Serializer - 我的猜测是它将是DOM Document。 关于反序列化,它不会工作得太好,只会变成java.util.Map

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

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