重复的JSON容器属性

重复的JSON容器属性,json,jersey,jackson,Json,Jersey,Jackson,我有一些jaxb对象建模元数据结构,其中容器对象具有值,可以是另一个容器对象,也可以只是一个简单的对象(例如字符串) XML看起来不错,但JSON最终具有重复的“容器”属性 <container> <value> <container> <value> <type>String</type>

我有一些jaxb对象建模元数据结构,其中容器对象具有值,可以是另一个容器对象,也可以只是一个简单的对象(例如字符串)

XML看起来不错,但JSON最终具有重复的“容器”属性

        <container>
          <value>
            <container>
              <value>
                <type>String</type>
              </value>
            </container>
          </value>
        </container>

            "container": {
              "value": {
                "container": {
                  "container": {
                    "value": {
                      "type": "STRING"
                    }
                  }
                }
              }
            }

一串
“容器”:{
“价值”:{
“容器”:{
“容器”:{
“价值”:{
“类型”:“字符串”
}
}
}
}
}

你知道为什么会有这种差异吗?

那是因为
容器

UPD。看


UPD2。请参见和

注意:我是专家组的负责人和成员

由于您的模型中有MOXy的
@XmlInverseReference
注释,您可能会对如何将其应用于JSON案例感兴趣

input.xml

从您的问题中我可以看出,如果在
容器
之间存在双向关系,那么XML表示应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<container>
    <value>
       <type>String</type>
    </value>
</container>

package forum10706457;

import javax.xml.bind.annotation.*;

@XmlEnum
public enum SimpleType
{
        @XmlEnumValue("String")STRING,
        @XmlEnumValue("Boolean")BOOLEAN,
}
SimpleType

package forum10706457;

import javax.xml.bind.annotation.*;

@XmlEnum
public enum SimpleType
{
        @XmlEnumValue("String")STRING,
        @XmlEnumValue("Boolean")BOOLEAN,
}
jaxb.properties

@XmlInverseReference
注释是一个MOXy扩展,因此您需要在与域模型相同的包中添加一个名为jaxb.properties的文件,并使用以下条目将MOXy指定为jaxb提供程序

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示

下面的代码演示了如何加载XML文档,然后将生成的对象封送到JSON。检查是否填充了双向关系

package forum10706457;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Container.class);

        File xml = new File("src/forum10706457/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Container container = (Container) unmarshaller.unmarshal(xml);

        System.out.println(container == container.value.container);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(container, System.out);
    }

}
输出

下面是运行演示代码的输出。注意
@XmlEnumValue(“String”)String
是如何在JSON表示中使用的

true
{
   "container" : {
      "value" : {
         "type" : "String"
      }
   }
}
了解更多信息


我更新了我的示例w/@XmlInverseReference,但它似乎没有影响json。
package forum10706457;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Container.class);

        File xml = new File("src/forum10706457/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Container container = (Container) unmarshaller.unmarshal(xml);

        System.out.println(container == container.value.container);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(container, System.out);
    }

}
true
{
   "container" : {
      "value" : {
         "type" : "String"
      }
   }
}