django-jsonpath 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- django_jsonpath/__init__.py +0 -0
- django_jsonpath/apps.py +30 -0
- django_jsonpath/compiler.py +97 -0
- django_jsonpath/exceptions.py +2 -0
- django_jsonpath/lookup.py +34 -0
- django_jsonpath/transform.py +56 -0
- django_jsonpath-0.1.0.dist-info/METADATA +93 -0
- django_jsonpath-0.1.0.dist-info/RECORD +9 -0
- django_jsonpath-0.1.0.dist-info/WHEEL +4 -0
|
File without changes
|
django_jsonpath/apps.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
from django.db.models import JSONField
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class DjangoJsonPathConfig(AppConfig):
|
|
6
|
+
name = "django_jsonpath"
|
|
7
|
+
|
|
8
|
+
def ready(self):
|
|
9
|
+
from django_jsonpath.lookup import (
|
|
10
|
+
JsonPathExactLookup,
|
|
11
|
+
JsonPathGteLookup,
|
|
12
|
+
JsonPathGtLookup,
|
|
13
|
+
JsonPathLteLookup,
|
|
14
|
+
JsonPathLtLookup,
|
|
15
|
+
)
|
|
16
|
+
from django_jsonpath.transform import (
|
|
17
|
+
JsonPathAnyTransform,
|
|
18
|
+
JSONPathField,
|
|
19
|
+
JsonPathTransform,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
JSONField.register_lookup(JsonPathTransform)
|
|
23
|
+
|
|
24
|
+
JSONPathField.register_lookup(JsonPathExactLookup)
|
|
25
|
+
JSONPathField.register_lookup(JsonPathGteLookup)
|
|
26
|
+
JSONPathField.register_lookup(JsonPathGtLookup)
|
|
27
|
+
JSONPathField.register_lookup(JsonPathLteLookup)
|
|
28
|
+
JSONPathField.register_lookup(JsonPathLtLookup)
|
|
29
|
+
|
|
30
|
+
JSONPathField.register_lookup(JsonPathAnyTransform)
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import math
|
|
3
|
+
|
|
4
|
+
from django_jsonpath.exceptions import JsonPathCompileError
|
|
5
|
+
from django_jsonpath.lookup import JsonPathTerminalLookup
|
|
6
|
+
from django_jsonpath.transform import (
|
|
7
|
+
JsonPathAnyTransform,
|
|
8
|
+
JsonPathIndexTransform,
|
|
9
|
+
JsonPathKeyTransform,
|
|
10
|
+
JsonPathTransform,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class JsonPathSQLCompiler:
|
|
15
|
+
def __init__(self, compiler):
|
|
16
|
+
self.compiler = compiler
|
|
17
|
+
|
|
18
|
+
def split_expression(self, node) -> tuple[list, object, object]:
|
|
19
|
+
|
|
20
|
+
path_nodes_start = node
|
|
21
|
+
predicate = []
|
|
22
|
+
is_predicate = True
|
|
23
|
+
|
|
24
|
+
while node and not isinstance(node, JsonPathTransform):
|
|
25
|
+
|
|
26
|
+
# The first time different node type encountered, it is not a predicate
|
|
27
|
+
if not isinstance(
|
|
28
|
+
node,
|
|
29
|
+
(JsonPathKeyTransform, JsonPathTerminalLookup)
|
|
30
|
+
):
|
|
31
|
+
is_predicate = False
|
|
32
|
+
|
|
33
|
+
if is_predicate:
|
|
34
|
+
predicate.append(node)
|
|
35
|
+
path_nodes_start = path_nodes_start.lhs
|
|
36
|
+
|
|
37
|
+
node = node.lhs
|
|
38
|
+
|
|
39
|
+
root = getattr(node, "lhs", None)
|
|
40
|
+
if root is None:
|
|
41
|
+
raise JsonPathCompileError("Cannot determine JsonPath root node.")
|
|
42
|
+
|
|
43
|
+
return predicate, path_nodes_start, root
|
|
44
|
+
|
|
45
|
+
def compile_path(self, node) -> str | None:
|
|
46
|
+
if isinstance(node, JsonPathAnyTransform):
|
|
47
|
+
return f"{self.compile_path(node.lhs)}[*]"
|
|
48
|
+
|
|
49
|
+
if isinstance(node, JsonPathKeyTransform):
|
|
50
|
+
return f"{self.compile_path(node.lhs)}.{json.dumps(node.key_name)}"
|
|
51
|
+
|
|
52
|
+
if isinstance(node, JsonPathTransform):
|
|
53
|
+
return "$"
|
|
54
|
+
|
|
55
|
+
if isinstance(node, JsonPathIndexTransform):
|
|
56
|
+
return f"{self.compile_path(node.lhs)}[{int(node.key_name)}]"
|
|
57
|
+
|
|
58
|
+
def compile_predicates(self, nodes) -> str:
|
|
59
|
+
sql = ""
|
|
60
|
+
for node in nodes:
|
|
61
|
+
|
|
62
|
+
if isinstance(node, JsonPathKeyTransform):
|
|
63
|
+
sql = f".{json.dumps(node.key_name)}{sql}"
|
|
64
|
+
|
|
65
|
+
if isinstance(node, JsonPathTerminalLookup):
|
|
66
|
+
rhs = self.compile_jsonpath_literal(node.rhs)
|
|
67
|
+
sql = f"{sql} {node.jsonpath_operator} {rhs}"
|
|
68
|
+
|
|
69
|
+
return f"@{sql}"
|
|
70
|
+
|
|
71
|
+
def compile_jsonpath_literal(self, value):
|
|
72
|
+
if value is None:
|
|
73
|
+
return "null"
|
|
74
|
+
|
|
75
|
+
if isinstance(value, bool):
|
|
76
|
+
return "true" if value else "false"
|
|
77
|
+
|
|
78
|
+
if isinstance(value, float) and not math.isfinite(value):
|
|
79
|
+
raise ValueError("JSONPath numeric value must be finite")
|
|
80
|
+
|
|
81
|
+
if isinstance(value, (int, str, float)):
|
|
82
|
+
return json.dumps(value)
|
|
83
|
+
|
|
84
|
+
raise TypeError(
|
|
85
|
+
f"Unsupported JSONPath literal type: {type(value).__name__}"
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def compile(self, end_node):
|
|
89
|
+
predicates, path, root = self.split_expression(end_node)
|
|
90
|
+
|
|
91
|
+
predicate_expr = self.compile_predicates(predicates)
|
|
92
|
+
path_expr = self.compile_path(path)
|
|
93
|
+
|
|
94
|
+
root_node_sql, root_params = self.compiler.compile(root)
|
|
95
|
+
|
|
96
|
+
jsonpath = f"{path_expr}?({predicate_expr})"
|
|
97
|
+
return f"{root_node_sql} @? %s::jsonpath", (*root_params, jsonpath)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from django.db.models import Lookup
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class JsonPathTerminalLookup(Lookup):
|
|
5
|
+
jsonpath_operator: str
|
|
6
|
+
|
|
7
|
+
def as_postgresql(self, compiler, connection, **extra_context):
|
|
8
|
+
from django_jsonpath.compiler import JsonPathSQLCompiler
|
|
9
|
+
return JsonPathSQLCompiler(compiler).compile(self)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class JsonPathExactLookup(JsonPathTerminalLookup):
|
|
13
|
+
lookup_name = "exact"
|
|
14
|
+
jsonpath_operator = "=="
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class JsonPathGtLookup(JsonPathTerminalLookup):
|
|
18
|
+
lookup_name = "gt"
|
|
19
|
+
jsonpath_operator = ">"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class JsonPathGteLookup(JsonPathTerminalLookup):
|
|
23
|
+
lookup_name = "gte"
|
|
24
|
+
jsonpath_operator = ">="
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class JsonPathLtLookup(JsonPathTerminalLookup):
|
|
28
|
+
lookup_name = "lt"
|
|
29
|
+
jsonpath_operator = "<"
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class JsonPathLteLookup(JsonPathTerminalLookup):
|
|
33
|
+
lookup_name = "lte"
|
|
34
|
+
jsonpath_operator = "<="
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
from django.db.models import Field, Transform
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class JSONPathField(Field):
|
|
5
|
+
|
|
6
|
+
def get_transform(self, lookup_name):
|
|
7
|
+
transform = super().get_transform(lookup_name)
|
|
8
|
+
if transform:
|
|
9
|
+
return transform
|
|
10
|
+
|
|
11
|
+
if lookup_name.isnumeric():
|
|
12
|
+
return JsonPathIndexTransformFactory(lookup_name)
|
|
13
|
+
|
|
14
|
+
return JsonPathKeyTransformFactory(lookup_name)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class JsonPathKeyTransform(Transform):
|
|
18
|
+
output_field = JSONPathField()
|
|
19
|
+
|
|
20
|
+
def __init__(self, key_name, *args, **kwargs):
|
|
21
|
+
self.key_name = key_name
|
|
22
|
+
super().__init__(*args, **kwargs)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class JsonPathKeyTransformFactory:
|
|
26
|
+
def __init__(self, key_name):
|
|
27
|
+
self.key_name = key_name
|
|
28
|
+
|
|
29
|
+
def __call__(self, *args, **kwargs):
|
|
30
|
+
return JsonPathKeyTransform(self.key_name, *args, **kwargs)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class JsonPathIndexTransform(Transform):
|
|
34
|
+
output_field = JSONPathField()
|
|
35
|
+
|
|
36
|
+
def __init__(self, key_name, *args, **kwargs):
|
|
37
|
+
self.key_name = key_name
|
|
38
|
+
super().__init__(*args, **kwargs)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class JsonPathIndexTransformFactory:
|
|
42
|
+
def __init__(self, key_name):
|
|
43
|
+
self.key_name = key_name
|
|
44
|
+
|
|
45
|
+
def __call__(self, *args, **kwargs):
|
|
46
|
+
return JsonPathIndexTransform(self.key_name, *args, **kwargs)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class JsonPathAnyTransform(Transform):
|
|
50
|
+
lookup_name = 'any'
|
|
51
|
+
output_field = JSONPathField()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class JsonPathTransform(Transform):
|
|
55
|
+
lookup_name = 'jsonpath'
|
|
56
|
+
output_field = JSONPathField()
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: django-jsonpath
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Django-native JSONPath support for queries through the ORM.
|
|
5
|
+
Author: marlizzz
|
|
6
|
+
Author-email: marlizzz <264201846+marlizzz@users.noreply.github.com>
|
|
7
|
+
Requires-Dist: django>=4.2
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
|
|
11
|
+
# django-jsonpath
|
|
12
|
+
|
|
13
|
+
> [!IMPORTANT]
|
|
14
|
+
> This project is in an early stage. Its API is experimental and may change before a stable release.
|
|
15
|
+
|
|
16
|
+
Django-native JSONPath support for queries through the ORM.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
Install the library with:
|
|
21
|
+
```bash
|
|
22
|
+
pip install django-jsonpath
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then add django-jsonpath to INSTALLED_APPS to your settings:
|
|
26
|
+
```python
|
|
27
|
+
INSTALLED_APPS = [
|
|
28
|
+
"django_jsonpath",
|
|
29
|
+
# ...
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
```
|
|
33
|
+
At this point jsonpath query expressions should be ready to use, just as regular Django query expressions.
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
This library adds a `__jsonpath` transform, which switches all the following transforms and lookups to jsonpath ones, not the regular json key transforms or terminal lookups.
|
|
38
|
+
|
|
39
|
+
Use Django JSONPath lookup syntax for JSON fields:
|
|
40
|
+
```python
|
|
41
|
+
Product.objects.filter(
|
|
42
|
+
payload__jsonpath__items__any__price__gt=100
|
|
43
|
+
)
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Any stands for [*] in JSONPath, so the above query translates roughly to
|
|
47
|
+
```sql
|
|
48
|
+
SELECT id, payload
|
|
49
|
+
FROM prototype_product
|
|
50
|
+
WHERE payload @? '$."items"[*]?(@."price" > 100)'::jsonpath
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Index transforms are also supported:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
Product.objects.filter(
|
|
57
|
+
payload__jsonpath__items__1__price__gt=100
|
|
58
|
+
)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Supported lookups
|
|
62
|
+
* exact (can be used as implicit one, i.e. `payload__jsonpath__number=5` and `payload__jsonpath__number__exact=5` are the same)
|
|
63
|
+
* gt
|
|
64
|
+
* gte
|
|
65
|
+
* lt
|
|
66
|
+
* lte
|
|
67
|
+
|
|
68
|
+
## Path and predicate semantics
|
|
69
|
+
The borderline between path expression and predicate expression lies at the point where `any` or index (`__0`) transform is used the last time, i.e. the rightmost usage of such a transform. In case no such transform is used, the boundary shifts left towards the column, e.g.:
|
|
70
|
+
```python
|
|
71
|
+
Product.objects.filter(
|
|
72
|
+
payload__jsonpath__item__position__price__gt=100
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Becomes
|
|
77
|
+
|
|
78
|
+
```sql
|
|
79
|
+
SELECT id, payload
|
|
80
|
+
FROM prototype_product
|
|
81
|
+
WHERE payload @? '$?(@."item"."position"."price" > 100)'::jsonpath
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Limitations
|
|
85
|
+
|
|
86
|
+
* `any` is a reserved word for `[*]` transform. A JSON object key named `"any"` cannot currently be addressed through the DSL.
|
|
87
|
+
* Integer transforms `__0` mean an array index, so key `"0"`, for example, would not be found
|
|
88
|
+
* Supported versions of Django are 4.2, 5.0, 5.1, 5.2, 6.0, 6.1.
|
|
89
|
+
* At this point, this library supports only PostgreSQL 12+ database.
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
This project is licensed under the BSD License. See the [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
django_jsonpath/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
django_jsonpath/apps.py,sha256=emaWBWPBEaehgUO7WXEGSCGKt9H-NmZv8e0tRFmbmiI,926
|
|
3
|
+
django_jsonpath/compiler.py,sha256=g4C-zWQXbN9X49Je-T_e5jfW38bBV1v2ifv6US2BT4A,3029
|
|
4
|
+
django_jsonpath/exceptions.py,sha256=paYvXINQg4xa-yes6xqY9rChJLfeqp0eAsF0aRQ8Kwg,48
|
|
5
|
+
django_jsonpath/lookup.py,sha256=5PbYKCy9A7ulivDKIhrICIMZwe1PeuaNsra6Z-9QEEs,814
|
|
6
|
+
django_jsonpath/transform.py,sha256=BCMxyaHU3ZDttEfsERyBUAywO8tUQ4JcDaa5I9YcVlQ,1423
|
|
7
|
+
django_jsonpath-0.1.0.dist-info/WHEEL,sha256=-unyBNsdNo6Y_XVO25zjvxasG_uUFN_fBd_kLyKE-Fk,81
|
|
8
|
+
django_jsonpath-0.1.0.dist-info/METADATA,sha256=IBK3Qgp4VImAEkBXPpIzDCP7iTC9aNZiok0KvCuEtsU,2653
|
|
9
|
+
django_jsonpath-0.1.0.dist-info/RECORD,,
|