MOXy doesn't support marshal/unmarshal of JSON-P structures by default, you need to implement XmlJavaTypeAdapter. Below is example for JsonObject adapter.
MsgPOJO.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();
}
}
Test.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);
}
}
If you don't want String, you can write general structure with the help of MyList and MyMap structures. After that, you can write XmlJavaTypeAdapter, which will marshal JsonObject to this new type. The result will be json, not reallly the same as string representation of the input, but legal 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