jsonpath 0.4.1 → 0.4.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Jsonpath
2
2
 
3
- This is an implementation of http://goessner.net/articles/JsonPath/.
3
+ This is an implementation of http://goessner.net/articles/JsonPath/.
4
4
 
5
5
  ## Usage
6
6
 
@@ -15,7 +15,7 @@ As well, you can include it as a library.
15
15
 
16
16
  ~~~~~ {ruby}
17
17
  object = JSON.parse(<<-HERE_DOC)
18
- {"store":
18
+ {"store":
19
19
  {"bicycle":
20
20
  {"price":19.95, "color":"red"},
21
21
  "book":[
@@ -30,10 +30,10 @@ As well, you can include it as a library.
30
30
 
31
31
  JsonPath.new('$..price').on(object)
32
32
  # => [19.95, 8.95, 12.99, 8.99, 22.99]
33
-
33
+
34
34
  JsonPath.on(object, '$..author')
35
35
  # => ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"]
36
-
36
+
37
37
  JsonPath.new('$..book[::2]').on(object)
38
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
39
 
data/lib/jsonpath.rb CHANGED
@@ -17,7 +17,7 @@ class JsonPath
17
17
  @path << token
18
18
  elsif token = scanner.scan(/@/)
19
19
  @path << token
20
- elsif token = scanner.scan(/[a-zA-Z_]+/)
20
+ elsif token = scanner.scan(/[a-zA-Z0-9_]+/)
21
21
  @path << "['#{token}']"
22
22
  elsif token = scanner.scan(/'(.*?)'/)
23
23
  @path << "[#{token}]"
@@ -38,8 +38,11 @@ class JsonPath
38
38
  elsif token = scanner.scan(/\.\./)
39
39
  @path << token
40
40
  elsif scanner.scan(/\./)
41
+ nil
41
42
  elsif token = scanner.scan(/\*/)
42
43
  @path << token
44
+ elsif token = scanner.scan(/[><=] \d+/)
45
+ @path.last << token
43
46
  elsif token = scanner.scan(/./)
44
47
  @path.last << token
45
48
  end
@@ -67,7 +67,7 @@ class JsonPath
67
67
 
68
68
  private
69
69
  def yield_value(blk, context, key)
70
- @substitute_with = nil
70
+ @substitute_with = nil
71
71
  case @mode
72
72
  when nil
73
73
  blk.call(key ? context[key] : context)
@@ -1,3 +1,3 @@
1
1
  class JsonPath
2
- VERSION = '0.4.1' unless const_defined?(:VERSION)
2
+ VERSION = '0.4.2' unless const_defined?(:VERSION)
3
3
  end
@@ -44,17 +44,23 @@ class TestJsonpath < MiniTest::Unit::TestCase
44
44
  def test_recognize_filters
45
45
  assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@['isbn'])]").on(@object)
46
46
  assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] < 10)]").on(@object)
47
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new("$..book[?(@['price'] == 9)]").on(@object)
48
+ assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@['price'] > 20)]").on(@object)
47
49
  assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new("$..book[?(@.isbn)]").on(@object)
48
50
  end
49
51
 
50
52
  def test_no_eval
51
53
  assert_equal [], JsonPath.new('$..book[(@.length-2)]', :allow_eval => false).on(@object)
52
54
  end
53
-
55
+
54
56
  def test_paths_with_underscores
55
57
  assert_equal [@object['store']['bicycle']['catalogue_number']], JsonPath.new('$.store.bicycle.catalogue_number').on(@object)
56
58
  end
57
59
 
60
+ def test_paths_with_numbers
61
+ assert_equal [@object['store']['bicycle']['2seater']], JsonPath.new('$.store.bicycle.2seater').on(@object)
62
+ end
63
+
58
64
  def test_recognize_array_with_evald_index
59
65
  assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-2)]').on(@object)
60
66
  end
@@ -64,7 +70,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
64
70
  end
65
71
 
66
72
  def test_counting
67
- assert_equal 29, JsonPath.new('$..*').on(@object).to_a.size
73
+ assert_equal 30, JsonPath.new('$..*').on(@object).to_a.size
68
74
  end
69
75
 
70
76
  def test_space_in_path
@@ -128,7 +134,8 @@ class TestJsonpath < MiniTest::Unit::TestCase
128
134
  "bicycle"=> {
129
135
  "color"=> "red",
130
136
  "price"=> 20,
131
- "catalogue_number" => 12345 }
137
+ "catalogue_number" => 12345,
138
+ "2seater" => "yes"}
132
139
  } }
133
140
  end
134
141
 
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 1
10
- version: 0.4.1
9
+ - 2
10
+ version: 0.4.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-26 00:00:00 -07:00
18
+ date: 2011-07-26 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency