Skip to content

Commit

Permalink
Merge pull request #838 from neo4j-contrib/834-utf-json
Browse files Browse the repository at this point in the history
834 utf json
  • Loading branch information
mariusconjeaud authored Nov 5, 2024
2 parents f8ba4a1 + ce74a54 commit 5b3b1aa
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
7 changes: 4 additions & 3 deletions neomodel/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,16 +534,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

0 comments on commit 5b3b1aa

Please sign in to comment.