我需要将一些任意JSON内容包装到POJO中,然后使用MOXy / JAXB将其序列化为JSON,但无法弄清楚如何将JsonObject
与JAXB绑定。我只需要对JsonObject
进行编组,不需要解组。
即。有POJO:
@XmlRootElement
public class MsgPOJO {
public String type;
public Object content;
}
如何将任意JSON内容放入' MsgPOJO.content',并将其序列化:
String jsonDoc = "{\"prop\":\"value\"}";
MsgPOJO msg = new MsgPOJO();
msg.type = "whatever";
msg.content = jsonDoc;
这样就是输出:
{
"type": "whatever",
"content": {
"prop": "value"
}
}
我在考虑使用MsgPOJO.content
注释@XmlJavaTypeAdapter
,但这似乎并没有让我任何地方,因为JSON内容可能是任意的。
如果moxy可以封送JsonObject
或JsonStructure
,那将是很好的,所以我可以像以下一样定义POJO:
@XmlRootElement
public class MsgPOJO {
public String type;
public JsonObject content;
}
有没有办法让这项工作?或者它是MOXy / JAXB的限制?
答案 0 :(得分:0)
默认情况下,MOXy不支持JSON-P结构的编组/解组,您需要实现XmlJavaTypeAdapter。下面是JsonObject适配器的示例。
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);
}
}
如果您不想要String,可以在MyList和MyMap结构的帮助下编写通用结构。之后,您可以编写XmlJavaTypeAdapter,它将JsonObject编组为此新类型。结果将是json,而不是与输入的字符串表示相同,但是合法的json。