È possibile ottenere l'annullamento / il marshalling di una struttura di classi che utilizza l'annidamento di diverse abstract
classi?
Data una struttura di classe come questa:
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {}
public abstract class Mammal extends Animal {}
public class Tiger extends Mammal {}
public class Elephant extends Mammal {}
Il @XmlRootElement
Ed Zoo
class ha un elenco di animali:
@XmlElementWrapper(name = "animals")
@XmlElements({
@XmlElement(name = "elephant", type = Elephant.class),
@XmlElement(name = "tiger", type = Tiger.class)
})
private List<Animal> animals;
Penso che tu abbia l'idea ... l'XML per questo:
<?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>
Sembra a posto, fantastico.
Ora il JSON ...
{
"animals" : {
"tiger" : [ {
"name" : "Richard",
"furry" : true
}, {
"name" : "Kirk",
"furry" : true
} ],
"elephant" : [ {
"name" : "Otis",
"furry" : false
} ]
}
}
Perché suddivide il file Mammal
oggetti di classe in JSON?
Sto usando EclipseLink MOXy 2.6 per il marshalling.
risposte:
1 per risposta № 1RISPOSTA ORIGINALE
MOXy raggruppa le chiavi tiger
e elephant
per evitare di ripeterli.
AGGIORNAMENTO # 1
Quindi non è possibile ottenere un JSON come {"animals": [{"@type": "tiger"}, {"@type": elephant "}, ...]}?
Sì, è possibile, devi solo mapparlo in questo modo:
Zoo
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Zoo {
private List<Animal> animals;
}
Animale
import javax.xml.bind.annotation.*;
@XmlSeeAlso({Elephant.class, Tiger.class})
@XmlAccessorType(XmlAccessType.FIELD)
public abstract class Animal {
}
dimostrazione
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/Output
{
"animals" : [ {
"@type" : "tiger"
}, {
"@type" : "elephant"
}, {
"@type" : "tiger"
} ]
}
AGGIORNAMENTO # 2
Se vuoi mantenere la tua attuale rappresentazione XML e cambiare semplicemente la rappresentazione JSON puoi usare l'estensione del documento di mappatura esterno di MOXy (vedi: http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html)
Documento di mappatura (oxm.xml)
Useremo il documento di mappatura esterno di MOXy per modificare la mappatura per il file animals
campo sul Zoo
classe.
<?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>
dimostrazione
Nel codice demo qui sotto creiamo 2 istanze di JAXBContext
sullo stesso modello di dominio. Quello per JSON sfrutta un documento di mapping esterno per personalizzare il mapping. input.xml
è il documento XML dalla tua domanda.
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);
}
}
Produzione
Di seguito è riportato l'output dall'esecuzione del codice demo.
{
"animals" : [ {
"@type" : "tiger",
"name" : "Richard",
"furry" : true
}, {
"@type" : "elephant",
"name" : "Otis",
"furry" : false
}, {
"@type" : "tiger",
"name" : "Kirk",
"furry" : true
} ]
}