With Jackson i can create Json strings very easily:
String json = new ObjectMapper().writeValueAsString(object)
The problem is that this relies on specific api implementation. Is there a pure JavaEE way to achieve this?
I want to send the json via websocket. Sending via rest is no problem - specifying the mediatype is enough. Probably there is a way to create the json the same way the rest api does it.
In theorey, you should be able to something along the lines of:
@Context
private MessageBodyWorkers workers;
and then
MessageBodyWriter<MyBean> messageBodyWriter =
workers.getMessageBodyWriter(YourBean.class, YourBean.class,
new Annotation[]{}, MediaType.APPLICATION_JSON);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
messageBodyWriter.writeTo(myBean, YourBean.class, YourBean.class,
new Annotation[] {},
MediaType.APPLICATION_JSON,
new MultivaluedHashMap<String, Object>(),
baos);
However, I have to add that it never worked for me reliably, so I ended up calling Jackson directly instead.