gpt4 book ai didi

java - 输出json时可以使用Eclipselink MOXy移动一个元素成为key吗

转载 作者:行者123 更新时间:2023-12-01 05:14:16 25 4
gpt4 key购买 nike

从我的 JAXB 模型中我可以输出这个 xml

<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0">
<work-list>
<work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
<relation-list target-type="artist">
<relation type="composer">
<direction>backward</direction>
</relation>
</relation-list>
</work>
</work-list>
</metadata>

当前使用 MOXy 和 oxml.xml 我可以输出以下 JSON

{
"work" : [ {
"relations": [ {
"target-type" : "artist",
"relation" : [ {
"type" : "composer",
"direction" : "backward",
},
} ]
} ]
} ]
}

(在我的 oxml.xml 中,我展平了工作列表和关系列表对象,并将关系重命名为关系。)

但实际的要求是一个更复杂的转换,我不确定如何用正确的术语解释它,但这里是所需输出的示例。

{
"work" : [ {
"relations": {
"artist": [{
"direction": "backward",
"type": "composer",
}
],
}
}]
}

这可以在 eclipselink MOXy 中完成吗?

最佳答案

下面是如何完成此操作的示例:

注册表

您将需要一个用 @XmlRegistry 注释的类,该类具有针对五个不同元素值中的每一个用 @XmlElementDecl 标记的方法。

package forum11537931;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.*;
import javax.xml.namespace.QName;

@XmlRegistry
public class Registry {
private static final String ARTIST = "artist";
private static final String FOO = "foo";

@XmlElementDecl(name=ARTIST)
public JAXBElement<Relation> createArtist(Relation relation) {
return new JAXBElement<Relation>(new QName(ARTIST), Relation.class, relation);
}

@XmlElementDecl(name=FOO, substitutionHeadName=ARTIST)
public JAXBElement<Relation> createFoo(Relation relation) {
return new JAXBElement<Relation>(new QName(FOO), Relation.class, relation);
}

}

关系适配器

我们将使用 XmlAdapterRelations 对象转换为更能映射到 JSON 表示形式的对象。我们将利用 @XmlElementRef 注释来执行此操作(请参阅 http://blog.bdoughan.com/2010/12/represent-string-values-as-element.html )。

package forum11537931;

import java.util.*;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;

public class RelationsAdapter extends XmlAdapter<RelationsAdapter.AdaptedRelations, Relations> {

@Override
public Relations unmarshal(AdaptedRelations v) throws Exception {
// TODO Auto-generated method stub
return null;
}

@Override
public AdaptedRelations marshal(Relations relations) throws Exception {
AdaptedRelations adaptedRelations = new AdaptedRelations();
for(Relation relation : relations.relations) {
adaptedRelations.relations.add(new JAXBElement<Relation>(new QName(relations.targetType), Relation.class, relation));
}
return adaptedRelations;
}

@XmlSeeAlso({Registry.class})
public static class AdaptedRelations {

@XmlElementRef(type=JAXBElement.class, name="artist")
public List<JAXBElement<Relation>> relations = new ArrayList<JAXBElement<Relation>>();

}

}

oxm.xml

由于您不希望将 XmlAdapter 应用于 XML 表示,我们将使用 MOXy 的外部映射文档指定它(请参阅 http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html )。

<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum11537931">
<java-types>
<java-type name="Work">
<java-attributes>
<xml-element java-attribute="relations">
<xml-java-type-adapter value="forum11537931.RelationsAdapter"/>
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>

input.xml

为了本示例的目的,我简化了您的 XML 文档。

<?xml version="1.0" encoding="UTF-8"?>
<work id="4ff89cf0-86af-11de-90ed-001fc6f176ff">
<relation-list target-type="artist">
<relation type="composer">
<direction>backward</direction>
</relation>
</relation-list>
</work>

演示

package forum11537931;

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 {
// XML
JAXBContext jc = JAXBContext.newInstance(Work.class, Registry.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum11537931/input.xml");
Work work = (Work) unmarshaller.unmarshal(xml);

// JSON
Map<String, Object> properties = new HashMap<String, Object>(2);
properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11537931/oxm.xml");
properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
JAXBContext jsonJC = JAXBContext.newInstance(new Class[] {Work.class, Registry.class}, properties);
Marshaller jsonMarshaller = jsonJC.createMarshaller();
jsonMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jsonMarshaller.marshal(work, System.out);
}

}

输出

{
"work" : {
"relations" : [ {
"artist" : [ {
"type" : "composer",
"direction" : "backward"
} ]
} ]
}
}
<小时/>

域模型

工作

package forum11537931;

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Work {

@XmlElement(name="relation-list")
List<Relations> relations;

}

关系

package forum11537931;

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

@XmlAccessorType(XmlAccessType.FIELD)
public class Relations {

@XmlAttribute(name="target-type")
String targetType;

@XmlElement(name="relation")
List<Relation> relations = new ArrayList<Relation>();

}

关系

package forum11537931;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Relation {

@XmlAttribute
String type;

String direction;

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要在与您的域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目(请参阅: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

关于java - 输出json时可以使用Eclipselink MOXy移动一个元素成为key吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11537931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com