我有以下类型,在架构中定义为:
<xsd:complexType name="any_t" mixed="true">
<xsd:sequence>
<xsd:any processContents="lax" minOccurs="0" maxOccurs="unbounded"></xsd:any>
</xsd:sequence>
</xsd:complexType>
生成的JAXB类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "any_t", propOrder = {
"content"
})
public class AnyT implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
}
使用此类型的一些JAXB类:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"addInfo"
})
@XmlRootElement(name = "FORM_OF_COOWNERS")
public class FormOfCoowners
implements Serializable
{
private final static long serialVersionUID = 1L;
@XmlElement(name = "add_info")
protected AnyT addInfo;
public AnyT getAddInfo() { return addInfo; }
public void setAddInfo(AnyT value) { this.addInfo = value; }
}
当我将此XML解组为JAXB对象时:
<FORM_OF_COOWNERS>
<add_info>
<info_analytics>
<issuer_subdiv>
<id><id>1940001</id></id>
</issuer_subdiv>
<fl_worker>Yes</fl_worker>
</info_analytics>
</add_info>
</FORM_OF_COOWNERS>
并将此对象编组为JSON,我得到:
{"FORM_OF_COOWNERS":{"add_info":{"info_analytics":[{"issuer_subdiv":{"id":{"id":"1940001"}},"fl_worker":"Yes"}]}}}
此列表来自哪里? info_analytics映射到没有任何集合/数组的类型。
如果我得到这个JSON,将其转换为JAXB对象并编组为XML,它会生成:
<info_analytics fl_worker="Yes">
<issuer_subdiv>
<id id="1940001">
<id>1940001</id>
</id>
</issuer_subdiv>
<fl_worker>Yes</fl_worker>
</info_analytics>
为什么元素作为属性重复?
更新:编组/解组json:
def unmarshallToJaxbTyped[A : ClassTag](json: String): Throwable \/ A =
\/.fromTryCatchNonFatal {
val cls = classTag[A].runtimeClass
val jc = JAXBContext.newInstance(cls)
val unmarshaller = jc.createUnmarshaller()
unmarshaller.setProperty("eclipselink.media-type", "application/json")
unmarshaller.unmarshal(new StreamSource(new StringReader(json)), cls)
.getValue.asInstanceOf[A]
}
def marshallJaxbToJson(obj: Any): Throwable \/ String =
\/.fromTryCatchNonFatal {
val jc = JAXBContext.newInstance(obj.getClass)
val marshaller = jc.createMarshaller()
marshaller.setProperty("eclipselink.media-type", "application/json")
val writer = new StringWriter()
marshaller.marshal(obj, writer)
writer.getBuffer.toString
}
的xml:
protected static synchronized JAXBContext getContext() {
if (jaxbContext == null)
try {
jaxbContext = JAXBContext.newInstance("org.example.models");
} catch (JAXBException ex) {
throw new RuntimeException(ex);
}
return jaxbContext;
}
public static void saveMessage(Object msg, OutputStream os) throws JAXBException {
Marshaller marshaller = getContext().createMarshaller();
marshaller.marshal(msg, os);
}
public static Object loadMessage(InputStream is) throws JAXBException {
Unmarshaller unmarshaller = getContext().createUnmarshaller();
return unmarshaller.unmarshal(is);
}
答案 0 :(得分:0)
我不确定如此奇怪的往返:XML - &gt; Java - &gt; JSON - &gt; Java - &gt;需要XML。我想你明白,XML支持比JSON更多的功能,这就是为什么并不总是能够进行1对1的映射。
如果这对您来说是一个真正的问题而且不仅仅是理论研究,请针对MOXy发出错误:https://bugs.eclipse.org/bugs/enter_bug.cgi?product=EclipseLink