Package: rejson
RedisJSON Python ClientDeprecation noticeAs ofredis-py 4.0.0this library is deprecated. It's features have been merged into redis-py. Please either install itfrom pypyorthe repo.rejson-py is a package that allows storing, updating and querying objects as
JSON documents in aRedisdatabase that is extended with theReJSON module. The package extendsredis-py's interface with ReJSON's
API, and performs on-the-fly serialization/deserialization of objects to/from
JSON.Installation$pipinstallrejsonDevelopmentCreate a virtualenv to manage your python dependencies, and ensure it's active.virtualenv -v venvInstallpypoetryto manage your dependencies.pip install --user poetryInstall dependencies.poetry installtoxruns all tests as its default target. Runningtoxby itself will run unit tests. Ensure you have a running redis, with the module loaded.Usage examplefromrejsonimportClient,Pathrj=Client(host='localhost',port=6379,decode_responses=True)# Set the key `obj` to some objectobj={'answer':42,'arr':[None,True,3.14],'truth':{'coord':'out there'}}rj.jsonset('obj',Path.rootPath(),obj)# Get somethingprint'Is there anybody...{}?'.format(rj.jsonget('obj',Path('.truth.coord')))# Delete something (or perhaps nothing), append something and pop itrj.jsondel('obj',Path('.arr[0]'))rj.jsonarrappend('obj',Path('.arr'),'something')print'{}popped!'.format(rj.jsonarrpop('obj',Path('.arr')))# Update something elserj.jsonset('obj',Path('.answer'),2.17)# And use just like the regular redis-py clientjp=rj.pipeline()jp.set('foo','bar')jp.jsonset('baz',Path.rootPath(),'qaz')jp.execute()# If you use non-ascii character in your JSON data, you can add the no_escape flag to JSON.GET commandobj_non_ascii={'non_ascii_string':'hyvää'}rj.jsonset('non-ascii',Path.rootPath(),obj_non_ascii)print'{}is a non-ascii string'.format(rj.jsonget('non-ascii',Path('.non_ascii_string'),no_escape=True))Encoding/Decodingrejson-py uses Python'sjson.
The client can be set to use custom encoders/decoders at creation, or by calling
explicitly thesetEncoder()andsetDecoder()methods, respectively.The following shows how to use this for a custom class that's stored as
a JSON string for example:fromjsonimportJSONEncoder,JSONDecoderfromrejsonimportClientclassCustomClass(object):"Some non-JSON-serializable"def__init__(self,s=None):ifsisnotNone:# deserialize the instance from the serializationifs.startswith('CustomClass:'):...else:raiseException('unknown format')else:# initialize the instance...def__str__(self):_str='CustomClass:'# append the instance's state to the serialization...return_str...classCustomEncoder(JSONEncoder):"A custom encoder for the custom class"defdefault(self,obj):ifisinstance(obj,CustomClass):returnstr(obj)returnjson.JSONEncoder.encode(self,obj)classTestDecoder(JSONDecoder):"A custom decoder for the custom class"defdecode(self,obj):d=json.JSONDecoder.decode(self,obj)ifisinstance(d,basestring)andd.startswith('CustomClass:'):returnCustomClass(d)returnd# Create a new instance of CustomClassobj=CustomClass()# Create a new client with the custom encoder and decoderrj=Client(encoder=CustomEncoder(),decoder=CustomDecoder())# Store the objectrj.jsonset('custom',Path.rootPath(),obj))# Retrieve itobj=rj.jsonget('custom',Path.rootPath())APIAs rejson-py exposes the same methods as redis-py, it can be used as a drop-in
replacement. On top of Redis' core commands, the client also adds ReJSON's
vocabulary and a couple of helper methods. These are documented in theAPI.mdfile, which can be generated by running:$pythongendocrejson>API.mdFor complete documentation about ReJSON's commands, refer toReJSON's website.LicenseBSD 2-Clause