php - Escaping special characters while storing JSON as string in Redis -
i trying store json data in redis through php, testing through redis-cli command line client.
in both clients, seem unable store json without escaping somehow.
redis 127.0.0.1:6379> set test1 {"array":[1,2,3],"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"hello world"} invalid argument(s)
doesn't work.
redis 127.0.0.1:6379> set test1 '{"array":[1,2,3],"number":123,"object":{"a":"b","c":"d","e":"f"},"string":"hello world"}' invalid argument(s)
tried single quotes. doesn't work.
redis 127.0.0.1:6379> set test1 \{\"array\"\:\[1\,2\,3\]\,\"number\"\:123\,\"object\"\:\{\"a\"\:\"b\"\,\"c\"\:\"d\"\,\"e\"\:\"f\"\},\"string\"\:\"hello world\"\} invalid argument(s)
tried escaping backslashes. doesn't work.
redis 127.0.0.1:6379> "\{\"array\"\:\[1\,2\,3\]\,\"number\"\:123\,\"object\"\:\{\"a\"\:\"b\"\,\"c\"\:\"d\"\,\"e\"\:\"f\"\},\"string\"\:\"hello world\"\}" ok
tried escaping backslashes , in double quotes.
and works!
redis 127.0.0.1:6379> test1 "{\"array\":[1,2,3],\"number\":123,\"object\":{\"a\":\"b\",\"c\":\"d\",\"e\":\"f\"},\"string\":\"hello world\"}"
now either there simple parameter in serialize() or json_encode() allows happen automatically,
or
i have write custom function using preg_replace() add slashes while storing , remove slashes while retrieving, , hope there no specific tricky data break custom regex based code.
i feel second option not advisable if there proper way it.
any ideas such option be?
i unable use predis (i tried https://github.com/nrk/predis/tree/php5.2_backport because working on php 5.2) found https://github.com/joelcox/codeigniter-redis , works basic data types perfectly.
so, option / parameter serialize() / json_encode() allow string redis-cli won't reject?
well, turns out line 118 @ https://github.com/joelcox/codeigniter-redis/blob/develop/libraries/redis.php
public function command($string) { $slices = explode(' ', $string); /* <-- here */ $request = $this->_encode_request($slices[0], array_slice($slices, 1)); return $this->_write_request($request); }
was blindly splitting entire command space character.
since had spaces in data (what real world data not...), breaking redis cli syntax.
so had escape spaces (replaced custom string) work properly.
Comments
Post a Comment