@Override
  public void writeTo(
      Object t,
      Class<?> type,
      Type genericType,
      Annotation[] annotations,
      MediaType mediaType,
      MultivaluedMap<String, Object> httpHeaders,
      OutputStream entityStream)
      throws IOException,
        WebApplicationException {
    // Special case of unsupported type, where surrounding framework
    // may have, mistakengly, chosen this provider based on media type, but when
    // response will be streamed using StreamingOutput or is already prepared using
    // in the form of CharSequence
    if (t instanceof StreamingOutput) {
      ((StreamingOutput) t).write(entityStream);
      return;
    }
    if (t instanceof CharSequence) {
      // UTF-8 used because it should be considered default encoding for the JSON-family
      // of media types
      OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8);
      writer.append((CharSequence) t);
      writer.flush();
      return;
    }
    // Standard way of handling writing using gson
    try {
      streamer.write(gson, genericType, t, entityStream);
    } catch (IOException ex) {
      exceptionHandler.onWrite(gson, ex);
      throw ex;
    }
  }
Creates new provider with flexible setup.
@param options options for provider
gson/src/org/immutables/gson/stream/GsonMessageBodyProvider.java