rbjq 1.1.0 → 1.2.0
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/README.md +30 -0
- data/lib/rbjq/version.rb +1 -1
- data/lib/rbjq.rb +11 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '069676a25843b3ef99cc871c48fb0db059d6719d920666fcac0eb2038e982617'
|
|
4
|
+
data.tar.gz: 1a8241175d72e8d477125228ccfb8f8f3b0dfe677669be132f98b458bb4c8d38
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ab91d242a876ec7d8c9921a75adfca809019406ebc73cb31f3bb169cf713cb3230e71abd4fe7a5a15b2ce46c8d29883573834126082d67472c0c064c6031ac29
|
|
7
|
+
data.tar.gz: f53b681e845048bb76b8045ae4b71ec7c0e3338ed8ba65a5f59ce95d5f436343c5d7dcbe837fb0908a49d5000bd2cb7bbf45d2cbbf0c0690d6ad7969fecd6467
|
data/README.md
CHANGED
|
@@ -38,6 +38,36 @@ Rbjq.query_json('{"a":1} {"a":2}', '.a') #=> [1, 2]
|
|
|
38
38
|
Rbjq.compile('[inputs]').run(nil, inputs: [1, 2, 3]) #=> [[1, 2, 3]]
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
+
#### Using another JSON parser
|
|
42
|
+
|
|
43
|
+
The built-in JSON parser is written in Ruby, so it is slow on large input.
|
|
44
|
+
`query_json` and `parse_json` take `parser:`, any object that responds to
|
|
45
|
+
`call(String)`, to parse the JSON text with something faster instead:
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require 'json'
|
|
49
|
+
|
|
50
|
+
Rbjq.query_json(json, '.users[] | .name', parser: JSON.method(:parse))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
On a 570KB document, `JSON.parse` takes about 2.5ms where the built-in
|
|
54
|
+
parser takes about 78ms.
|
|
55
|
+
|
|
56
|
+
- The parser returns one value, so the JSON text holds one value. With
|
|
57
|
+
`JSON.parse`, text with several values such as `1 2` is an error.
|
|
58
|
+
- Numbers are converted the way jq reads them (`1.0` is `1`), but other jq
|
|
59
|
+
extensions are up to the parser: `JSON.parse` rejects `NaN`, and reads
|
|
60
|
+
`-0` as `0`.
|
|
61
|
+
- `JSON.parse` in json 3.0 and later rejects duplicate keys such as
|
|
62
|
+
`{"a":1,"a":2}`, and by default it allows 100 levels of nesting where jq
|
|
63
|
+
allows 10,000. To accept both as jq does (json 2.13 and later):
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
parser: ->(s) { JSON.parse(s, allow_duplicate_key: true, max_nesting: false) }
|
|
67
|
+
```
|
|
68
|
+
- Exceptions from the parser propagate unchanged, such as
|
|
69
|
+
`JSON::ParserError`, instead of `Rbjq::ParseError`.
|
|
70
|
+
|
|
41
71
|
#### jq functions in Ruby
|
|
42
72
|
|
|
43
73
|
You can implement jq functions in Ruby, either for one program or for every
|
data/lib/rbjq/version.rb
CHANGED
data/lib/rbjq.rb
CHANGED
|
@@ -72,13 +72,21 @@ module Rbjq
|
|
|
72
72
|
|
|
73
73
|
# Run a jq filter against JSON text (which may contain several values)
|
|
74
74
|
# and return the outputs as an Array of Ruby values.
|
|
75
|
-
|
|
75
|
+
#
|
|
76
|
+
# parser: replaces the built-in JSON parser with any object that responds
|
|
77
|
+
# to call(String) and returns one value, e.g. JSON.method(:parse). Its
|
|
78
|
+
# exceptions propagate unchanged.
|
|
79
|
+
def query_json(json, filter, vars: {}, parser: nil, **)
|
|
76
80
|
prog = compile(filter, vars: vars, **)
|
|
77
|
-
|
|
81
|
+
values = parser ? [parser.call(json)] : JSONParser.parse_all(json)
|
|
82
|
+
values.flat_map { |v| prog.run(v) }
|
|
78
83
|
end
|
|
79
84
|
|
|
80
85
|
# Parse JSON text into Ruby values (jq-compatible numbers).
|
|
81
|
-
|
|
86
|
+
# parser: works as in query_json.
|
|
87
|
+
def parse_json(json, parser: nil)
|
|
88
|
+
return Program.normalize_value(parser.call(json)) if parser
|
|
89
|
+
|
|
82
90
|
JSONParser.parse(json)
|
|
83
91
|
end
|
|
84
92
|
|