jsonpath 0.9.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +33 -0
- data/.gitignore +5 -0
- data/.rubocop_todo.yml +33 -18
- data/Gemfile +1 -1
- data/README.md +173 -32
- data/jsonpath.gemspec +2 -9
- data/lib/jsonpath/dig.rb +57 -0
- data/lib/jsonpath/enumerable.rb +53 -20
- data/lib/jsonpath/parser.rb +176 -40
- data/lib/jsonpath/proxy.rb +12 -2
- data/lib/jsonpath/version.rb +1 -1
- data/lib/jsonpath.rb +79 -16
- data/test/test_jsonpath.rb +666 -9
- data/test/test_readme.rb +117 -0
- metadata +25 -25
- data/.travis.yml +0 -6
data/test/test_readme.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'phocus'
|
5
|
+
require 'jsonpath'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class TestJsonpathReadme < MiniTest::Unit::TestCase
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@json = <<-HERE_DOC
|
12
|
+
{"store":
|
13
|
+
{"bicycle":
|
14
|
+
{"price":19.95, "color":"red"},
|
15
|
+
"book":[
|
16
|
+
{"price":8.95, "category":"reference", "title":"Sayings of the Century", "author":"Nigel Rees"},
|
17
|
+
{"price":12.99, "category":"fiction", "title":"Sword of Honour", "author":"Evelyn Waugh"},
|
18
|
+
{"price":8.99, "category":"fiction", "isbn":"0-553-21311-3", "title":"Moby Dick", "author":"Herman Melville","color":"blue"},
|
19
|
+
{"price":22.99, "category":"fiction", "isbn":"0-395-19395-8", "title":"The Lord of the Rings", "author":"Tolkien"}
|
20
|
+
]
|
21
|
+
}
|
22
|
+
}
|
23
|
+
HERE_DOC
|
24
|
+
end
|
25
|
+
attr_reader :json
|
26
|
+
|
27
|
+
def test_library_section
|
28
|
+
path = JsonPath.new('$..price')
|
29
|
+
assert_equal [19.95, 8.95, 12.99, 8.99, 22.99], path.on(json)
|
30
|
+
assert_equal [18.88], path.on('{"books":[{"title":"A Tale of Two Somethings","price":18.88}]}')
|
31
|
+
assert_equal ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "Tolkien"], JsonPath.on(json, '$..author')
|
32
|
+
assert_equal [
|
33
|
+
{"price" => 8.95, "category" => "reference", "title" => "Sayings of the Century", "author" => "Nigel Rees"},
|
34
|
+
{"price" => 8.99, "category" => "fiction", "isbn" => "0-553-21311-3", "title" => "Moby Dick", "author" => "Herman Melville","color" => "blue"},
|
35
|
+
], JsonPath.new('$..book[::2]').on(json)
|
36
|
+
assert_equal [8.95, 8.99], JsonPath.new("$..price[?(@ < 10)]").on(json)
|
37
|
+
assert_equal ["Sayings of the Century", "Moby Dick"], JsonPath.new("$..book[?(@['price'] == 8.95 || @['price'] == 8.99)].title").on(json)
|
38
|
+
assert_equal [], JsonPath.new("$..book[?(@['price'] == 8.95 && @['price'] == 8.99)].title").on(json)
|
39
|
+
assert_equal "red", JsonPath.new('$..color').first(json)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_library_section_enumerable
|
43
|
+
enum = JsonPath.new('$..color')[json]
|
44
|
+
assert_equal "red", enum.first
|
45
|
+
assert enum.any?{ |c| c == 'red' }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_ruby_structures_section
|
49
|
+
book = { title: "Sayings of the Century" }
|
50
|
+
assert_equal [], JsonPath.new('$.title').on(book)
|
51
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title', use_symbols: true).on(book)
|
52
|
+
|
53
|
+
book_class = Struct.new(:title)
|
54
|
+
book = book_class.new("Sayings of the Century")
|
55
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title').on(book)
|
56
|
+
|
57
|
+
book_class = Class.new{ attr_accessor :title }
|
58
|
+
book = book_class.new
|
59
|
+
book.title = "Sayings of the Century"
|
60
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title', allow_send: true).on(book)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_options_section
|
64
|
+
assert_equal ["0-553-21311-3", "0-395-19395-8"], JsonPath.new('$..book[*].isbn').on(json)
|
65
|
+
assert_equal [nil, nil, "0-553-21311-3", "0-395-19395-8"], JsonPath.new('$..book[*].isbn', default_path_leaf_to_null: true).on(json)
|
66
|
+
|
67
|
+
assert_equal ["price", "category", "title", "author"], JsonPath.new('$..book[0]').on(json).map(&:keys).flatten.uniq
|
68
|
+
assert_equal [:price, :category, :title, :author], JsonPath.new('$..book[0]').on(json, symbolize_keys: true).map(&:keys).flatten.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
def selecting_value_section
|
72
|
+
json = <<-HERE_DOC
|
73
|
+
{
|
74
|
+
"store": {
|
75
|
+
"book": [
|
76
|
+
{
|
77
|
+
"category": "reference",
|
78
|
+
"author": "Nigel Rees",
|
79
|
+
"title": "Sayings of the Century",
|
80
|
+
"price": 8.95
|
81
|
+
},
|
82
|
+
{
|
83
|
+
"category": "fiction",
|
84
|
+
"author": "Evelyn Waugh",
|
85
|
+
"title": "Sword of Honour",
|
86
|
+
"price": 12.99
|
87
|
+
}
|
88
|
+
]
|
89
|
+
}
|
90
|
+
HERE_DOC
|
91
|
+
got = JsonPath.on(json, "$.store.book[*](category,author)")
|
92
|
+
expected = [
|
93
|
+
{
|
94
|
+
"category" => "reference",
|
95
|
+
"author" => "Nigel Rees"
|
96
|
+
},
|
97
|
+
{
|
98
|
+
"category" => "fiction",
|
99
|
+
"author" => "Evelyn Waugh"
|
100
|
+
}
|
101
|
+
]
|
102
|
+
assert_equal expected, got
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_manipulation_section
|
106
|
+
assert_equal({"candy" => "big turks"}, JsonPath.for('{"candy":"lollipop"}').gsub('$..candy') {|v| "big turks" }.to_hash)
|
107
|
+
|
108
|
+
json = '{"candy":"lollipop","noncandy":null,"other":"things"}'
|
109
|
+
o = JsonPath.for(json).
|
110
|
+
gsub('$..candy') {|v| "big turks" }.
|
111
|
+
compact.
|
112
|
+
delete('$..other').
|
113
|
+
to_hash
|
114
|
+
assert_equal({"candy" => "big turks"}, o)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonpath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
8
8
|
- Gergely Brautigam
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-10-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: multi_json
|
@@ -25,20 +25,6 @@ dependencies:
|
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
version: '0'
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: to_regexp
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - "~>"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: 0.2.1
|
35
|
-
type: :runtime
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: 0.2.1
|
42
28
|
- !ruby/object:Gem::Dependency
|
43
29
|
name: bundler
|
44
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,6 +81,20 @@ dependencies:
|
|
95
81
|
- - ">="
|
96
82
|
- !ruby/object:Gem::Version
|
97
83
|
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: racc
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
98
|
- !ruby/object:Gem::Dependency
|
99
99
|
name: rake
|
100
100
|
requirement: !ruby/object:Gem::Requirement
|
@@ -120,11 +120,11 @@ extra_rdoc_files:
|
|
120
120
|
- README.md
|
121
121
|
files:
|
122
122
|
- ".gemtest"
|
123
|
+
- ".github/workflows/test.yml"
|
123
124
|
- ".gitignore"
|
124
125
|
- ".rspec"
|
125
126
|
- ".rubocop.yml"
|
126
127
|
- ".rubocop_todo.yml"
|
127
|
-
- ".travis.yml"
|
128
128
|
- Gemfile
|
129
129
|
- LICENSE.md
|
130
130
|
- README.md
|
@@ -132,35 +132,35 @@ files:
|
|
132
132
|
- bin/jsonpath
|
133
133
|
- jsonpath.gemspec
|
134
134
|
- lib/jsonpath.rb
|
135
|
+
- lib/jsonpath/dig.rb
|
135
136
|
- lib/jsonpath/enumerable.rb
|
136
137
|
- lib/jsonpath/parser.rb
|
137
138
|
- lib/jsonpath/proxy.rb
|
138
139
|
- lib/jsonpath/version.rb
|
139
140
|
- test/test_jsonpath.rb
|
140
141
|
- test/test_jsonpath_bin.rb
|
142
|
+
- test/test_readme.rb
|
141
143
|
homepage: https://github.com/joshbuddy/jsonpath
|
142
144
|
licenses:
|
143
145
|
- MIT
|
144
146
|
metadata: {}
|
145
|
-
post_install_message:
|
146
|
-
rdoc_options:
|
147
|
-
- "--charset=UTF-8"
|
147
|
+
post_install_message:
|
148
|
+
rdoc_options: []
|
148
149
|
require_paths:
|
149
150
|
- lib
|
150
151
|
required_ruby_version: !ruby/object:Gem::Requirement
|
151
152
|
requirements:
|
152
153
|
- - ">="
|
153
154
|
- !ruby/object:Gem::Version
|
154
|
-
version: '
|
155
|
+
version: '2.6'
|
155
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
156
157
|
requirements:
|
157
158
|
- - ">="
|
158
159
|
- !ruby/object:Gem::Version
|
159
160
|
version: '0'
|
160
161
|
requirements: []
|
161
|
-
|
162
|
-
|
163
|
-
signing_key:
|
162
|
+
rubygems_version: 3.4.1
|
163
|
+
signing_key:
|
164
164
|
specification_version: 4
|
165
165
|
summary: Ruby implementation of http://goessner.net/articles/JsonPath/
|
166
166
|
test_files: []
|