class JsonEntityProvider implements EntityProvider<T> {

    @SuppressWarnings("unchecked")
    @Override
    public void writeTo(T t,
                        Class<?> type,
                        Type genericType,
                        Annotation[] annotations,
                        MediaType mediaType,
                        MultivaluedMap<String, Object> httpHeaders,
                        OutputStream entityStream) throws IOException {
        try {
            JsonValue jsonValue;
            if (t instanceof JsonValue) {
                jsonValue = (JsonValue)t;
            } else {
                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 = JsonGenerator.createJsonArray(t);
                } else if (jType == Types.COLLECTION) {
                    jsonValue = JsonGenerator.createJsonArray((Collection<?>)t);
                } else if (jType == Types.MAP) {
                    jsonValue = JsonGenerator.createJsonObjectFromMap((Map<String, ?>)t);
                } else {
                    jsonValue = JsonGenerator.createJsonObject(t);
                }
            }
            JsonWriter jsonWriter = new JsonWriter(entityStream);
            jsonValue.writeTo(jsonWriter);
            jsonWriter.flush();
        } catch (JsonException e) {
            LOG.debug(e.getMessage(), e);
            throw new IOException(String.format("Can't write to output stream. %s", e.getMessage()), e);
        }
    }

}