@SuppressWarnings("unchecked")
    @Override
    public void write(Gson gson, Type type, Object object, OutputStream stream) throws IOException {
      @Nullable JsonGeneratorWriter writer = null;
      boolean wasOriginalException = false;
      try {
        JsonGenerator generator = JSON_FACTORY.createGenerator(stream);
        if (options.prettyPrinting) {
          generator.useDefaultPrettyPrinter();
        }
        writer = new JsonGeneratorWriter(generator);
        options.setWriterOptions(writer);

        gson.getAdapter((TypeToken<Object>) TypeToken.get(type)).write(writer, object);
      } catch (IOException ex) {
        wasOriginalException = true;
        throw ex;
      } catch (Exception ex) {
        wasOriginalException = true;
        throw new IOException(ex);
      } finally {
        if (writer != null) {
          try {
            // note that stream is not closed here as per JSON_FACTORY configuration
            writer.close();
          } catch (IOException ex) {
            if (!wasOriginalException) {
              throw ex;
            }
          }
        }
      }
    }
Forces creation of more strict in string reading if in !lenient mode
gson/src/org/immutables/gson/stream/GsonMessageBodyProvider.java