Skip to content
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

834 utf json #838

Merged
merged 5 commits into from
Nov 5, 2024
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
7 changes: 4 additions & 3 deletions neomodel/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,17 @@ class JSONProperty(Property):
The structure will be inflated when a node is retrieved.
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, ensure_ascii=True, *args, **kwargs):
self.ensure_ascii = ensure_ascii
super(JSONProperty, self).__init__(*args, **kwargs)

@validator
def inflate(self, value):
return json.loads(value)

@validator
def deflate(self, value):
return json.dumps(value)
return json.dumps(value, ensure_ascii=self.ensure_ascii)


class AliasProperty(property, Property):
Expand Down
15 changes: 15 additions & 0 deletions test/async_/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ def test_json():
assert prop.deflate(value) == '{"test": [1, 2, 3]}'
assert prop.inflate('{"test": [1, 2, 3]}') == value

value_with_unicode = {"test": [1, 2, 3, "©"]}
assert prop.deflate(value_with_unicode) == '{"test": [1, 2, 3, "\\u00a9"]}'
assert prop.inflate('{"test": [1, 2, 3, "\\u00a9"]}') == value_with_unicode


def test_json_unicode():
prop = JSONProperty(ensure_ascii=False)
prop.name = "json"
prop.owner = FooBar

value = {"test": [1, 2, 3, "©"]}

assert prop.deflate(value) == '{"test": [1, 2, 3, "©"]}'
assert prop.inflate('{"test": [1, 2, 3, "©"]}') == value


def test_indexed():
indexed = StringProperty(index=True)
Expand Down
15 changes: 15 additions & 0 deletions test/sync_/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,21 @@ def test_json():
assert prop.deflate(value) == '{"test": [1, 2, 3]}'
assert prop.inflate('{"test": [1, 2, 3]}') == value

value_with_unicode = {"test": [1, 2, 3, "©"]}
assert prop.deflate(value_with_unicode) == '{"test": [1, 2, 3, "\\u00a9"]}'
assert prop.inflate('{"test": [1, 2, 3, "\\u00a9"]}') == value_with_unicode


def test_json_unicode():
prop = JSONProperty(ensure_ascii=False)
prop.name = "json"
prop.owner = FooBar

value = {"test": [1, 2, 3, "©"]}

assert prop.deflate(value) == '{"test": [1, 2, 3, "©"]}'
assert prop.inflate('{"test": [1, 2, 3, "©"]}') == value


def test_indexed():
indexed = StringProperty(index=True)
Expand Down
Loading