Skip to content

Commit 5e25a8f

Browse files
authored
Merge pull request #46 from bentsku/master
fixes issues #44 #45 setDecoder not working, TypeError when reJSON returns None
2 parents b6fa063 + e195358 commit 5e25a8f

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

rejson/client.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class Client(StrictRedis):
3636
_encoder = None
3737
_encode = None
3838
_decoder = None
39-
_decode = None
4039

4140
def __init__(self, encoder=None, decoder=None, *args, **kwargs):
4241
"""
@@ -90,7 +89,9 @@ def setDecoder(self, decoder):
9089
self._decoder = json.JSONDecoder()
9190
else:
9291
self._decoder = decoder
93-
self._decode = self._decoder.decode
92+
93+
def _decode(self, s, *args, **kwargs):
94+
return self._decoder.decode(s, *args, **kwargs) if s is not None else None
9495

9596
def jsondel(self, name, path=Path.rootPath()):
9697
"""
@@ -113,14 +114,9 @@ def jsonget(self, name, *args, no_escape=False):
113114

114115
else:
115116
for p in args:
116-
pieces.append(str_path(p))
117-
118-
# Handle case where key doesn't exist. The JSONDecoder would raise a
119-
# TypeError exception since it can't decode None
120-
try:
121-
return self.execute_command('JSON.GET', *pieces)
122-
except TypeError:
123-
return None
117+
pieces.append(str_path(p))
118+
119+
return self.execute_command('JSON.GET', *pieces)
124120

125121
def jsonmget(self, path, *args):
126122
"""

tests/test_rejson.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ def testPipelineShouldSucceed(self):
167167
p = rj.pipeline()
168168
p.jsonset('foo', Path.rootPath(), 'bar')
169169
p.jsonget('foo')
170+
p.jsonget('bar')
170171
p.jsondel('foo')
171172
p.exists('foo')
172-
self.assertListEqual([True, 'bar', 1, False], p.execute())
173+
self.assertListEqual([True, 'bar', None, 1, False], p.execute())
173174

174175
def testCustomEncoderDecoderShouldSucceed(self):
175176
"Test a custom encoder and decoder"
@@ -209,6 +210,21 @@ def decode(self, obj):
209210
# Check the custom encoder
210211
self.assertTrue(rj.jsonset('cus', Path.rootPath(),
211212
CustomClass('foo', 'bar')))
213+
# Check the custom decoder
214+
obj = rj.jsonget('cus', Path.rootPath())
215+
self.assertIsNotNone(obj)
216+
self.assertEqual(CustomClass, obj.__class__)
217+
self.assertEqual('foo', obj.key)
218+
self.assertEqual('bar', obj.val)
219+
220+
# Test resetting the decoder after the client have been created
221+
rj.setDecoder(json.JSONDecoder())
222+
obj = rj.jsonget('cus', Path.rootPath())
223+
self.assertIsNotNone(obj)
224+
self.assertNotEqual(CustomClass, obj.__class__)
225+
226+
# Test setting the decoder after the client have been created
227+
rj.setDecoder(TestDecoder())
212228
obj = rj.jsonget('cus', Path.rootPath())
213229
self.assertIsNotNone(obj)
214230
self.assertEqual(CustomClass, obj.__class__)

0 commit comments

Comments
 (0)