编程语言
首页 > 编程语言> > java-如何使用MOXy将地图编组为{key:value,key:value,…}

java-如何使用MOXy将地图编组为{key:value,key:value,…}

作者:互联网

使用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折叠外部meta元素,因此我无法以相反的方式对MyObject进行编组.

附带说明一下,我也无法使用Eclipselink 2.6中的新XmlVariableNode,因为我只被允许使用API​​的稳定版本:(

有人知道我该如何解决?

解决方法:

On a side note, I’m also not able to use the new XmlVariableNode in
Eclipselink 2.6 as I’m only allowed to use stable releases of the API

标签:jaxb,moxy,json,java
来源: https://codeday.me/bug/20191122/2062377.html