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 +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +21 -3
- data/lib/json_path/doc.rb +2 -8
- data/lib/json_path/node_list.rb +14 -8
- data/lib/json_path/nodes/base.rb +11 -0
- data/lib/json_path_rfc9535/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fad15e45cdd7174b10b5b7bc990958f07af160af1807f890dcad622e0e8eef59
|
4
|
+
data.tar.gz: 05216cbac139928acdf3a3fc6ff49573833041035faf5de1d0555871dc368c61
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
78
|
+
If you already parsed the Json, you can use that instead of the Json string:
|
79
79
|
|
80
80
|
```ruby
|
81
|
-
|
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
|
-
|
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
|
16
|
-
|
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
|
data/lib/json_path/node_list.rb
CHANGED
@@ -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
|
12
|
-
json_path = Path.new json_path
|
13
|
-
|
11
|
+
def query(...)
|
14
12
|
nodes
|
15
|
-
.flat_map {
|
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
|
-
|
22
|
+
map(&:value)
|
21
23
|
end
|
22
24
|
|
23
25
|
def paths
|
24
|
-
|
26
|
+
map(&:path)
|
27
|
+
end
|
28
|
+
|
29
|
+
def size
|
30
|
+
nodes.size
|
25
31
|
end
|
26
32
|
end
|
27
33
|
end
|
data/lib/json_path/nodes/base.rb
CHANGED
@@ -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
|
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.
|
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-
|
12
|
+
date: 2024-09-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|