jsonpath 0.3.1 → 0.3.2
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.
- data/README.md +15 -4
- data/Rakefile +18 -16
- data/jsonpath.gemspec +4 -2
- data/lib/jsonpath/version.rb +1 -1
- data/test/test_jsonpath.rb +101 -0
- metadata +32 -20
- data/spec/jsonpath_spec.rb +0 -105
- data/spec/spec_helper.rb +0 -2
data/README.md
CHANGED
@@ -13,7 +13,8 @@ There is stand-alone usage through the binary `jsonpath`
|
|
13
13
|
|
14
14
|
As well, you can include it as a library.
|
15
15
|
|
16
|
-
|
16
|
+
~~~~~ {ruby}
|
17
|
+
object = JSON.parse(<<-HERE_DOC)
|
17
18
|
{"store":
|
18
19
|
{"bicycle":
|
19
20
|
{"price":19.95, "color":"red"},
|
@@ -27,11 +28,21 @@ As well, you can include it as a library.
|
|
27
28
|
}
|
28
29
|
HERE_DOC
|
29
30
|
|
30
|
-
JsonPath.new('$..price').on(object)
|
31
|
+
JsonPath.new('$..price').on(object)
|
31
32
|
# => [19.95, 8.95, 12.99, 8.99, 22.99]
|
32
33
|
|
33
|
-
JsonPath.
|
34
|
+
JsonPath.on(object, '$..author')
|
34
35
|
# => ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"]
|
35
36
|
|
36
|
-
JsonPath.new('$..book[::2]').on(object)
|
37
|
+
JsonPath.new('$..book[::2]').on(object)
|
37
38
|
# => [{"price"=>8.95, "category"=>"reference", "author"=>"Nigel Rees", "title"=>"Sayings of the Century"}, {"price"=>8.99, "category"=>"fiction", "author"=>"Herman Melville", "title"=>"Moby Dick", "isbn"=>"0-553-21311-3"}]
|
39
|
+
|
40
|
+
JsonPath.new('$..color').first(object)
|
41
|
+
# => "red"
|
42
|
+
|
43
|
+
# Lazy enumeration - only needs to find the first two matches
|
44
|
+
JsonPath.new('$..price').enum_on(object).each do |match|
|
45
|
+
break price if price < 15.0
|
46
|
+
end
|
47
|
+
# => 8.95
|
48
|
+
~~~~~
|
data/Rakefile
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
Bundler::GemHelper.install_tasks
|
3
|
-
|
4
|
-
require 'rake/rdoctask'
|
5
|
-
desc "Generate documentation"
|
6
|
-
Rake::RDocTask.new do |rd|
|
7
|
-
rd.main = "README.rdoc"
|
8
|
-
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
9
|
-
rd.rdoc_dir = 'rdoc'
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
desc "Generate documentation"
|
6
|
+
Rake::RDocTask.new do |rd|
|
7
|
+
rd.main = "README.rdoc"
|
8
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
9
|
+
rd.rdoc_dir = 'rdoc'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :test do
|
13
|
+
$: << 'lib'
|
14
|
+
require 'minitest/autorun'
|
15
|
+
require 'phocus'
|
16
|
+
require 'jsonpath'
|
17
|
+
Dir['./test/**/test_*.rb'].each { |test| require test }
|
18
|
+
end
|
data/jsonpath.gemspec
CHANGED
@@ -18,12 +18,14 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.rubygems_version = %q{1.3.7}
|
19
19
|
s.test_files = `git ls-files`.split("\n").select{|f| f =~ /^spec/}
|
20
20
|
s.rubyforge_project = 'jsonpath'
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
22
|
|
22
23
|
# dependencies
|
23
24
|
s.add_runtime_dependency 'json'
|
24
25
|
s.add_development_dependency 'code_stats'
|
25
26
|
s.add_development_dependency 'rake'
|
26
|
-
s.add_development_dependency '
|
27
|
-
s.add_development_dependency '
|
27
|
+
s.add_development_dependency 'minitest', '~> 2.2.0'
|
28
|
+
s.add_development_dependency 'phocus'
|
29
|
+
s.add_development_dependency 'bundler', '~> 1.0.10'
|
28
30
|
end
|
29
31
|
|
data/lib/jsonpath/version.rb
CHANGED
@@ -0,0 +1,101 @@
|
|
1
|
+
class TestJsonpath < MiniTest::Unit::TestCase
|
2
|
+
|
3
|
+
def setup
|
4
|
+
@object = { "store"=> {
|
5
|
+
"book" => [
|
6
|
+
{ "category"=> "reference",
|
7
|
+
"author"=> "Nigel Rees",
|
8
|
+
"title"=> "Sayings of the Century",
|
9
|
+
"price"=> 8.95
|
10
|
+
},
|
11
|
+
{ "category"=> "fiction",
|
12
|
+
"author"=> "Evelyn Waugh",
|
13
|
+
"title"=> "Sword of Honour",
|
14
|
+
"price"=> 12.99
|
15
|
+
},
|
16
|
+
{ "category"=> "fiction",
|
17
|
+
"author"=> "Herman Melville",
|
18
|
+
"title"=> "Moby Dick",
|
19
|
+
"isbn"=> "0-553-21311-3",
|
20
|
+
"price"=> 8.99
|
21
|
+
},
|
22
|
+
{ "category"=> "fiction",
|
23
|
+
"author"=> "J. R. R. Tolkien",
|
24
|
+
"title"=> "The Lord of the Rings",
|
25
|
+
"isbn"=> "0-395-19395-8",
|
26
|
+
"price"=> 22.99
|
27
|
+
}
|
28
|
+
],
|
29
|
+
"bicycle"=> {
|
30
|
+
"color"=> "red",
|
31
|
+
"price"=> 19.95,
|
32
|
+
"catalogue_number" => 12345,
|
33
|
+
} } }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_lookup_direct_path
|
37
|
+
assert_equal 4, JsonPath.new('$.store.*').on(@object).to_a.first['book'].size
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_retrieve_all_authors
|
41
|
+
assert_equal [
|
42
|
+
@object['store']['book'][0]['author'],
|
43
|
+
@object['store']['book'][1]['author'],
|
44
|
+
@object['store']['book'][2]['author'],
|
45
|
+
@object['store']['book'][3]['author']
|
46
|
+
], JsonPath.new('$..author').on(@object)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_retrieve_all_prices
|
50
|
+
assert_equal [
|
51
|
+
@object['store']['bicycle']['price'],
|
52
|
+
@object['store']['book'][0]['price'],
|
53
|
+
@object['store']['book'][1]['price'],
|
54
|
+
@object['store']['book'][2]['price'],
|
55
|
+
@object['store']['book'][3]['price']
|
56
|
+
].sort, JsonPath.new('$..price').on(@object).sort
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_recognize_array_splices
|
60
|
+
assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0:1:1]').on(@object)
|
61
|
+
assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new('$..book[1::2]').on(@object)
|
62
|
+
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[::2]').on(@object)
|
63
|
+
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[:-2:2]').on(@object)
|
64
|
+
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2::]').on(@object)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_recognize_array_comma
|
68
|
+
assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0,1]').on(@object)
|
69
|
+
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2,-1::]').on(@object)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_recognize_filters
|
73
|
+
assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object)
|
74
|
+
assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_paths_with_underscores
|
78
|
+
assert_equal [@object['store']['bicycle']['catalogue_number']], JsonPath.new('$.store.bicycle.catalogue_number').on(@object)
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_recognize_array_with_evald_index
|
82
|
+
assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-2)]').on(@object)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_use_first
|
86
|
+
assert_equal @object['store']['book'][2], JsonPath.new('$..book[(@.length-2)]').first(@object)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_counting
|
90
|
+
assert_equal 29, JsonPath.new('$..*').on(@object).to_a.size
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_space_in_path
|
94
|
+
assert_equal ['e'], JsonPath.new("$.'c d'").on({"a" => "a","b" => "b", "c d" => "e"})
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_class_method
|
98
|
+
assert_equal JsonPath.new('$..author').on(@object), JsonPath.on(@object, '$..author')
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonpath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 2
|
10
|
+
version: 0.3.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Joshua Hull
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-06-
|
18
|
+
date: 2011-06-07 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -61,41 +61,55 @@ dependencies:
|
|
61
61
|
type: :development
|
62
62
|
version_requirements: *id003
|
63
63
|
- !ruby/object:Gem::Dependency
|
64
|
-
name:
|
64
|
+
name: minitest
|
65
65
|
prerelease: false
|
66
66
|
requirement: &id004 !ruby/object:Gem::Requirement
|
67
67
|
none: false
|
68
68
|
requirements:
|
69
69
|
- - ~>
|
70
70
|
- !ruby/object:Gem::Version
|
71
|
-
hash:
|
71
|
+
hash: 7
|
72
72
|
segments:
|
73
73
|
- 2
|
74
|
-
-
|
74
|
+
- 2
|
75
75
|
- 0
|
76
|
-
version: 2.
|
76
|
+
version: 2.2.0
|
77
77
|
type: :development
|
78
78
|
version_requirements: *id004
|
79
79
|
- !ruby/object:Gem::Dependency
|
80
|
-
name:
|
80
|
+
name: phocus
|
81
81
|
prerelease: false
|
82
82
|
requirement: &id005 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id005
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: bundler
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
83
97
|
none: false
|
84
98
|
requirements:
|
85
99
|
- - ~>
|
86
100
|
- !ruby/object:Gem::Version
|
87
|
-
hash:
|
101
|
+
hash: 3
|
88
102
|
segments:
|
89
103
|
- 1
|
90
104
|
- 0
|
91
|
-
-
|
92
|
-
version: 1.0.
|
105
|
+
- 10
|
106
|
+
version: 1.0.10
|
93
107
|
type: :development
|
94
|
-
version_requirements: *
|
108
|
+
version_requirements: *id006
|
95
109
|
description: Ruby implementation of http://goessner.net/articles/JsonPath/.
|
96
110
|
email: joshbuddy@gmail.com
|
97
|
-
executables:
|
98
|
-
|
111
|
+
executables:
|
112
|
+
- jsonpath
|
99
113
|
extensions: []
|
100
114
|
|
101
115
|
extra_rdoc_files:
|
@@ -112,8 +126,7 @@ files:
|
|
112
126
|
- lib/jsonpath.rb
|
113
127
|
- lib/jsonpath/enumerable.rb
|
114
128
|
- lib/jsonpath/version.rb
|
115
|
-
-
|
116
|
-
- spec/spec_helper.rb
|
129
|
+
- test/test_jsonpath.rb
|
117
130
|
has_rdoc: true
|
118
131
|
homepage: http://github.com/joshbuddy/jsonpath
|
119
132
|
licenses: []
|
@@ -148,6 +161,5 @@ rubygems_version: 1.6.2
|
|
148
161
|
signing_key:
|
149
162
|
specification_version: 3
|
150
163
|
summary: Ruby implementation of http://goessner.net/articles/JsonPath/
|
151
|
-
test_files:
|
152
|
-
|
153
|
-
- spec/spec_helper.rb
|
164
|
+
test_files: []
|
165
|
+
|
data/spec/jsonpath_spec.rb
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "JsonPath" do
|
4
|
-
|
5
|
-
before(:each) do
|
6
|
-
@object = { "store"=> {
|
7
|
-
"book" => [
|
8
|
-
{ "category"=> "reference",
|
9
|
-
"author"=> "Nigel Rees",
|
10
|
-
"title"=> "Sayings of the Century",
|
11
|
-
"price"=> 8.95
|
12
|
-
},
|
13
|
-
{ "category"=> "fiction",
|
14
|
-
"author"=> "Evelyn Waugh",
|
15
|
-
"title"=> "Sword of Honour",
|
16
|
-
"price"=> 12.99
|
17
|
-
},
|
18
|
-
{ "category"=> "fiction",
|
19
|
-
"author"=> "Herman Melville",
|
20
|
-
"title"=> "Moby Dick",
|
21
|
-
"isbn"=> "0-553-21311-3",
|
22
|
-
"price"=> 8.99
|
23
|
-
},
|
24
|
-
{ "category"=> "fiction",
|
25
|
-
"author"=> "J. R. R. Tolkien",
|
26
|
-
"title"=> "The Lord of the Rings",
|
27
|
-
"isbn"=> "0-395-19395-8",
|
28
|
-
"price"=> 22.99
|
29
|
-
}
|
30
|
-
],
|
31
|
-
"bicycle"=> {
|
32
|
-
"color"=> "red",
|
33
|
-
"price"=> 19.95,
|
34
|
-
"catalogue_number" => 12345,
|
35
|
-
}
|
36
|
-
}
|
37
|
-
}
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should lookup a direct path" do
|
41
|
-
JsonPath.new('$.store.*').on(@object).to_a.first['book'].size.should == 4
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should retrieve all authors" do
|
45
|
-
JsonPath.new('$..author').on(@object).to_a.should == [
|
46
|
-
@object['store']['book'][0]['author'],
|
47
|
-
@object['store']['book'][1]['author'],
|
48
|
-
@object['store']['book'][2]['author'],
|
49
|
-
@object['store']['book'][3]['author']
|
50
|
-
]
|
51
|
-
end
|
52
|
-
|
53
|
-
it "should retrieve all prices" do
|
54
|
-
JsonPath.new('$..price').on(@object).to_a.sort.should == [
|
55
|
-
@object['store']['bicycle']['price'],
|
56
|
-
@object['store']['book'][0]['price'],
|
57
|
-
@object['store']['book'][1]['price'],
|
58
|
-
@object['store']['book'][2]['price'],
|
59
|
-
@object['store']['book'][3]['price']
|
60
|
-
].sort
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should recognize all types of array splices" do
|
64
|
-
JsonPath.new('$..book[0:1:1]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][1]]
|
65
|
-
JsonPath.new('$..book[1::2]').on(@object).to_a.should == [@object['store']['book'][1], @object['store']['book'][3]]
|
66
|
-
JsonPath.new('$..book[::2]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
|
67
|
-
JsonPath.new('$..book[:-2:2]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
|
68
|
-
JsonPath.new('$..book[2::]').on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should recognize array comma syntax" do
|
72
|
-
JsonPath.new('$..book[0,1]').on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][1]]
|
73
|
-
JsonPath.new('$..book[2,-1::]').on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should support filters" do
|
77
|
-
JsonPath.new("$..book[?(@['isbn'])]").on(@object).to_a.should == [@object['store']['book'][2], @object['store']['book'][3]]
|
78
|
-
JsonPath.new("$..book[?(@['price'] < 10)]").on(@object).to_a.should == [@object['store']['book'][0], @object['store']['book'][2]]
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should recognize paths with underscores" do
|
82
|
-
JsonPath.new('$.store.bicycle.catalogue_number').on(@object).to_a.should == [@object['store']['bicycle']['catalogue_number']]
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should support eval'd array indicies" do
|
86
|
-
JsonPath.new('$..book[(@.length-2)]').on(@object).to_a.should == [@object['store']['book'][2]]
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should support first" do
|
90
|
-
JsonPath.new('$..book[(@.length-2)]').first(@object) == @object['store']['book'][2]
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should correct retrieve the right number of all nodes" do
|
94
|
-
JsonPath.new('$..*').on(@object).to_a.size.should == 29
|
95
|
-
end
|
96
|
-
|
97
|
-
it "should deal with a space in the key name" do
|
98
|
-
JsonPath.new("$.'c d'").on({"a" => "a","b" => "b", "c d" => "e"}).to_a.should == ['e']
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should support a convenience class method" do
|
102
|
-
JsonPath.on(@object, '$..author').to_a.should == JsonPath.new('$..author').on(@object).to_a
|
103
|
-
end
|
104
|
-
|
105
|
-
end
|
data/spec/spec_helper.rb
DELETED