2

I need to wrap some arbitrary JSON content into a POJO that is then serialized with MOXy/JAXB to JSON, but could not figure out how to bind a JsonObject with JAXB. I only need to marshall the JsonObject, unmarshalling is not required.

i.e. having the POJO:

@XmlRootElement
public class MsgPOJO {
  public String type;
  public Object content;
}

how to put an arbitrary JSON content in 'MsgPOJO.content', and serialize it:

String jsonDoc = "{\"prop\":\"value\"}";
MsgPOJO msg = new MsgPOJO();
msg.type = "whatever";
msg.content = jsonDoc;

so that this would be the output:

{
  "type": "whatever",
  "content": {
    "prop": "value"
   }
}

I was thinking about annotating the MsgPOJO.content with a @XmlJavaTypeAdapter, but this does not seem to get me anywhere, since the JSON content could be arbitrary.

It would be nice if moxy could marshal JsonObject or JsonStructure, so I could just define the POJO like:

@XmlRootElement
public class MsgPOJO {
  public String type;
  public JsonObject content;
}

Is there a way to make this work? Or is it a limitation in MOXy/JAXB?

4

1 回答 1

0

MOXy 默认不支持 JSON-P 结构的编组/解组,需要实现 XmlJavaTypeAdapter。下面是 JsonObject 适配器的示例。

消息POJO.java

package org.eclipse.persistence.testing.jsonp;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Created by mvojtek on 24/02/15.
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MsgPOJO {

    public String type;

    public JsonObjectWrapper content;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public JsonObjectWrapper getContent() {
        return content;
    }

    public void setContent(JsonObjectWrapper content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "MsgPOJO{" +
                "type='" + type + '\'' +
                ", content=" + content +
                '}';
    }
}

JsonObjectWrapper.java

package org.eclipse.persistence.testing.jsonp;

import javax.json.JsonObject;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * Created by mvojtek on 24/02/15.
 */
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class JsonObjectWrapper {

    @XmlJavaTypeAdapter(JsonObjectAdapter.class)
    private JsonObject jsonObject;

    public JsonObject getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(JsonObject jsonObject) {
        this.jsonObject = jsonObject;
    }

    @Override
    public String toString() {
        return "JsonObjectWrapper{" +
                "jsonObject=" + jsonObject +
                '}';
    }
}

JsonObjectAdapter.java

package org.eclipse.persistence.testing.jsonp;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.io.StringReader;

/**
 * Created by mvojtek on 24/02/15.
 */
public final class JsonObjectAdapter extends XmlAdapter<String,JsonObject> {
    @Override
    public String marshal(JsonObject v) throws Exception {
        if (null == v) {
            return null;
        }
        return v.toString();
    }

    @Override
    public JsonObject unmarshal(String v) throws Exception {
        if (null == v) {
            return null;
        }
        JsonReader jsonReader = Json.createReader(new StringReader(v));
        return jsonReader.readObject();
    }
}

测试.java

package org.eclipse.persistence.testing.jsonp;

import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.JAXBContextProperties;
import org.eclipse.persistence.oxm.MediaType;

import javax.json.Json;
import javax.json.JsonReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;

public class Test {

    public static void main(String[] args) throws Exception {

        //marshal
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class[]{MsgPOJO.class}, null);

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);

        MsgPOJO msgPOJO = new MsgPOJO();
        msgPOJO.setType("myType");

        JsonReader jsonReader = Json.createReader(new StringReader("{\"prop\":\"value\"}"));

        JsonObjectWrapper wrapper = new JsonObjectWrapper();
        wrapper.setJsonObject(jsonReader.readObject());

        msgPOJO.setContent(wrapper);

        StringWriter marshallerOutput = new StringWriter();

        marshaller.marshal(msgPOJO, marshallerOutput);

        String result = marshallerOutput.toString();
        System.out.println("marshal result = "+result);

        //unmarshal
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
        unmarshaller.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true);

        MsgPOJO msgPOJO2 = (MsgPOJO)unmarshaller.unmarshal(new StringReader(result));

        System.out.println("msgPOJO2="+msgPOJO2);
    }
}

如果您不想要 String,您可以借助 MyList 和 MyMap 结构来编写通用结构。之后,您可以编写 XmlJavaTypeAdapter,它将 JsonObject 编组为这个新类型。结果将是 json,与输入的字符串表示并不完全相同,而是合法的 json。

https://github.com/eclipse/eclipselink.runtime/blob/master/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/rs/model/MyList.java

https://github.com/eclipse/eclipselink.runtime/blob/master/moxy/eclipselink.moxy.test/src/org/eclipse/persistence/testing/jaxb/rs/model/MyMap.java

于 2015-02-24T23:02:52.113 回答