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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +30 -0
  3. data/lib/rbjq/version.rb +1 -1
  4. data/lib/rbjq.rb +11 -3
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d32e4488a80488433e57f546c331b567398fbf5e78442e8163c21ced9364037
4
- data.tar.gz: 388c8deb0dd22b169d7ab986da80c8bd6056831216031d7942cd2bc8503b9c1e
3
+ metadata.gz: '069676a25843b3ef99cc871c48fb0db059d6719d920666fcac0eb2038e982617'
4
+ data.tar.gz: 1a8241175d72e8d477125228ccfb8f8f3b0dfe677669be132f98b458bb4c8d38
5
5
  SHA512:
6
- metadata.gz: 656ff877d9a9c22f43a1339a2fc318fe5e3c2f4844e4c3b288e112d5ff0032ec218ba27a438ee69950f265dd0d54c75fb50aae7ea51cb497325d651eaaa009bb
7
- data.tar.gz: 121450e6089c0597186c69ff07b168ee1c2790285f68663f7d9e3d0f1cac32a116d8b43797bf0f9015dcdcfe0fa5ac91e225c396071e289a6ffa064fdf3b624d
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Rbjq
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
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
- def query_json(json, filter, vars: {}, **)
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
- JSONParser.parse_all(json).flat_map { |v| prog.run(v) }
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
- def parse_json(json)
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbjq
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - winebarrel