栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

EclipseLink MOXy JSON序列化

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

EclipseLink MOXy JSON序列化

问题#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;@XmlRootElementpublic class Bird extends Animal {    private String wingSpan;    private String preferredFood;}

input.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"   } ]}

想要查询更多的信息

  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html

问题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;}

input.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"   } ]}

想要查询更多的信息

  • http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html

选项#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;}

由于MOXy处理

@XmlElement
@XmlAttribute
批注的方式,您想要使可用的任何数据
ClassExtractor
都需要使用批注
@XmlAttribute

import javax.xml.bind.annotation.XmlAttribute;public class Bird extends Animal {    @XmlAttribute    private String wingSpan;    @XmlAttribute    private String preferredFood;}

input.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"   } ]}

想要查询更多的信息

  • http://blog.bdoughan.com/2012/02/jaxb-and-inheritance-eclipselink-moxy.html

演示代码

以下演示代码可与上述两个映射一起使用。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/401782.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号