JAXB和一堆子类产生错误的json

时间:2013-10-15 14:58:31

标签: json jaxb moxy

是否可以实现利用多个abstract类的嵌套的类结构的解/编组?
鉴于这样的类结构:

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

@XmlRootElement ed Zoo类有一个动物列表:

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

我认为你明白这个想法......

<?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
       } ]
    }
 }

为什么它在JSON中对Mammal类对象进行子组化?

我正在使用EclipseLink MOXy 2.6进行编组。

1 个答案:

答案 0 :(得分:1)

原始答案

MOXy将键tigerelephant分组,以避免重复这些键。


更新#1

  

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

是的,这是可能的,你只需要以这种方式映射:

<强>动物园

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

}

<强> input.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的外部映射文档来更改animalsZoo字段的映射。

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

<强>演示

在下面的演示代码中,我们在同一个域模型上创建了2个JAXBContext实例。 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
   } ]
}