Skip to content

Commit 4fe7c93

Browse files
authored
Merge pull request #26 from bentsku/master
Fixing retrieving non-ascii string with JSON.GET, added no_escape flag
2 parents 0a35994 + f6b0b03 commit 4fe7c93

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

rejson/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
jp.set('foo', 'bar')
4848
jp.jsonset('baz', Path.rootPath(), 'qaz')
4949
jp.execute()
50+
51+
# If you use non-ascii character in your JSON data, you can add the no_escape flag to JSON.GET command
52+
obj_non_ascii = {
53+
'non_ascii_string': 'hyvää'
54+
}
55+
56+
rj.jsonset('non-ascii', Path.rootPath(), obj_non_ascii)
57+
print '{} is a non-ascii string'.format(rj.jsonget('non-ascii', Path('.non_ascii_string'), no_escape=True))
5058
```
5159
5260
## Encoding/Decoding

rejson/client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,19 @@ def jsondel(self, name, path=Path.rootPath()):
103103
"""
104104
return self.execute_command('JSON.DEL', name, str_path(path))
105105

106-
def jsonget(self, name, *args):
106+
def jsonget(self, name, *args, no_escape=False):
107107
"""
108108
Get the object stored as a JSON value at key ``name``
109109
``args`` is zero or more paths, and defaults to root path
110+
```no_escape`` is a boolean flag to add no_escape option to get non-ascii characters
110111
"""
111112
pieces = [name]
113+
if no_escape:
114+
pieces.append('noescape')
115+
112116
if len(args) == 0:
113117
pieces.append(Path.rootPath())
118+
114119
else:
115120
for p in args:
116121
pieces.append(str_path(p))

tests/test_rejson.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from unittest import TestCase
55
from rejson import Client, Path
66

7-
87
rj = None
98
port = 6379
109

@@ -25,6 +24,15 @@ def testJSONSetGetDelShouldSucceed(self):
2524
self.assertEqual(1, rj.jsondel('foo'))
2625
self.assertFalse(rj.exists('foo'))
2726

27+
def testJSONSetGetDelNonAsciiShouldSucceed(self):
28+
"Test non-ascii JSONSet/Get/Del"
29+
30+
self.assertTrue(rj.jsonset('notascii', Path.rootPath(), 'hyvää-élève'))
31+
self.assertNotEqual('hyvää-élève', rj.jsonget('notascii'))
32+
self.assertEqual('hyvää-élève', rj.jsonget('notascii', no_escape=True))
33+
self.assertEqual(1, rj.jsondel('notascii'))
34+
self.assertFalse(rj.exists('notascii'))
35+
2836
def testJSONSetExistentialModifiersShouldSucceed(self):
2937
"Test JSONSet's NX/XX flags"
3038

0 commit comments

Comments
 (0)