11

我有一个示例类:

class Zoo {
    public Collection<? extends Animal> animals;
}

当用 MOXy 序列化时,我得到:

{
    "bird": [
        {
            "name": "bird-1",
            "wingSpan": "6 feets",
            "preferredFood": "food-1"
        }
    ],
    "cat": [
        {
            "name": "cat-1",
            "favoriteToy": "toy-1"
        }
    ],
    "dog": [
        {
            "name": "dog-1",
            "breed": "bread-1",
            "leashColor": "black"
        }
    ]
}

为什么它使用数组指示符“[]”,而鸟、猫和狗不是数组?二、有没有办法去掉“鸟”、“猫”、“狗”?

换句话说,我试图达到:

{
        {
            "name": "bird-1",
            "wingSpan": "6 feets",
            "preferredFood": "food-1"
        }
    ,
        {
            "name": "cat-1",
            "favoriteToy": "toy-1"
        }
    ,
        {
            "name": "dog-1",
            "breed": "bread-1",
            "leashColor": "black"
        }
}

谢谢,贝扎德

4

1 回答 1

10

问题 #1

为什么它使用数组指示符“[]”,而鸟、猫和狗不是数组?

要获得此 JSON 表示,您已将模型与@XmlElementRef注释映射,该注释告诉 JAXB 使用@XmlRootElement注释的值作为继承指示符。使用 MOXy 的 JSON 绑定,这些成为键。我们将这些键的值设为 JSON 数组,因为键不允许重复。

动物园

在您的模型中,您的字段/属性@XmlElementRef上有注释。animals

import java.util.Collection;
import javax.xml.bind.annotation.XmlElementRef;

class Zoo {
    @XmlElementRef
    public Collection<? extends Animal> animals;
}

动物

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Bird.class, Cat.class, Dog.class})
public abstract class Animal {

    private String name;

}

在您的每个子类上,您都有一个@XmlRootElement注释。

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Bird extends Animal {

    private String wingSpan;
    private String preferredFood;

}

输入.json/输出

{
   "bird" : [ {
      "name" : "bird-1",
      "wingSpan" : "6 feets",
      "preferredFood" : "food-1"
   } ],
   "cat" : [ {
      "name" : "cat-1",
      "favoriteToy" : "toy-1"
   } ],
   "dog" : [ {
      "name" : "dog-1",
      "breed" : "bread-1",
      "leashColor" : "black"
   } ]
}

了解更多信息


问题2

二、有没有办法去掉“鸟”、“猫”、“狗”?

您将需要某种继承指示符来表示各种子类。

选项 #1 - @XmlDescriminatorNode/@XmlDescriminatorValue

在这里,我使用 MOXy 的@XmlDescriminatorNode/@XmlDescriminatorValue注释来​​做到这一点。

动物园

import java.util.Collection;

class Zoo {
    public Collection<? extends Animal> animals;
}

动物

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Bird.class, Cat.class, Dog.class})
@XmlDiscriminatorNode("@type")
public abstract class Animal {

    private String name;

}

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("bird")
public class Bird extends Animal {

    private String wingSpan;
    private String preferredFood;

}

输入.json/输出

{
   "animals" : [ {
      "type" : "bird",
      "name" : "bird-1",
      "wingSpan" : "6 feets",
      "preferredFood" : "food-1"
   }, {
      "type" : "cat",
      "name" : "cat-1",
      "favoriteToy" : "toy-1"
   }, {
      "type" : "dog",
      "name" : "dog-1",
      "breed" : "bread-1",
      "leashColor" : "black"
   } ]
}

了解更多信息

选项 #2 -@XmlClassExtractor

类提取器(AnimalExtractor)

您可以编写一些代码,根据 JSON 内容确定适当的子类。

import org.eclipse.persistence.descriptors.ClassExtractor;
import org.eclipse.persistence.sessions.*;

public class AnimalExtractor extends ClassExtractor {

    @Override
    public Class extractClassFromRow(Record record, Session session) {
        if(null != record.get("@wingSpan") || null != record.get("@preferredFood")) {
            return Bird.class;
        } else if(null != record.get("@favoriteToy")) {
            return Cat.class;
        } else {
            return Dog.class;
        }
    }

}

动物

注解@XmlClassExtractor用于指定.ClassExtractor

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlClassExtractor;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Bird.class, Cat.class, Dog.class})
@XmlClassExtractor(AnimalExtractor.class)
public abstract class Animal {

    private String name;

}

由于 MOXy 处理@XmlElement@XmlAttribute注释的方式,您希望提供给 的任何数据ClassExtractor都需要使用@XmlAttribute.

import javax.xml.bind.annotation.XmlAttribute;

public class Bird extends Animal {

    @XmlAttribute
    private String wingSpan;

    @XmlAttribute
    private String preferredFood;

}

输入.json/输出

{
   "animals" : [ {
      "wingSpan" : "6 feets",
      "preferredFood" : "food-1",
      "name" : "bird-1"
   }, {
      "favoriteToy" : "toy-1",
      "name" : "cat-1"
   }, {
      "breed" : "bread-1",
      "leashColor" : "black",
      "name" : "dog-1"
   } ]
}

了解更多信息


演示代码

以下演示代码可用于上述两种映射。

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);
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StreamSource json = new StreamSource("src/forum14210676/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);
    }

}
于 2013-01-08T10:41:59.907 回答