title: Escaping special characters while storing JSON as string in Redis
body: <p>I am trying to store JSON data in Redis through PHP, but testing it through the redis-cli command line client.</p>
  
  <p>In both clients, I seem to be unable to store JSON without escaping it somehow.</p>
  
  <pre><code>redis 127.0.0.1:6379&gt; set test1 {"array":[1,2,3],"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Hello World"}
  Invalid argument(s)
  </code></pre>
  
  <p>Doesn't work.</p>
  
  <pre><code>redis 127.0.0.1:6379&gt; set test1 '{"array":[1,2,3],"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"Hello World"}'
  Invalid argument(s)
  </code></pre>
  
  <p>Tried single quotes. Doesn't work. </p>
  
  <pre><code>redis 127.0.0.1:6379&gt; set test1 \{\"array\"\:\[1\,2\,3\]\,\"number\"\:123\,\"object\"\:\{\"a\"\:\"b\"\,\"c\"\:\"d\"\,\"e\"\:\"f\"\},\"string\"\:\"Hello World\"\}
  Invalid argument(s)
  </code></pre>
  
  <p>Tried escaping everything with backslashes. Doesn't work. </p>
  
  <pre><code>redis 127.0.0.1:6379&gt; "\{\"array\"\:\[1\,2\,3\]\,\"number\"\:123\,\"object\"\:\{\"a\"\:\"b\"\,\"c\"\:\"d\"\,\"e\"\:\"f\"\},\"string\"\:\"Hello World\"\}"
  OK
  </code></pre>
  
  <p>Tried escaping everything with backslashes and in double quotes. </p>
  
  <p><strong>And it works!</strong></p>
  
  <pre><code>redis 127.0.0.1:6379&gt; get test1
  "{\"array\":[1,2,3],\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"Hello World\"}"
  </code></pre>
  
  <p>Now either there is a simple parameter in serialize() or json_encode() which allows this to happen automatically, </p>
  
  <p>OR</p>
  
  <p>I have to write a custom function using preg_replace() to add slashes while storing and then remove slashes while retrieving, and hope that there is no specific tricky data that will break my custom regex based code. </p>
  
  <p>I feel that the second option is really not advisable if there is a proper way to do it.</p>
  
  <p>Any ideas what such an option would be?</p>
  
  <p>I am unable to use Predis (I tried <a href="https://github.com/nrk/predis/tree/php5.2_backport" rel="nofollow">https://github.com/nrk/predis/tree/php5.2_backport</a> because I am working on PHP 5.2) but then found <a href="https://github.com/joelcox/codeigniter-redis" rel="nofollow">https://github.com/joelcox/codeigniter-redis</a> and it works for all basic data types perfectly.</p>
  
  <p>So, what is the option / parameter to serialize() / json_encode() that will allow a string which redis-cli won't reject?</p>