EclipseLink的莫西JSON序列化(EclipseLink MOXy JSON Serial

2019-07-17 23:31发布

我有一个示例类:

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

当与莫西连载,我得到:

{
    "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"
        }
}

谢谢,Behzad

Answer 1:

问题1

为什么使用数组指标“[]”,而鸟,猫,狗不是数组?

为了让你映射了与你的模型中,这JSON表示@XmlElementRef注释,它告诉JAXB使用的值@XmlRootElement注释作为继承指标。 随着莫西的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;

}

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

在这里,我做到这一点使用莫西的@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;

}

由于莫西如何处理@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);
    }

}


文章来源: EclipseLink MOXy JSON Serialization