1

我有一个 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();
    }

上面的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 为何如此疯狂的任何线索?

4

1 回答 1

1

由于 的名义类型acceptTermsAndConditionsjava.lang.Object,因此很难知道到底发生了什么。序列化器将根据序列化的运行时类型进行选择——我猜它会是 DOM Document。在反序列化时,它不会很好地工作,只会变成java.util.Map.

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

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

于 2013-04-26T17:53:41.973 回答