|
| 1 | +ReJSON Python Client |
| 2 | +==================== |
| 3 | + |
| 4 | +.. image:: https://travis-ci.org/RedisLabs/rejson-py.svg?branch=master |
| 5 | + :target: https://travis-ci.org/RedisLabs/rejson-py |
| 6 | + |
| 7 | +.. image:: https://img.shields.io/pypi/pyversions/rejson.svg |
| 8 | + :target: https://github.com/RedisLabs/rejson-py |
| 9 | + |
| 10 | +rejson-py is a package that allows storing, updating and querying objects as |
| 11 | +JSON documents in a `Redis`_ database that is extended with the |
| 12 | +`ReJSON module`_. The package extends |
| 13 | +`redis-py`_'s interface with ReJSON's |
| 14 | +API, and performs on-the-fly serialization/deserialization of objects to/from |
| 15 | +JSON. |
| 16 | + |
| 17 | +.. _`Redis`: https://redis.io |
| 18 | +.. _`ReJSON module`: https://github.com/redislabsmodules/rejson |
| 19 | +.. _`redis-py`: https://github.com/andymccurdy/redis-py |
| 20 | + |
| 21 | +Installation |
| 22 | +------------ |
| 23 | + |
| 24 | +.. code-block:: bash |
| 25 | +
|
| 26 | + $ pip install rejson |
| 27 | +
|
| 28 | +Usage example |
| 29 | +------------- |
| 30 | + |
| 31 | +.. code-block:: python |
| 32 | +
|
| 33 | + from rejson import Client, Path |
| 34 | +
|
| 35 | + rj = Client(host='localhost', port=6379) |
| 36 | +
|
| 37 | + # Set the key `obj` to some object |
| 38 | + obj = { |
| 39 | + 'answer': 42, |
| 40 | + 'arr': [None, True, 3.14], |
| 41 | + 'truth': { |
| 42 | + 'coord': 'out there' |
| 43 | + } |
| 44 | + } |
| 45 | + rj.jsonset('obj', Path.rootPath(), obj) |
| 46 | +
|
| 47 | + # Get something |
| 48 | + print 'Is there anybody... {}?'.format( |
| 49 | + rj.jsonget('obj', Path('.truth.coord')) |
| 50 | + ) |
| 51 | +
|
| 52 | + # Delete something (or perhaps nothing), append something and pop it |
| 53 | + rj.jsondel('obj', Path('.arr[0]')) |
| 54 | + rj.jsonarrappend('obj', Path('.arr'), 'something') |
| 55 | + print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr'))) |
| 56 | +
|
| 57 | + # Update something else |
| 58 | + rj.jsonset('obj', Path('.answer'), 2.17) |
| 59 | +
|
| 60 | + # And use just like the regular redis-py client |
| 61 | + jp = rj.pipeline() |
| 62 | + jp.set('foo', 'bar') |
| 63 | + jp.jsonset('baz', Path.rootPath(), 'qaz') |
| 64 | + jp.execute() |
| 65 | +
|
| 66 | +
|
| 67 | +Encoding/Decoding |
| 68 | +----------------- |
| 69 | + |
| 70 | +rejson-py uses Python's json_. |
| 71 | +The client can be set to use custom encoders/decoders at creation, or by calling |
| 72 | +explicitly the setEncoder_ () and |
| 73 | +setDecoder_ () methods, respectively. |
| 74 | + |
| 75 | +.. _json: https://docs.python.org/2/library/json.html |
| 76 | +.. _setDecoder: ./API.md#setdecoder |
| 77 | +.. _setEncoder: ./API.md#setencoder |
| 78 | + |
| 79 | +The following shows how to use this for a custom class that's stored as |
| 80 | +a JSON string for example: |
| 81 | + |
| 82 | +.. code-block:: python |
| 83 | +
|
| 84 | + from json import JSONEncoder, JSONDecoder |
| 85 | + from rejson import Client |
| 86 | +
|
| 87 | + class CustomClass(object): |
| 88 | + "Some non-JSON-serializable" |
| 89 | + def __init__(self, s=None): |
| 90 | + if s is not None: |
| 91 | + # deserialize the instance from the serialization |
| 92 | + if s.startswith('CustomClass:'): |
| 93 | + ... |
| 94 | + else: |
| 95 | + raise Exception('unknown format') |
| 96 | + else: |
| 97 | + # initialize the instance |
| 98 | + ... |
| 99 | +
|
| 100 | + def __str__(self): |
| 101 | + _str = 'CustomClass:' |
| 102 | + # append the instance's state to the serialization |
| 103 | + ... |
| 104 | + return _str |
| 105 | +
|
| 106 | + ... |
| 107 | +
|
| 108 | + class CustomEncoder(JSONEncoder): |
| 109 | + "A custom encoder for the custom class" |
| 110 | + def default(self, obj): |
| 111 | + if isinstance(obj, CustomClass): |
| 112 | + return str(obj) |
| 113 | + return json.JSONEncoder.encode(self, obj) |
| 114 | +
|
| 115 | + class TestDecoder(JSONDecoder): |
| 116 | + "A custom decoder for the custom class" |
| 117 | + def decode(self, obj): |
| 118 | + d = json.JSONDecoder.decode(self, obj) |
| 119 | + if isinstance(d, basestring) and d.startswith('CustomClass:'): |
| 120 | + return CustomClass(d) |
| 121 | + return d |
| 122 | +
|
| 123 | + # Create a new instance of CustomClass |
| 124 | + obj = CustomClass() |
| 125 | +
|
| 126 | + # Create a new client with the custom encoder and decoder |
| 127 | + rj = Client(encoder=CustomEncoder(), decoder=CustomDecoder()) |
| 128 | +
|
| 129 | + # Store the object |
| 130 | + rj.jsonset('custom', Path.rootPath(), obj)) |
| 131 | +
|
| 132 | + # Retrieve it |
| 133 | + obj = rj.jsonget('custom', Path.rootPath()) |
| 134 | +
|
| 135 | +
|
| 136 | +API |
| 137 | +--- |
| 138 | + |
| 139 | +As rejson-py exposes the same methods as redis-py, it can be used as a drop-in |
| 140 | +replacement. On top of Redis' core commands, the client also adds ReJSON's |
| 141 | +vocabulary and a couple of helper methods. These are documented in the |
| 142 | +[API.md](API.md) file, which can be generated by running: |
| 143 | + |
| 144 | +.. code-block:: bash |
| 145 | +
|
| 146 | + $ python gendoc rejson > API.md |
| 147 | +
|
| 148 | +
|
| 149 | +For complete documentation about ReJSON's commands, refer to rejson_. |
| 150 | + |
| 151 | +.. _`rejson`: http://rejson.io |
| 152 | + |
| 153 | +License |
| 154 | +------- |
| 155 | + |
| 156 | +`BSD 2-Clause`_ |
| 157 | + |
| 158 | +.. _`BSD 2-Clause`: https://github.com/RedisLabs/rejson-py/blob/master/LICENSE |
0 commit comments