简体   繁体   English

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

[英]Jackson serializing JAXB object gives strange results

I have a JAXB object. 我有一个JAXB对象。 When I serialize it, the results are funny! 当我序列化它时,结果很有趣! Like this => 像这样=>

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

Whereas, the source XML just has true and false as the values:- 而源XML仅具有truefalse作为值:

  <formData>
    <preConditions>
      <acceptTermsAndConditions>true</acceptTermsAndConditions>
      <receivePromoEmail>false</receivePromoEmail>
    </preConditions> etc...

My code to generate the JSON is as follows:- 我的生成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();
    }

Where the PreConditions class above is generated by JAXB2 (XJC). 其中前提条件类以上由JAXB2(XJC)生成。 The following is a snippet:- 以下是代码段:-

@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;
        }

    }

Any clues on why the JSON is so crazy? 关于JSON为什么如此疯狂的任何线索?

Since the nominal type for acceptTermsAndConditions is java.lang.Object , it is hard to know what exactly is going on. 由于acceptTermsAndConditions的名义类型是java.lang.Object ,所以很难知道到底发生了什么。 Serializer will be chosen based on runtime type on serialization -- my guess is that it would be DOM Document . 序列化器将基于序列化时的运行时类型进行选择-我的猜测是它将是DOM Document On deserialization it would not work too well, and would just become java.util.Map . 在反序列化时,它不能很好地工作,而只是成为java.util.Map

So you may need to change the schema to generate more specific types: anything with java.lang.Object are likely to cause issues. 因此,您可能需要更改架构以生成更特定的类型:带有java.lang.Object任何内容都可能引起问题。

You may want to see actual Java types that your object has before serialization. 您可能需要查看序列化之前对象具有的实际Java类型。 That should explain where the strange output comes from. 那应该解释奇怪的输出是从哪里来的。 I don't think it can be simple boolean value. 我认为这不是简单的布尔值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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