code: @Override
      public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations,
          MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
          throws IOException, WebApplicationException
      {
          OutputStreamWriter outputStreamWriter = null;
          
          try
          {    
              outputStreamWriter = new OutputStreamWriter(entityStream, CHARSET);   
              Type jsonType = getAppropriateType(type, genericType);
              String json = getGson().toJson(object, jsonType);
              if(logger.isLoggable(Level.FINE))
                  logger.fine("Outgoing JSON Entity: "+json);
              getGson().toJson(object, jsonType, outputStreamWriter);
          }
          finally
          {    
              if(outputStreamWriter != null)
                  outputStreamWriter.close();
          }
      }
docs: Write a type to a HTTP message.
  @param object The instance to write
  @param type The class of instance that is to be written
  @param genericType The type of instance to be written
  @param annotations An array of the annotations attached to the message entity instance
  @param mediaType The media type of the HTTP entity
  @param httpHeaders A mutable map of the HTTP message headers
  @param entityStream the OutputStream for the HTTP entity