diff options
author | Kenn Knowles <kenn.knowles@gmail.com> | 2013-06-14 18:07:45 -0400 |
---|---|---|
committer | Kenn Knowles <kenn.knowles@gmail.com> | 2013-06-14 18:07:45 -0400 |
commit | 27cdaad57dbe2941b671cadbf5195e22193ef1c7 (patch) | |
tree | 0d645f262e52bedfbb7329bbfdb32d9f184b6c0e | |
parent | f4281ba927238bd5cbf356482c9dbf4cb328cddd (diff) | |
download | jsonpath-rw-27cdaad57dbe2941b671cadbf5195e22193ef1c7.tar.gz |
Add support for `parent` named operator
-rw-r--r-- | jsonpath_rw/jsonpath.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/jsonpath_rw/jsonpath.py b/jsonpath_rw/jsonpath.py index 8af0e87..7e45cb9 100644 --- a/jsonpath_rw/jsonpath.py +++ b/jsonpath_rw/jsonpath.py @@ -236,6 +236,27 @@ class Child(JSONPath): def __repr__(self): return '%s(%r, %r)' % (self.__class__.__name__, self.left, self.right) +class Parent(JSONPath): + """ + JSONPath that matches the parent node of the current match. + Will crash if no such parent exists. + Available via named operator `parent`. + """ + + def find(self, datum): + datum = DatumInContext.wrap(datum) + return [datum.context] + + def __eq__(self, other): + return isinstance(other, Parent) + + def __str__(self): + return '`parent`' + + def __repr__(self): + return 'Parent()' + + class Where(JSONPath): """ JSONPath that first matches the left, and then |