diff --git a/API.md b/API.md index 4c4a233..cd127c5 100644 --- a/API.md +++ b/API.md @@ -134,6 +134,14 @@ Creates a new ReJSON client. ``encoder`` should be an instance of a ``json.JSONEncoder`` class ``decoder`` should be an instance of a ``json.JSONDecoder`` class +**IMPORTANT**: With Python 3 the redis-py client defaults to returning bytes +instead of string replies. To overcome this, make sure that you +initialize the class with its ``decode_responses`` argument set to +``True``, e.g.: + +```py +rj = Client(host='localhost', port=6379, decode_responses=True) +``` ### jsonarrappend ```py @@ -210,7 +218,7 @@ def jsonarrtrim(self, name, path, start, stop) -Trim the array JSON value under ``path`` at key ``name`` to the +Trim the array JSON value under ``path`` at key ``name`` to the inclusive range given by ``start`` and ``stop`` @@ -248,7 +256,7 @@ def jsonmget(self, path, *args) -Gets the objects stored as a JSON values under ``path`` from +Gets the objects stored as a JSON values under ``path`` from keys ``args`` diff --git a/README.md b/README.md index 3e8a369..e723bd7 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![Mailing List](https://img.shields.io/badge/Mailing%20List-RedisJSON-blue)](https://groups.google.com/forum/#!forum/redisjson) [![Gitter](https://badges.gitter.im/RedisLabs/RedisJSON.svg)](https://gitter.im/RedisLabs/RedisJSON?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge) - + rejson-py is a package that allows storing, updating and querying objects as JSON documents in a [Redis](https://redis.io) database that is extended with the [ReJSON module](https://github.com/redislabsmodules/rejson). The package extends @@ -67,6 +67,18 @@ $ pip install rejson print '{} is a non-ascii string'.format(rj.jsonget('non-ascii', Path('.non_ascii_string'), no_escape=True)) ``` +## Python 2 vs. 3 + +The following is an excerpt from [redis-py's README "Getting Started" section](https://github.com/andymccurdy/redis-py/blob/master/README.rst#getting-started): + +> By default, all responses are returned as bytes in Python 3 and str in Python 2. The user is responsible for decoding to Python 3 strings or Python 2 unicode objects. + +Because of that, when using this client with Python 3, you initialize it with the base class's `decode_responses` argument set to `True`, e.g.: + +```py +rj = Client(host='localhost', port=6379, decode_responses=True) +``` + ## Encoding/Decoding rejson-py uses Python's [json](https://docs.python.org/2/library/json.html). diff --git a/rejson/client.py b/rejson/client.py index d8c806c..fb5a5c9 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -44,6 +44,15 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs): ``encoder`` should be an instance of a ``json.JSONEncoder`` class ``decoder`` should be an instance of a ``json.JSONDecoder`` class + + IMPORTANT: With Python 3 the redis-py client defaults to returning bytes + instead of string replies. To overcome this, make sure that you + initialize the class with its ``decode_responses`` argument set to + ``True``, e.g.: + + ``` + rj = Client(host='localhost', port=6379, decode_responses=True) + ``` """ self.setEncoder(encoder) self.setDecoder(decoder) @@ -69,7 +78,7 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs): } for k, v in six.iteritems(MODULE_CALLBACKS): self.set_response_callback(k, v) - + def setEncoder(self, encoder): """ Sets the client's encoder @@ -124,7 +133,7 @@ def jsonget(self, name, *args, no_escape=False): def jsonmget(self, path, *args): """ - Gets the objects stored as a JSON values under ``path`` from + Gets the objects stored as a JSON values under ``path`` from keys ``args`` """ pieces = [] @@ -228,7 +237,7 @@ def jsonarrpop(self, name, path=Path.rootPath(), index=-1): def jsonarrtrim(self, name, path, start, stop): """ - Trim the array JSON value under ``path`` at key ``name`` to the + Trim the array JSON value under ``path`` at key ``name`` to the inclusive range given by ``start`` and ``stop`` """ return self.execute_command('JSON.ARRTRIM', name, str_path(path), start, stop)