1

複数のクラスのネストを利用するクラス構造のアン/マーシャリングを達成することは可能abstractですか?
次のようなクラス構造があるとします。

@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {}
public abstract class Mammal extends Animal {}
public class Tiger extends Mammal {}
public class Elephant extends Mammal {}

@XmlRootElementedクラスには動物のZooリストがあります。

@XmlElementWrapper(name = "animals")
@XmlElements({
    @XmlElement(name = "elephant", type = Elephant.class),
    @XmlElement(name = "tiger", type = Tiger.class)
})
private List<Animal> animals;

私はあなたがアイデアを得ると思います...これのためのXML:

<?xml version="1.0" encoding="UTF-8"?>
<zoo>
   <animals>
      <tiger>
         <name>Richard</name>
         <furry>true</furry>
      </tiger>
      <elephant>
         <name>Otis</name>
         <furry>false</furry>
      </elephant>
      <tiger>
         <name>Kirk</name>
         <furry>true</furry>
      </tiger>
   </animals>
</zoo>

これはいいですね。
今JSON...

 {
    "animals" : {
       "tiger" : [ {
          "name" : "Richard",
          "furry" : true
       }, {
          "name" : "Kirk",
          "furry" : true
       } ],
       "elephant" : [ {
          "name" : "Otis",
          "furry" : false
       } ]
    }
 }

MammalJSON でクラス オブジェクトをサブグループ化するのはなぜですか?

マーシャリングにEclipseLink MOXy 2.6を使用しています。

4

1 に答える 1

1

元の答え

MOXy はキーtigerをグループ化し、キーのelephant繰り返しを回避します。


更新 #1

したがって、{ '動物': [ {'@type': '虎'}, {'@type': 象'}, ... ] } のような JSON を取得することはできませんか?

はい、可能です。そのようにマッピングする必要があります。

動物園

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Zoo {

    private List<Animal> animals;

}

動物

import javax.xml.bind.annotation.*;

@XmlSeeAlso({Elephant.class, Tiger.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {

}

デモ

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

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

}

input.json/出力

{
   "animals" : [ {
      "@type" : "tiger"
   }, {
      "@type" : "elephant"
   }, {
      "@type" : "tiger"
   } ]
}

更新 #2

現在の XML 表現を維持し、JSON 表現のみを変更したい場合は、MOXy の外部マッピング ドキュメント拡張を使用できます ( http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.htmlを参照) 。

マッピング ドキュメント (oxm.xml)

MOXy の外部マッピング ドキュメントを使用して、クラスのanimalsフィールドのマッピングを変更します。Zoo

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum19384491">
    <java-types>
        <java-type name="Zoo">
            <java-attributes>
                <xml-element java-attribute="animals"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

デモ

以下のデモ コードでJAXBContextは、同じドメイン モデルに の 2 つのインスタンスを作成します。JSON 用のものは、外部マッピング ドキュメントを利用してマッピングをカスタマイズします。 input.xml質問の XML ドキュメントです。

import java.io.File;
import java.util.*;
import javax.xml.bind.*;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext xmlJC = JAXBContext.newInstance(Zoo.class);

        Unmarshaller unmarshaller = xmlJC.createUnmarshaller();
        File xml = new File("src/forum19384491/input.xml");
        Zoo zoo = (Zoo) unmarshaller.unmarshal(xml);

        Map<String, Object> properties = new HashMap<String, Object>(4);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum19384491/oxm.xml");
        properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
        properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
        properties.put(JAXBContextProperties.JSON_ATTRIBUTE_PREFIX, "@");
        JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Zoo.class}, properties);

        Marshaller marshaller = jsonJC.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(zoo, System.out);
    }

}

出力

以下は、デモ コードを実行した結果の出力です。

{
   "animals" : [ {
      "@type" : "tiger",
      "name" : "Richard",
      "furry" : true
   }, {
      "@type" : "elephant",
      "name" : "Otis",
      "furry" : false
   }, {
      "@type" : "tiger",
      "name" : "Kirk",
      "furry" : true
   } ]
}
于 2013-10-15T17:01:19.090 に答える