微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – EclipseLink MOXy JSON序列化

我有一个示例类:
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","preferredFood": "food-1"
        },{
            "name": "cat-1","favoritetoy": "toy-1"
        },{
            "name": "dog-1","leashColor": "black"
        }
}

谢谢,
Behzad

解决方法

问题#1

Why is it using array indicators “[]”,while bird,cat,and dog are
not arrays?

获取此JSON表示,您已将模型映射到@XmlElementRef注释,该注释告诉JAXB使用@XmlRootElement注释的值作为继承指标.使用MOXy的JSON绑定这些成为关键.我们使这些键JSON值的值由于键不允许重复.

动物园

在你的模型中,你的动物字段/属性上有@XmlElementRef注释.

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;

}

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

Second,is there a way to get rid of “bird”,“cat”,and “dog”?

您将需要某种继承指标来表示各种子类.

选项#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,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","preferredFood" : "food-1"
   },{
      "type" : "cat","name" : "cat-1","favoritetoy" : "toy-1"
   },{
      "type" : "dog","name" : "dog-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,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);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


应用场景 C端用户提交工单、工单创建完成之后、会发布一条工单创建完成的消息事件(异步消息)、MQ消费者收到消息之后、会通知各处理器处理该消息、各处理器处理完后都会发布一条将该工单写入搜索引擎的消息、最终该工单出现在搜索引擎、被工单处理人检索和处理。 事故异常体现 1、异常体现 从工单的流转记录发现、
线程类,设置有一个公共资源 package cn.org.chris.concurrent; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * @Descrip
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量
Java 9中JShell中的不同启动脚本是什么?
使用Java的位填充错误检测技术
java中string是什么
如何使用Java中的JSON-lib API将Map转换为JSON对象?