@Override
  public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8);
    try {
      Type jsonType;
      if (type.equals(genericType)) {
        jsonType = type;
      } else {
        jsonType = genericType;
      }
      if(!object.getClass().isAssignableFrom(Exception.class)){
    	  getGson().toJson(object, jsonType, writer);
      }else{
    	  writer.write(object.toString());
      }
    } finally {
      writer.close();
    }
  }