title: Escape JSON string in Java
body: <p>I'm using Google's <code>com.google.api.client.json.GenericJson</code> and <code>com.fasterxml.jackson.core.JsonGenerator</code>. I would like to serialize JSON object and escape quotes and backslashes so that I can pass that string in Bash. And afterwards deserialize that string.</p>
  
  <p><code>GenericJson.toString</code> produces simple JSON, but <code>\n</code> etc. are not escaped:</p>
  
  <pre><code>{commands=ls -laF\ndu -h, id=0, timeout=0}
  </code></pre>
  
  <p>is there a simple way how to get something like this:</p>
  
  <pre><code>"{commands=\"ls -laF\\ndu -h\", id=0, timeout=0}"
  </code></pre>
  
  <p>I don't want to reinvent the wheel, so I'd like to use Jackson or an existing API, if possible.</p>