title: ObjectMapper explicitly printing carriage returns and escaping quotation marks in my JSON
body: <p>Been asked to modify a project to save space in our console and log files that has the following Spring application property <strong>which I've been asked to leave in place</strong>:</p>
  
  <pre><code>spring.jackson.serialization.indent_output=true
  </code></pre>
  
  <p>Obviously, that's going to pretty print all of our JSON and take up a ton of space in the console and log files.</p>
  
  <p>The application has a groovy class that extends Spring's OncePerRequestFilter class that grabs HTTP requests and responses and sends them through a groovy class that masks sensitive data.  This class has a method that takes in a string which is essentially the stringified version of the request's or response's body.</p>
  
  <p>Once the string has been masked, it is run through Jackson's ObjectMapper to basically undo the spring application property to make every print pretty:</p>
  
  <p><strong>Code #1:</strong></p>
  
  <pre><code>return objectMapper.writer().without(SerializationFeature.INDENT_OUTPUT).writeValueAsString(stringToWrite);
  </code></pre>
  
  <p>Here's some example input that will be going through the code:</p>
  
  <pre><code>{
    "person" : {
      "personName" : "BAR,      FOO",
    }
  }
  </code></pre>
  
  <p>... and the result ends up being ...</p>
  
  <p><strong>Result #1:</strong></p>
  
  <pre><code>"{\r\n  \"person\" : {\r\n    \"personName\" : \"BAR,      FOO\",\r\n    }\r\n    }"
  </code></pre>
  
  <p>... trying to take the easy way out, I figured applying a simple .replaceAll() on the string would knock out the explicit quote escapes and carriage returns, but I found the carriage returns disappeared and that the quote escapes just come right back...</p>
  
  <p><strong>Code #2:</strong></p>
  
  <pre><code>return objectMapper.writer().without(SerializationFeature.INDENT_OUTPUT).writeValueAsString(stringToWrite.replaceAll("\\\"", '"').replaceAll("\\r", "").replaceAll("\\n", ""));
  </code></pre>
  
  <p><strong>Result #2:</strong></p>
  
  <pre><code>"{  \"person\" : {    \"personName\" : \"BAR,      FOO\"  }  }"
  </code></pre>
  
  <p>... and finally, if I just use </p>
  
  <p><strong>Code #3</strong></p>
  
  <pre><code>.replaceAll("\\\"", "")
  </code></pre>
  
  <p>... instead of what I used in Code #2 which was ...</p>
  
  <pre><code>.replaceAll("\\\"", '"')
  </code></pre>
  
  <p>... then I get ...</p>
  
  <p><strong>Result #3</strong></p>
  
  <pre><code>"{  person : {    personName : BAR,      FOO  }  }"
  </code></pre>
  
  <p>... but what is asked of me is ...</p>
  
  <p><strong>Desired Result:</strong> </p>
  
  <pre><code>"{  "person" : {    "personName" : "BAR,      FOO"  }  }"
  </code></pre>
  
  <p>It appears to be a result of something the objectMapper is using, but I'm a little at a loss at this point.</p>