do not create a StringWriter when not needed (#15276)

Change-Id: I528f4034ef5cf375be31750bbad9baddcd3a2b64