RedisJSON/redisjson-py

client.jsonget('foo', '.') causes TypeError if key doesn't exist.

eddieparker opened this issue · 2 comments

If you run the following:

connection = rejson.Client(host='redis')
print(connection.jsonget('test', '.'))

Python (3.7) will complain with an error like this:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/rejson/client.py", line 114, in jsonget
    return self.execute_command('JSON.GET', *pieces)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 668, in execute_command
    return self.parse_response(connection, command_name, **options)
  File "/usr/local/lib/python3.6/site-packages/redis/client.py", line 682, in parse_response
    return self.response_callbacks[command_name](response, **options)
  File "/usr/local/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: cannot use a string pattern on a bytes-like object

Given that running this on the cli yields 'nil', I'd expect jsonget to return None.

To fix this I've built a custom JSON Decoder that handles this, but it'd be nice to have it done in the API.

In case it's useful to someone else, here's what I wrote as a work around:

import json
import rejson

class ReJsonEncoder(json.JSONEncoder): pass

class ReJsonDecoder(json.JSONDecoder):
	def decode(self, s, *args, **kwargs):
		if s is not None:
			return json.JSONDecoder.decode(self, s, *args, **kwargs)
		return None

client = rejson.Client(host='redis', encoder=ReJsonEncoder(), decoder=ReJsonDecoder())

Hello @eddieparker

Thanks for reporting this. Would you like to take a stab at fixing the client and making a PR?

I've submitted pull request #11 to fix this issue.