Quote path elements in sqlite json paths

This commit is contained in:
Charles Leifer
2026-04-24 14:58:01 -05:00
parent 93da1ac2e4
commit cefdec2116
2 changed files with 11 additions and 1 deletions
+1 -1
View File
@@ -88,7 +88,7 @@ class JSONPath(ColumnBase):
if isinstance(idx, int) or idx == '#':
item = '[%s]' % idx
else:
item = '.%s' % idx
item = '."%s"' % idx.replace('"', '""')
return type(self)(self._field, self._path + (item,))
def append(self, value, as_json=None):
+10
View File
@@ -199,6 +199,16 @@ class TestJSONField(ModelTestCase):
kd_db = KeyData.get(KeyData.key == 'kx')
self.assertEqual(kd_db.data, value)
def test_key_with_special_chars(self):
kd = KeyData.create(key='k1', data={'k 1': {'k.. 2': {'{k3}': 'v4'}}})
def assertMatch(expr):
obj = KeyData.select().where(expr.is_null(False)).get()
self.assertEqual(obj.key, 'k1')
assertMatch(KeyData.data['k 1'])
assertMatch(KeyData.data['k 1']['k.. 2'])
assertMatch(KeyData.data['k 1']['k.. 2']['{k3}'])
def test_json_unicode(self):
with self.database.atomic():
KeyData.delete().execute()