【问题标题】:How to marshal Map into {key: value, key: value, ...} with MOXy如何使用 MOXy 将 Map 编组为 {key: value, key: value, ...}
【发布时间】:2013-09-13 06:53:10
【问题描述】:

使用 Eclipselink MOXy,我有以下课程:

@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(name = "")
public class MyObject {
  private Map<String, String> meta;

  @XmlPath(".")
  @XmlJavaTypeAdapter(MetaMapAdapter.class)
  public Map<String, String> getMeta() {
    return meta;
  }

  public setMeta(Map<String, String> m) {
    meta = m;
  }
}

我的 AdaptedMap 看起来像这样(感谢 JAXB: how to marshall map into <key>value</key>):

import javax.xml.bind.annotation.XmlAnyElement;

public class AdaptedMap {
    private Object value;
    public AdaptedMap() {}
    @XmlAnyElement
    public Object getValue() { return value; }
    public void setValue(final Object value) { this.value = value; }
}

MapAdapter 看起来像这样(感谢JAXB: how to marshall map into <key>value</key>):

import java.util.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.parsers.*;
import org.eclipse.persistence.oxm.XMLRoot;
import org.w3c.dom.*;

public class MetaMapAdapter extends XmlAdapter<AdaptedMap, Map<String, String>> {
    public MapAdapter() {}

    @Override public AdaptedMap marshal(final Map<String, String> map) throws Exception {
        if (map == null) { return null; }

        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        final DocumentBuilder db = dbf.newDocumentBuilder();
        final Document document = db.newDocument();
        final Element rootElement = document.createElement(getTagName());
        document.appendChild(rootElement);

        for (final Entry<String, String> entry : map.entrySet()) {
            final Element mapElement = document.createElement(entry.getKey());
            mapElement.setTextContent(entry.getValue());
            rootElement.appendChild(mapElement);
        }

        final AdaptedMap adaptedMap = new AdaptedMap();
        adaptedMap.setValue(document);

        return adaptedMap;
    }

    @Override public Map<String, String> unmarshal(final AdaptedMap adaptedMap) {
        if (adaptedMap == null) { return null; }

        final Map<String, String> map = new HashMap<String, String>();
        final Element rootElement = (Element) adaptedMap.getValue();
        final NodeList childNodes = rootElement.getChildNodes();

        for (int x = 0, size = childNodes.getLength(); x < size; x++) {
            final Node childNode = childNodes.item(x);

            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                map.put(childNode.getLocalName(), childNode.getTextContent());
            }
        }

        return map;
    }
}

通过使用 Eclipselink MOXy,我可以在 XmlPath 的帮助下得到这个 JSON:

{
  "meta": {
    "akey":"avalue",
    "bkey":"bvalue"
  }
}

不幸的是,由于使用 XmlPath 折叠外部元元素,我无法反向解组到 MyObject。

附带说明,我也无法在 Eclipselink 2.6 中使用新的 XmlVariableNode,因为我只能使用 API 的稳定版本 :(

有人知道我该如何解决这个问题吗?

【问题讨论】:

  • 嗨,谢谢。我在我的应用程序中采用了类似的方法,但遇到了问题并且无法获得所需的输出。如果可能的话,请你看看这个问题,我已经详细解释了我在代码示例中遇到的问题。如果您有一些建议或解决方法,这对我真的很有帮助:stackoverflow.com/questions/67648941/…

标签: java json jaxb moxy


【解决方案1】:

顺便说一句,我也无法使用新的 XmlVariableNode Eclipselink 2.6,因为我只被允许使用 API 的稳定版本 :(

@XmlVariableNode 也已包含在现已发布的 EclipseLink 2.5.1 中:

此注释非常适合映射您的用例:

【讨论】:

  • 嗨,谢谢。我在我的应用程序中采用了类似的方法,但遇到了问题并且无法获得所需的输出。如果可能的话,请你看看这个问题,我已经详细解释了我在代码示例中遇到的问题。如果您有一些建议或解决方法,这对我真的很有帮助:stackoverflow.com/questions/67648941/…
猜你喜欢
  • 1970-01-01
  • 2015-03-22
  • 1970-01-01
  • 2021-03-17
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
相关资源
最近更新 更多