public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException
{
try
{
JsonValue jsonValue = null;
if (t instanceof JsonValue)
{
// Don't do any transformation if object is prepared JsonValue.
jsonValue = (JsonValue)t;
}
else
{
JsonGeneratorImpl generator = new JsonGeneratorImpl();
Types jtype = JsonUtils.getType(type);
if (jtype == Types.ARRAY_BOOLEAN || jtype == Types.ARRAY_BYTE || jtype == Types.ARRAY_SHORT
|| jtype == Types.ARRAY_INT || jtype == Types.ARRAY_LONG || jtype == Types.ARRAY_FLOAT
|| jtype == Types.ARRAY_DOUBLE || jtype == Types.ARRAY_CHAR || jtype == Types.ARRAY_STRING
|| jtype == Types.ARRAY_OBJECT)
{
jsonValue = generator.createJsonArray(t);
}
else if (jtype == Types.COLLECTION)
{
jsonValue = generator.createJsonArray((Collection<?>)t);
}
else if (jtype == Types.MAP)
{
jsonValue = generator.createJsonObjectFromMap((Map<String, ?>)t);
}
else
{
jsonValue = generator.createJsonObject(t);
}
}
JsonWriterImpl jsonWriter = new JsonWriterImpl(entityStream);
jsonValue.writeTo(jsonWriter);
jsonWriter.flush();
}
catch (JsonException e)
{
throw new IOException("Can't write to output stream " + e);