diff --git a/neomodel/properties.py b/neomodel/properties.py index 0cd716bd..e28e2df3 100644 --- a/neomodel/properties.py +++ b/neomodel/properties.py @@ -534,8 +534,9 @@ 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): @@ -543,7 +544,7 @@ def inflate(self, value): @validator def deflate(self, value): - return json.dumps(value) + return json.dumps(value, ensure_ascii=self.ensure_ascii) class AliasProperty(property, Property): diff --git a/test/async_/test_properties.py b/test/async_/test_properties.py index 1e8f0c44..646f2cd5 100644 --- a/test/async_/test_properties.py +++ b/test/async_/test_properties.py @@ -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) diff --git a/test/sync_/test_properties.py b/test/sync_/test_properties.py index 53ae0002..0b0b576b 100644 --- a/test/sync_/test_properties.py +++ b/test/sync_/test_properties.py @@ -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)