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?