问题 #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
注释来完成此操作。
Zoo
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
ClassExtractor(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;
}
Bird
由于 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);
}
}
ClassExtractor
内部访问Zoo
中的字段)?我已经在http://stackoverflow.com/questions/26253383/access-parent-json-element-in-eclipselink-moxy-s-classextractor上发布了这个问题。谢谢。 - Ken Chan