1

是否有可能实现利用多个abstract类嵌套的类结构的取消/编组?
给定这样的类结构:

@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {}
public abstract class Mammal extends Animal {}
public class Tiger extends Mammal {}
public class Elephant extends Mammal {}

@XmlRootElemented类Zoo有一个动物列表:

@XmlElementWrapper(name = "animals")
@XmlElements({
    @XmlElement(name = "elephant", type = Elephant.class),
    @XmlElement(name = "tiger", type = Tiger.class)
})
private List<Animal> animals;

我想你明白了......这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<zoo>
   <animals>
      <tiger>
         <name>Richard</name>
         <furry>true</furry>
      </tiger>
      <elephant>
         <name>Otis</name>
         <furry>false</furry>
      </elephant>
      <tiger>
         <name>Kirk</name>
         <furry>true</furry>
      </tiger>
   </animals>
</zoo>

这看起来不错,很酷。
现在 JSON ...

 {
    "animals" : {
       "tiger" : [ {
          "name" : "Richard",
          "furry" : true
       }, {
          "name" : "Kirk",
          "furry" : true
       } ],
       "elephant" : [ {
          "name" : "Otis",
          "furry" : false
       } ]
    }
 }

为什么它对MammalJSON 中的类对象进行分组?

我使用 EclipseLink MOXy 2.6 进行编组。

4

1 回答 1

1

原始答案

MOXy 对键进行分组tigerelephant避免重复它们。


更新#1

所以不可能得到像 { 'animals': [ {'@type': 'tiger'}, {'@type':elephant'}, ... ] } 这样的 JSON?

是的,这是可能的,你只需要这样映射它:

动物园

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Zoo {

    private List<Animal> animals;

}

动物

import javax.xml.bind.annotation.*;

@XmlSeeAlso({Elephant.class, Tiger.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {

}

演示

import java.util.*;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum19384491/input.json");
        Zoo zoo = unmarshaller.unmarshal(json, Zoo.class).getValue();

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(zoo, System.out);
    }

}

输入.json/输出

{
   "animals" : [ {
      "@type" : "tiger"
   }, {
      "@type" : "elephant"
   }, {
      "@type" : "tiger"
   } ]
}

更新#2

如果您想保留当前的 ​​XML 表示并仅更改 JSON 表示,您可以使用 MOXy 的外部映射文档扩展(参见: http ://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html )

映射文件 (oxm.xml)

我们将使用 MOXy 的外部映射文档来更改类animals上字段的映射Zoo

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum19384491">
    <java-types>
        <java-type name="Zoo">
            <java-attributes>
                <xml-element java-attribute="animals"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

演示

JAXBContext在下面的演示代码中,我们在同一个域模型上创建了 2 个实例。JSON 的一个利用外部映射文档来自定义映射。 input.xml是您问题中的 XML 文档。

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

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

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

        Unmarshaller unmarshaller = xmlJC.createUnmarshaller();
        File xml = new File("src/forum19384491/input.xml");
        Zoo zoo = (Zoo) unmarshaller.unmarshal(xml);

        Map<String, Object> properties = new HashMap<String, Object>(4);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19384491/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
        JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);

        Marshaller marshaller = jsonJC.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(zoo, System.out);
    }

}

输出

下面是运行演示代码的输出。

{
   "animals" : [ {
      "@type" : "tiger",
      "name" : "Richard",
      "furry" : true
   }, {
      "@type" : "elephant",
      "name" : "Otis",
      "furry" : false
   }, {
      "@type" : "tiger",
      "name" : "Kirk",
      "furry" : true
   } ]
}
于 2013-10-15T17:01:19.090 回答