diff --git a/lib/jsonpath/parser.rb b/lib/jsonpath/parser.rb index 84e0c47..94a28d1 100644 --- a/lib/jsonpath/parser.rb +++ b/lib/jsonpath/parser.rb @@ -99,6 +99,7 @@ def parse_exp(exp) elements.inject(@_current_node, &:__send__) end + return !el if operator&.strip == '!' && operand.nil? return (el ? true : false) if el.nil? || operator.nil? el = Float(el) rescue el diff --git a/lib/jsonpath/version.rb b/lib/jsonpath/version.rb index db0675c..8d16028 100644 --- a/lib/jsonpath/version.rb +++ b/lib/jsonpath/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class JsonPath - VERSION = '1.1.5' + VERSION = '1.1.6' end diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 80fa0bb..30cffba 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -67,6 +67,7 @@ def test_recognize_array_comma def test_recognize_filters assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object) + assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?(!@['isbn'])]").on(@object) assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object) assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] == 9)]").on(@object) assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object) @@ -157,6 +158,7 @@ def test_paths_with_numbers def test_recognized_dot_notation_in_filters assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[?(@.isbn)]').on(@object) + assert_equal [@object['store']['book'][0], @object['store']['book'][1], @object['store']['book'][4], @object['store']['book'][5], @object['store']['book'][6]], JsonPath.new("$..book[?(!@.isbn)]").on(@object) end def test_works_on_non_hash