Skip to content

Commit 3054ca4

Browse files
committed
perf: use isidentifier() instead of regex in _build_path
1 parent d80c353 commit 3054ca4

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

jsonpath/jsonpath.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ def _build_path(path: str, key) -> str:
212212
"""
213213
if isinstance(key, int):
214214
return f"{path}[{key}]"
215-
if JSONPath.REP_WORD_KEY.match(key):
215+
# Fast check: if all chars are word chars (alphanumeric + underscore)
216+
if key.isidentifier() or (key and key.replace("_", "a").isalnum()):
216217
return f"{path}.{key}"
217218
return f"{path}['{key}']"
218219

0 commit comments

Comments
 (0)