diff --git a/rejson/client.py b/rejson/client.py index d8c806c..bb97f1f 100644 --- a/rejson/client.py +++ b/rejson/client.py @@ -36,7 +36,6 @@ class Client(StrictRedis): _encoder = None _encode = None _decoder = None - _decode = None def __init__(self, encoder=None, decoder=None, *args, **kwargs): """ @@ -90,7 +89,9 @@ def setDecoder(self, decoder): self._decoder = json.JSONDecoder() else: self._decoder = decoder - self._decode = self._decoder.decode + + def _decode(self, s, *args, **kwargs): + return self._decoder.decode(s, *args, **kwargs) if s is not None else None def jsondel(self, name, path=Path.rootPath()): """ @@ -113,14 +114,9 @@ def jsonget(self, name, *args, no_escape=False): else: for p in args: - pieces.append(str_path(p)) - - # Handle case where key doesn't exist. The JSONDecoder would raise a - # TypeError exception since it can't decode None - try: - return self.execute_command('JSON.GET', *pieces) - except TypeError: - return None + pieces.append(str_path(p)) + + return self.execute_command('JSON.GET', *pieces) def jsonmget(self, path, *args): """ diff --git a/tests/test_rejson.py b/tests/test_rejson.py index f537873..e35aded 100644 --- a/tests/test_rejson.py +++ b/tests/test_rejson.py @@ -167,9 +167,10 @@ def testPipelineShouldSucceed(self): p = rj.pipeline() p.jsonset('foo', Path.rootPath(), 'bar') p.jsonget('foo') + p.jsonget('bar') p.jsondel('foo') p.exists('foo') - self.assertListEqual([True, 'bar', 1, False], p.execute()) + self.assertListEqual([True, 'bar', None, 1, False], p.execute()) def testCustomEncoderDecoderShouldSucceed(self): "Test a custom encoder and decoder" @@ -209,6 +210,21 @@ def decode(self, obj): # Check the custom encoder self.assertTrue(rj.jsonset('cus', Path.rootPath(), CustomClass('foo', 'bar'))) + # Check the custom decoder + obj = rj.jsonget('cus', Path.rootPath()) + self.assertIsNotNone(obj) + self.assertEqual(CustomClass, obj.__class__) + self.assertEqual('foo', obj.key) + self.assertEqual('bar', obj.val) + + # Test resetting the decoder after the client have been created + rj.setDecoder(json.JSONDecoder()) + obj = rj.jsonget('cus', Path.rootPath()) + self.assertIsNotNone(obj) + self.assertNotEqual(CustomClass, obj.__class__) + + # Test setting the decoder after the client have been created + rj.setDecoder(TestDecoder()) obj = rj.jsonget('cus', Path.rootPath()) self.assertIsNotNone(obj) self.assertEqual(CustomClass, obj.__class__)