| 1 | @none_guard
|
|---|
| 2 | def _sqlite_json_contains(haystack, needle):
|
|---|
| 3 | if isinstance(haystack, str):
|
|---|
| 4 | try:
|
|---|
| 5 | target = json.loads(haystack)
|
|---|
| 6 | except json.JSONDecodeError:
|
|---|
| 7 | target = haystack
|
|---|
| 8 | else:
|
|---|
| 9 | target = haystack
|
|---|
| 10 | if isinstance(needle, str):
|
|---|
| 11 | try:
|
|---|
| 12 | candidate = json.loads(needle)
|
|---|
| 13 | except json.JSONDecodeError:
|
|---|
| 14 | candidate = needle
|
|---|
| 15 | else:
|
|---|
| 16 | candidate = needle
|
|---|
| 17 | if isinstance(target, dict) and isinstance(candidate, dict):
|
|---|
| 18 | if target.items() >= candidate.items():
|
|---|
| 19 | return True
|
|---|
| 20 | for key, value in candidate.items():
|
|---|
| 21 | if key in target:
|
|---|
| 22 | if not _sqlite_json_contains(target[key], value):
|
|---|
| 23 | return False
|
|---|
| 24 | else:
|
|---|
| 25 | return False
|
|---|
| 26 | return True
|
|---|
| 27 | if isinstance(target, list):
|
|---|
| 28 | if isinstance(candidate, list):
|
|---|
| 29 | try:
|
|---|
| 30 | # When possible, use superset checking for better performance.
|
|---|
| 31 | return set(target).issuperset(candidate)
|
|---|
| 32 | except TypeError:
|
|---|
| 33 | # Superset checking may not be possible, e.g. with nested lists.
|
|---|
| 34 | return all(c in target for c in candidate)
|
|---|
| 35 | return candidate in target
|
|---|
| 36 | return target == candidate
|
|---|