json_path_rfc9535 1.0.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 341631a563a6872bcf0f04f58fdb547a5a17500f3f7fae04ba29ada044c17dbd
4
- data.tar.gz: '0669414a8c33cd4ddfd67f2e330d72783ec862ba1a26573053a5bbf372a6b26e'
3
+ metadata.gz: fad15e45cdd7174b10b5b7bc990958f07af160af1807f890dcad622e0e8eef59
4
+ data.tar.gz: 05216cbac139928acdf3a3fc6ff49573833041035faf5de1d0555871dc368c61
5
5
  SHA512:
6
- metadata.gz: c355da298d64c50f35f59a76bbff44b2511de1dafbf65181a078db74909433396dd86f803ecd1b821ce0bf4327264786298d7189e5c7ee37777bf35e53dacc8e
7
- data.tar.gz: f8e4f5e8a131adb0b1d88b9a820218687db2a146fea1a4ef91e33adb461b1d299285895c5fde88554f6ac14d87b7e86235ed97734253f397e06e741a5f270b13
6
+ metadata.gz: fed52753d34dd25209e34e384ce498c85742618789e7ea08e056205b4c947de1bb0a3c9931404a0239c1fc2d3665f1491108a269483602a3249aeccca10a36f9
7
+ data.tar.gz: 4b5def5abb34c9f6c8173b66b468669ae8e1210ca53a6f0bc466bccd6c8e40eef4cda7378cf7f3881a480129990707c2e2368726c8e4097ca00a387a5c09f3ee
data/CHANGELOG.md CHANGED
@@ -8,6 +8,19 @@
8
8
  ### Bug fixes
9
9
  )-->
10
10
 
11
+ ## 1.1.1 2024-09-10
12
+
13
+ ### New features
14
+
15
+ - Added `size` to the return value of queries.
16
+
17
+ ## 1.1.0 2024-09-10
18
+
19
+ ### New features
20
+
21
+ - Made the return value of queries an `Enumerable`.
22
+ - Made individual nodes queryable.
23
+
11
24
  ## 1.0.0 2024-09-09
12
25
 
13
26
  First public release. Refer to [README.md](README.md) for the full documentation.
data/README.md CHANGED
@@ -75,12 +75,23 @@ JSON
75
75
  doc.query('$.store.bicycle.color')
76
76
  ```
77
77
 
78
- The returned object has methods to retrieve the values or the paths of all the retrieved nodes:
78
+ If you already parsed the Json, you can use that instead of the Json string:
79
79
 
80
80
  ```ruby
81
- doc.query('$.store.book.*.category').values
81
+ json = JSON.parse('...')
82
+ ...
83
+ doc = JsonPath::Doc(json)
84
+ ```
85
+
86
+ The query returns an `Enumerable`, which also has methods to retrieve the values or the paths of all the retrieved nodes:
87
+
88
+ ```ruby
89
+ results = doc.query('$.store.book.*.category')
90
+ results.count
91
+ # => 4
92
+ results.values
82
93
  # => ["reference", "fiction", "fiction", "fiction"]
83
- doc.query('$.store.book.*.category').paths
94
+ results.paths
84
95
  # => ["$['store']['book'][0]['category']", "$['store']['book'][1]['category']", "$['store']['book'][2]['category']", "$['store']['book'][3]['category']"]
85
96
  ```
86
97
 
@@ -94,6 +105,13 @@ results.query('$.author').values
94
105
  # => ["Evelyn Waugh", "J. R. R. Tolkien"]
95
106
  ```
96
107
 
108
+ Alternatively, you can query the single nodes:
109
+
110
+ ```ruby
111
+ results.flat_map { _1.query('$.author').values }.join(', ')
112
+ # => "Evelyn Waugh, J. R. R. Tolkien"
113
+ ```
114
+
97
115
  This gem implements most of RFC 9535, with the exception of [function extensions](https://datatracker.ietf.org/doc/html/rfc9535#name-function-extensions) and the related [type system](https://datatracker.ietf.org/doc/html/rfc9535#name-type-system-for-function-ex). It also relies on the underlying Ruby interpreter for string evaluation, meaning that characters don't need to be double-escaped.
98
116
 
99
117
  ## Plans for future development
data/lib/json_path/doc.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'json'
2
2
  require_relative 'nodes'
3
- require_relative 'path'
4
- require_relative 'node_list'
5
3
 
6
4
  module JsonPath
7
5
  class Doc
@@ -12,12 +10,8 @@ module JsonPath
12
10
  @root_node = Nodes.parse '$', json
13
11
  end
14
12
 
15
- def query json_path
16
- json_path = Path.new json_path
17
-
18
- json_path
19
- .apply(root_node)
20
- .then { NodeList.new _1 }
13
+ def query(...)
14
+ root_node.query(...)
21
15
  end
22
16
 
23
17
  def value
@@ -1,27 +1,33 @@
1
- require_relative 'path'
2
-
3
1
  module JsonPath
4
2
  class NodeList
3
+ include Enumerable
4
+
5
5
  attr_reader :nodes
6
6
 
7
7
  def initialize nodes
8
8
  @nodes = nodes
9
9
  end
10
10
 
11
- def query json_path
12
- json_path = Path.new json_path
13
-
11
+ def query(...)
14
12
  nodes
15
- .flat_map { json_path.apply _1 }
13
+ .flat_map { _1.query(...) }
16
14
  .then { self.class.new _1 }
17
15
  end
18
16
 
17
+ def each(&block)
18
+ nodes.each(&block)
19
+ end
20
+
19
21
  def values
20
- nodes.map(&:value)
22
+ map(&:value)
21
23
  end
22
24
 
23
25
  def paths
24
- nodes.map(&:path)
26
+ map(&:path)
27
+ end
28
+
29
+ def size
30
+ nodes.size
25
31
  end
26
32
  end
27
33
  end
@@ -1,3 +1,6 @@
1
+ require_relative '../path'
2
+ require_relative '../node_list'
3
+
1
4
  module JsonPath
2
5
  module Nodes
3
6
  class Base
@@ -8,6 +11,14 @@ module JsonPath
8
11
  @path = path
9
12
  @children = []
10
13
  end
14
+
15
+ def query json_path
16
+ json_path = Path.new json_path
17
+
18
+ json_path
19
+ .apply(self)
20
+ .then { NodeList.new _1 }
21
+ end
11
22
  end
12
23
  end
13
24
  end
@@ -1,3 +1,3 @@
1
1
  module JsonPathRfc9535
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_path_rfc9535
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-09-09 00:00:00.000000000 Z
12
+ date: 2024-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet