{
MessageBodyWriter writer = factory.getMessageBodyWriter(token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE);
if (writer == null) throw new NullPointerException("Could not find MessageBodyWriter for JSON");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Providers old = ResteasyProviderFactory.getContextData(Providers.class);
ResteasyProviderFactory.pushContext(Providers.class, factory);
try
{
writer.writeTo(token, token.getClass(), null, null, MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<String, Object>(), baos);