繁体   English   中英

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

[英]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仅具有truefalse作为值:

  <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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM