jsonpath 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/lib/jsonpath/enumerable.rb +9 -2
- data/lib/jsonpath/parser.rb +10 -22
- data/lib/jsonpath/version.rb +1 -1
- data/test/test_jsonpath.rb +25 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dc2dc97587acd71d6ac7b1d5a2cb178f1ee9938823ef924c12e2213adafea1a
|
4
|
+
data.tar.gz: bc9b02cbc4c0a68329c9a682ed344b544028e27ba3f63f5079824cd536192792
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a8834f4fdcd9d1a3ba41f9431297ce87b3cf8da93ef33c0f9cd69f4c3649d1948aa32de0184f00dd157497a5ef5ec459fcfdd6be8987480b337384fcbeb4bd16
|
7
|
+
data.tar.gz: 3e2370e1114d041f108804e33b409e21884d2ebf21d2745fd94b81ce054646538e2f5c4d5889109f166ce8fed1d2f522e02298608bc9e766cce163b690f88f23
|
data/.travis.yml
CHANGED
data/lib/jsonpath/enumerable.rb
CHANGED
@@ -12,7 +12,12 @@ class JsonPath
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def each(context = @object, key = nil, pos = 0, &blk)
|
15
|
-
node =
|
15
|
+
node =
|
16
|
+
if key
|
17
|
+
context.is_a?(Hash) || context.is_a?(Array) ? context[key] : context.__send__(key)
|
18
|
+
else
|
19
|
+
context
|
20
|
+
end
|
16
21
|
@_current_node = node
|
17
22
|
return yield_value(blk, context, key) if pos == @path.size
|
18
23
|
|
@@ -39,10 +44,12 @@ class JsonPath
|
|
39
44
|
expr[1, expr.size - 2].split(',').each do |sub_path|
|
40
45
|
case sub_path[0]
|
41
46
|
when '\'', '"'
|
47
|
+
k = sub_path[1, sub_path.size - 2]
|
42
48
|
if node.is_a?(Hash)
|
43
|
-
k = sub_path[1, sub_path.size - 2]
|
44
49
|
node[k] ||= nil if @options[:default_path_leaf_to_null]
|
45
50
|
each(node, k, pos + 1, &blk) if node.key?(k)
|
51
|
+
elsif node.respond_to?(k.to_s)
|
52
|
+
each(node, k, pos + 1, &blk)
|
46
53
|
end
|
47
54
|
when '?'
|
48
55
|
handle_question_mark(sub_path, node, pos, &blk)
|
data/lib/jsonpath/parser.rb
CHANGED
@@ -53,15 +53,9 @@ class JsonPath
|
|
53
53
|
scanner = StringScanner.new(exp)
|
54
54
|
elements = []
|
55
55
|
until scanner.eos?
|
56
|
-
if scanner.scan(
|
57
|
-
|
58
|
-
|
59
|
-
num = scanner.scan(/\d+/)
|
60
|
-
return @_current_node.send(sym.to_sym).send(op.to_sym, num.to_i)
|
61
|
-
end
|
62
|
-
if t = scanner.scan(/\['[a-zA-Z@&\*\/\$%\^\?_]+'\]+/)
|
63
|
-
elements << t.gsub(/\[|\]|'|\s+/, '')
|
64
|
-
elsif t = scanner.scan(/(\s+)?[<>=][=~]?(\s+)?/)
|
56
|
+
if t = scanner.scan(/\['[a-zA-Z@&\*\/\$%\^\?_]+'\]|\.[a-zA-Z0-9_]+[?!]?/)
|
57
|
+
elements << t.gsub(/[\[\]'\.]|\s+/, '')
|
58
|
+
elsif t = scanner.scan(/(\s+)?[<>=!\-+][=~]?(\s+)?/)
|
65
59
|
operator = t
|
66
60
|
elsif t = scanner.scan(/(\s+)?'?.*'?(\s+)?/)
|
67
61
|
# If we encounter a node which does not contain `'` it means
|
@@ -81,11 +75,15 @@ class JsonPath
|
|
81
75
|
|
82
76
|
el = if elements.empty?
|
83
77
|
@_current_node
|
78
|
+
elsif @_current_node.is_a?(Hash)
|
79
|
+
@_current_node.dig(*elements)
|
84
80
|
else
|
85
|
-
|
81
|
+
elements.inject(@_current_node) do |agg, key|
|
82
|
+
agg.__send__(key)
|
83
|
+
end
|
86
84
|
end
|
87
|
-
|
88
|
-
return true if
|
85
|
+
|
86
|
+
return (el ? true : false) if el.nil? || operator.nil?
|
89
87
|
|
90
88
|
el = Float(el) rescue el
|
91
89
|
operand = Float(operand) rescue operand
|
@@ -95,16 +93,6 @@ class JsonPath
|
|
95
93
|
|
96
94
|
private
|
97
95
|
|
98
|
-
# @TODO: Remove this once JsonPath no longer supports ruby versions below 2.3
|
99
|
-
def dig(keys, hash)
|
100
|
-
return nil unless hash.is_a? Hash
|
101
|
-
return nil unless hash.key?(keys.first)
|
102
|
-
return hash.fetch(keys.first) if keys.size == 1
|
103
|
-
|
104
|
-
prev = keys.shift
|
105
|
-
dig(keys, hash.fetch(prev))
|
106
|
-
end
|
107
|
-
|
108
96
|
# This will break down a parenthesis from the left to the right
|
109
97
|
# and replace the given expression with it's returned value.
|
110
98
|
# It does this in order to make it easy to eliminate groups
|
data/lib/jsonpath/version.rb
CHANGED
data/test/test_jsonpath.rb
CHANGED
@@ -70,6 +70,12 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
70
70
|
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object)
|
71
71
|
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] == 9)]").on(@object)
|
72
72
|
assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object)
|
73
|
+
assert_equal [
|
74
|
+
@object['store']['book'][0],
|
75
|
+
@object['store']['book'][4],
|
76
|
+
@object['store']['book'][5],
|
77
|
+
@object['store']['book'][6]
|
78
|
+
], JsonPath.new("$..book[?(@['category'] != 'fiction')]").on(@object)
|
73
79
|
end
|
74
80
|
|
75
81
|
def test_or_operator
|
@@ -113,6 +119,25 @@ class TestJsonpath < MiniTest::Unit::TestCase
|
|
113
119
|
assert_equal [@object['store']['bicycle']['2seater']], JsonPath.new('$.store.bicycle.2seater').on(@object)
|
114
120
|
end
|
115
121
|
|
122
|
+
def test_recognized_dot_notation_in_filters
|
123
|
+
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[?(@.isbn)]').on(@object)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_works_on_non_hash
|
127
|
+
klass = Struct.new(:a, :b)
|
128
|
+
object = klass.new('some', 'value')
|
129
|
+
|
130
|
+
assert_equal ['value'], JsonPath.new('$.b').on(object)
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_works_on_non_hash_with_filters
|
134
|
+
klass = Struct.new(:a, :b)
|
135
|
+
first_object = klass.new('some', 'value')
|
136
|
+
second_object = klass.new('next', 'other value')
|
137
|
+
|
138
|
+
assert_equal ['other value'], JsonPath.new('$[?(@.a == "next")].b').on([first_object, second_object])
|
139
|
+
end
|
140
|
+
|
116
141
|
def test_recognize_array_with_evald_index
|
117
142
|
assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-5)]').on(@object)
|
118
143
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonpath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-05-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
@@ -158,8 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
158
158
|
- !ruby/object:Gem::Version
|
159
159
|
version: '0'
|
160
160
|
requirements: []
|
161
|
-
|
162
|
-
rubygems_version: 2.7.8
|
161
|
+
rubygems_version: 3.0.2
|
163
162
|
signing_key:
|
164
163
|
specification_version: 4
|
165
164
|
summary: Ruby implementation of http://goessner.net/articles/JsonPath/
|