Skip to content

Fixing retrieving non-ascii string with JSON.GET, added no_escape flag #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions rejson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
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 command
obj_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/Decoding
Expand Down
7 changes: 6 additions & 1 deletion rejson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,19 @@ def jsondel(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.DEL', name, str_path(path))

def jsonget(self, name, *args):
def jsonget(self, name, *args, no_escape=False):
"""
Get the object stored as a JSON value at key ``name``
``args`` is zero or more paths, and defaults to root path
```no_escape`` is a boolean flag to add no_escape option to get non-ascii characters
"""
pieces = [name]
if no_escape:
pieces.append('noescape')

if len(args) == 0:
pieces.append(Path.rootPath())

else:
for p in args:
pieces.append(str_path(p))
Expand Down
10 changes: 9 additions & 1 deletion tests/test_rejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from unittest import TestCase
from rejson import Client, Path


rj = None
port = 6379

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

def testJSONSetGetDelNonAsciiShouldSucceed(self):
"Test non-ascii JSONSet/Get/Del"

self.assertTrue(rj.jsonset('notascii', Path.rootPath(), 'hyvää-élève'))
self.assertNotEqual('hyvää-élève', rj.jsonget('notascii'))
self.assertEqual('hyvää-élève', rj.jsonget('notascii', no_escape=True))
self.assertEqual(1, rj.jsondel('notascii'))
self.assertFalse(rj.exists('notascii'))

def testJSONSetExistentialModifiersShouldSucceed(self):
"Test JSONSet's NX/XX flags"

Expand Down