json_p3 0.4.0 → 1.0.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 (76) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.rubocop.yml +26 -7
  4. data/.ruby-version +1 -1
  5. data/CHANGELOG.md +58 -0
  6. data/README.md +125 -123
  7. data/Rakefile +3 -3
  8. data/certs/jgrp.pem +21 -21
  9. data/lib/json_p3/errors.rb +51 -43
  10. data/lib/json_p3/patch/op.rb +23 -0
  11. data/lib/json_p3/patch/op_add.rb +51 -0
  12. data/lib/json_p3/patch/op_copy.rb +64 -0
  13. data/lib/json_p3/patch/op_move.rb +74 -0
  14. data/lib/json_p3/patch/op_remove.rb +56 -0
  15. data/lib/json_p3/patch/op_replace.rb +54 -0
  16. data/lib/json_p3/patch/op_test.rb +31 -0
  17. data/lib/json_p3/patch.rb +15 -330
  18. data/lib/json_p3/path/environment.rb +113 -0
  19. data/lib/json_p3/path/filter.rb +463 -0
  20. data/lib/json_p3/path/function.rb +12 -0
  21. data/lib/json_p3/path/function_extensions/count.rb +15 -0
  22. data/lib/json_p3/path/function_extensions/length.rb +17 -0
  23. data/lib/json_p3/path/function_extensions/match.rb +62 -0
  24. data/lib/json_p3/path/function_extensions/pattern.rb +42 -0
  25. data/lib/json_p3/path/function_extensions/search.rb +44 -0
  26. data/lib/json_p3/path/function_extensions/value.rb +15 -0
  27. data/lib/json_p3/path/lexer.rb +220 -0
  28. data/lib/json_p3/path/node.rb +48 -0
  29. data/lib/json_p3/path/parser.rb +676 -0
  30. data/lib/json_p3/path/query.rb +74 -0
  31. data/lib/json_p3/path/segment.rb +172 -0
  32. data/lib/json_p3/path/selector.rb +304 -0
  33. data/lib/json_p3/path/serialize.rb +16 -0
  34. data/lib/json_p3/path/unescape.rb +134 -0
  35. data/lib/json_p3/pointer.rb +15 -76
  36. data/lib/json_p3/relative_pointer.rb +69 -0
  37. data/lib/json_p3/version.rb +1 -1
  38. data/lib/json_p3.rb +50 -13
  39. data/sig/json_p3/cache.rbs +21 -0
  40. data/sig/json_p3/errors.rbs +55 -0
  41. data/sig/json_p3/patch.rbs +145 -0
  42. data/sig/json_p3/path/environment.rbs +81 -0
  43. data/sig/json_p3/path/filter.rbs +196 -0
  44. data/sig/json_p3/path/function.rbs +94 -0
  45. data/sig/json_p3/path/lexer.rbs +62 -0
  46. data/sig/json_p3/path/node.rbs +46 -0
  47. data/sig/json_p3/path/parser.rbs +92 -0
  48. data/sig/json_p3/path/query.rbs +47 -0
  49. data/sig/json_p3/path/segment.rbs +54 -0
  50. data/sig/json_p3/path/selector.rbs +100 -0
  51. data/sig/json_p3/path/serialize.rbs +9 -0
  52. data/sig/json_p3/path/unescape.rbs +12 -0
  53. data/sig/json_p3/pointer.rbs +64 -0
  54. data/sig/json_p3/relative_pointer.rbs +30 -0
  55. data/sig/json_p3.rbs +24 -1313
  56. data.tar.gz.sig +0 -0
  57. metadata +66 -46
  58. metadata.gz.sig +0 -0
  59. data/lib/json_p3/environment.rb +0 -111
  60. data/lib/json_p3/filter.rb +0 -459
  61. data/lib/json_p3/function.rb +0 -10
  62. data/lib/json_p3/function_extensions/count.rb +0 -15
  63. data/lib/json_p3/function_extensions/length.rb +0 -17
  64. data/lib/json_p3/function_extensions/match.rb +0 -62
  65. data/lib/json_p3/function_extensions/pattern.rb +0 -39
  66. data/lib/json_p3/function_extensions/search.rb +0 -44
  67. data/lib/json_p3/function_extensions/value.rb +0 -15
  68. data/lib/json_p3/lexer.rb +0 -419
  69. data/lib/json_p3/node.rb +0 -44
  70. data/lib/json_p3/parser.rb +0 -553
  71. data/lib/json_p3/path.rb +0 -72
  72. data/lib/json_p3/segment.rb +0 -158
  73. data/lib/json_p3/selector.rb +0 -306
  74. data/lib/json_p3/serialize.rb +0 -13
  75. data/lib/json_p3/token.rb +0 -36
  76. data/lib/json_p3/unescape.rb +0 -112
data/sig/json_p3.rbs CHANGED
@@ -1,1325 +1,36 @@
1
1
  # JSONPath, JSON Patch and JSON Pointer for Ruby
2
2
  module JSONP3
3
- DefaultEnvironment: JSONPathEnvironment
3
+ VERSION: String
4
+
5
+ module Path
6
+ DefaultEnvironment: JSONP3::Path::Environment
7
+
8
+ def self.find: (String path, untyped data) -> JSONP3::Path::NodeList
9
+
10
+ def self.find_enum: (String path, untyped data) -> Enumerable[JSONP3::Path::Node]
11
+
12
+ def self.compile: (String path) -> JSONP3::Path::Query
13
+
14
+ def self.match: (String path, untyped data) -> (JSONP3::Path::Node | nil)
15
+
16
+ def self.first: (String path, untyped data) -> (JSONP3::Path::Node | nil)
17
+
18
+ def self.match?: (String path, untyped data) -> bool
19
+ end
4
20
 
5
- def self.find: (String path, untyped data) -> JSONPathNodeList
6
-
7
- def self.find_enum: (String path, untyped data) -> Enumerable[JSONPathNode]
21
+ def self.find: (String path, untyped data) -> JSONP3::Path::NodeList
22
+
23
+ def self.find_enum: (String path, untyped data) -> Enumerable[JSONP3::Path::Node]
8
24
 
9
- def self.compile: (String path) -> JSONPath
25
+ def self.compile: (String path) -> JSONP3::Path::Query
10
26
 
11
- def self.match: (String path, untyped data) -> (JSONPathNode | nil)
27
+ def self.match: (String path, untyped data) -> (JSONP3::Path::Node | nil)
12
28
 
13
- def self.first: (String path, untyped data) -> (JSONPathNode | nil)
29
+ def self.first: (String path, untyped data) -> (JSONP3::Path::Node | nil)
14
30
 
15
31
  def self.match?: (String path, untyped data) -> bool
16
32
 
17
33
  def self.resolve: (String pointer, untyped value, ?default: untyped) -> untyped
18
34
 
19
- def self.apply: (Array[Op | Hash[String, untyped]] ops, untyped value) -> untyped
20
- end
21
-
22
- module JSONP3
23
- # A least recently used cache relying on Ruby hash insertion order.
24
- class LRUCache
25
- @data: Hash[untyped, untyped]
26
-
27
- @max_size: Integer
28
-
29
- attr_reader max_size: Integer
30
-
31
- def initialize: (Integer max_size) -> void
32
-
33
- # Return the cached value or nil if _key_ does not exist.
34
- def []: (untyped key) -> (nil | untyped)
35
-
36
- def []=: (untyped key, untyped value) -> untyped
37
-
38
- def length: () -> Integer
39
-
40
- def keys: () -> Array[untyped]
41
- end
42
- end
43
-
44
- module JSONP3
45
- # JSONPath configuration
46
- class JSONPathEnvironment
47
- MAX_INT_INDEX: Numeric
48
-
49
- MIN_INT_INDEX: Numeric
50
-
51
- MAX_RECURSION_DEPTH: Integer
52
-
53
- # XXX: I don't known how to specify a class rather than a class instance.
54
- NAME_SELECTOR: untyped
55
-
56
- INDEX_SELECTOR: untyped
57
-
58
- @parser: Parser
59
-
60
- @function_extensions: Hash[String, FunctionExtension]
61
-
62
- attr_accessor function_extensions: Hash[String, FunctionExtension]
63
-
64
- def initialize: () -> void
65
-
66
- # Prepare JSONPath expression _query_ for repeated application.
67
- # @param query [String]
68
- # @return [JSONPath]
69
- def compile: (String query) -> JSONPath
70
-
71
- # Apply JSONPath expression _query_ to _value_.
72
- # @param query [String]
73
- # @param value [JSON-like data]
74
- # @return [Array<JSONPath>]
75
- def find: (String query, untyped value) -> JSONPathNodeList
76
-
77
- def find_enum: (String query, untyped value) -> Enumerable[JSONPathNode]
78
-
79
- def match: (String query, untyped value) -> (JSONPathNode | nil)
80
-
81
- def match?: (String query, untyped value) -> bool
82
-
83
- def first: (String query, untyped value) -> (JSONPathNode | nil)
84
-
85
- def setup_function_extensions: () -> void
86
- end
87
- end
88
-
89
- module JSONP3
90
- # An exception raised when a JSONPathEnvironment is misconfigured.
91
- class JSONPathEnvironmentError < StandardError
92
- end
93
-
94
- # Base class for JSONPath exceptions that happen when parsing or evaluating a query.
95
- class JSONPathError < StandardError
96
- FULL_MESSAGE: bool
97
-
98
- @token: Token
99
-
100
- def initialize: (String msg, Token token) -> void
101
- end
102
-
103
- class JSONPathSyntaxError < JSONPathError
104
- end
105
-
106
- class JSONPathTypeError < JSONPathError
107
- end
108
-
109
- class JSONPathNameError < JSONPathError
110
- end
111
-
112
- class JSONPathRecursionError < JSONPathError
113
- end
114
-
115
- class JSONPointerError < StandardError
116
- end
117
-
118
- class JSONPointerIndexError < JSONPointerError
119
- end
120
-
121
- class JSONPointerSyntaxError < JSONPointerError
122
- end
123
-
124
- class JSONPointerTypeError < JSONPointerError
125
- end
126
-
127
- class JSONPatchError < StandardError
128
- end
129
-
130
- class JSONPatchTestFailure < JSONPatchError
131
- end
132
-
133
- end
134
-
135
- module JSONP3
136
- # Base class for all filter expression nodes.
137
- class Expression
138
- @token: Token
139
-
140
- # @dynamic token
141
- attr_reader token: Token
142
-
143
- def initialize: (Token token) -> void
144
-
145
- # Evaluate the filter expression in the given context.
146
- def evaluate: (FilterContext _context) -> untyped
147
- end
148
-
149
- # An expression that evaluates to true or false.
150
- class FilterExpression < Expression
151
- @expression: Expression
152
-
153
- attr_reader expression: Expression
154
-
155
- def initialize: (Token token, Expression expression) -> void
156
-
157
- def evaluate: (FilterContext context) -> untyped
158
-
159
- def to_s: () -> String
160
-
161
- def ==: (untyped other) -> bool
162
-
163
- alias eql? ==
164
-
165
- def hash: () -> Integer
166
-
167
- private
168
-
169
- class Precedence
170
- LOWEST: 1
171
-
172
- LOGICAL_OR: 3
173
-
174
- LOGICAL_AND: 4
175
-
176
- PREFIX: 7
177
- end
178
-
179
- def to_canonical_string: (Expression expression, Integer parent_precedence) -> String
180
- end
181
-
182
- # Base class for expression literals.
183
- class FilterExpressionLiteral < Expression
184
- @value: untyped
185
-
186
- attr_reader value: untyped
187
-
188
- def initialize: (Token token, untyped value) -> void
189
-
190
- def evaluate: (FilterContext _context) -> untyped
191
-
192
- def to_s: () -> String
193
-
194
- def ==: (untyped other) -> bool
195
-
196
- alias eql? ==
197
-
198
- def hash: () -> Integer
199
- end
200
-
201
- # Literal true or false.
202
- class BooleanLiteral < FilterExpressionLiteral
203
- end
204
-
205
- # A double or single quoted string literal.
206
- class StringLiteral < FilterExpressionLiteral
207
- def to_s: () -> String
208
- end
209
-
210
- # A literal integer.
211
- class IntegerLiteral < FilterExpressionLiteral
212
- end
213
-
214
- # A literal float
215
- class FloatLiteral < FilterExpressionLiteral
216
- end
217
-
218
- # A literal null
219
- class NullLiteral < FilterExpressionLiteral
220
- def to_s: () -> "null"
221
- end
222
-
223
- # An expression prefixed with the logical not operator.
224
- class LogicalNotExpression < Expression
225
- @expression: Expression
226
-
227
- attr_reader expression: Expression
228
-
229
- def initialize: (Token token, Expression expression) -> void
230
-
231
- def evaluate: (FilterContext context) -> untyped
232
-
233
- def to_s: () -> ::String
234
-
235
- def ==: (untyped other) -> bool
236
-
237
- alias eql? ==
238
-
239
- def hash: () -> Integer
240
- end
241
-
242
- # Base class for expression with a left expression, operator and right expression.
243
- class InfixExpression < Expression
244
- @left: Expression
245
-
246
- @right: Expression
247
-
248
- attr_reader left: Expression
249
-
250
- attr_reader right: Expression
251
-
252
- def initialize: (Token token, Expression left, Expression right) -> void
253
-
254
- def evaluate: (FilterContext _context) -> untyped
255
-
256
- def to_s: () -> String
257
-
258
- def ==: (untyped other) -> bool
259
-
260
- alias eql? ==
261
-
262
- def hash: () -> Integer
263
- end
264
-
265
- # A logical `&&` expression.
266
- class LogicalAndExpression < InfixExpression
267
- def evaluate: (FilterContext context) -> untyped
268
-
269
- def to_s: () -> ::String
270
- end
271
-
272
- # A logical `||` expression.
273
- class LogicalOrExpression < InfixExpression
274
- def evaluate: (FilterContext context) -> untyped
275
-
276
- def to_s: () -> ::String
277
- end
278
-
279
- # An `==` expression.
280
- class EqExpression < InfixExpression
281
- def evaluate: (FilterContext context) -> untyped
282
-
283
- def to_s: () -> ::String
284
- end
285
-
286
- # A `!=` expression.
287
- class NeExpression < InfixExpression
288
- def evaluate: (FilterContext context) -> untyped
289
-
290
- def to_s: () -> ::String
291
- end
292
-
293
- # A `<=` expression.
294
- class LeExpression < InfixExpression
295
- def evaluate: (FilterContext context) -> untyped
296
-
297
- def to_s: () -> ::String
298
- end
299
-
300
- # A `>=` expression.
301
- class GeExpression < InfixExpression
302
- def evaluate: (FilterContext context) -> untyped
303
-
304
- def to_s: () -> ::String
305
- end
306
-
307
- # A `<` expression.
308
- class LtExpression < InfixExpression
309
- def evaluate: (FilterContext context) -> untyped
310
-
311
- def to_s: () -> ::String
312
- end
313
-
314
- # A `>` expression.
315
- class GtExpression < InfixExpression
316
- def evaluate: (FilterContext context) -> untyped
317
-
318
- def to_s: () -> ::String
319
- end
320
-
321
- # Base class for all embedded filter queries
322
- class QueryExpression < Expression
323
- @query: JSONPath
324
-
325
- attr_reader query: JSONPath
326
-
327
- def initialize: (Token token, JSONPath query) -> void
328
-
329
- def evaluate: (FilterContext _context) -> untyped
330
-
331
- def to_s: () -> String
332
-
333
- def ==: (untyped other) -> bool
334
-
335
- alias eql? ==
336
-
337
- def hash: () -> Integer
338
- end
339
-
340
- # An embedded query starting at the current node.
341
- class RelativeQueryExpression < QueryExpression
342
- def evaluate: (FilterContext context) -> untyped
343
-
344
- def to_s: () -> ::String
345
- end
346
-
347
- # An embedded query starting at the root node.
348
- class RootQueryExpression < QueryExpression
349
- def evaluate: (FilterContext context) -> untyped
350
-
351
- def to_s: () -> String
352
- end
353
-
354
- # A filter function call.
355
- class FunctionExpression < Expression
356
- @name: String
357
-
358
- @args: Array[Expression]
359
-
360
- attr_reader name: String
361
-
362
- attr_reader args: Array[Expression]
363
-
364
- # @param name [String]
365
- # @param args [Array<Expression>]
366
- def initialize: (Token token, String name, Array[Expression] args) -> void
367
-
368
- def evaluate: (FilterContext context) -> untyped
369
-
370
- def to_s: () -> ::String
371
-
372
- def ==: (untyped other) -> bool
373
-
374
- alias eql? ==
375
-
376
- def hash: () -> Integer
377
-
378
- private
379
-
380
- def unpack_node_lists: (FunctionExtension func, Array[untyped] args) -> Array[untyped]
381
- end
382
-
383
- def self.truthy?: (untyped obj) -> bool
384
-
385
- def self.eq?: (Expression left, Expression right) -> bool
386
-
387
- def self.lt?: (Expression left, Expression right) -> bool
388
-
389
- # Contextual information and data used for evaluating a filter expression.
390
- class FilterContext
391
- @env: JSONPathEnvironment
392
-
393
- @current: untyped
394
-
395
- @root: untyped
396
-
397
- attr_reader env: JSONPathEnvironment
398
-
399
- attr_reader current: untyped
400
-
401
- attr_reader root: untyped
402
-
403
- def initialize: (JSONPathEnvironment env, untyped current, untyped root) -> void
404
- end
405
- end
406
-
407
- type expression_t = :value_expression | :logical_expression | :nodes_expression
408
-
409
- module JSONP3
410
- # Base class for all filter functions.
411
- class FunctionExtension
412
- ARG_TYPES: Array[expression_t]
413
-
414
- RETURN_TYPE: expression_t
415
-
416
- def call: (*untyped _args, **untyped _kwargs) -> untyped
417
- end
418
- end
419
-
420
- module JSONP3
421
- # Return an array of tokens for the JSONPath expression _query_.
422
- #
423
- # @param query [String] the JSONPath expression to tokenize.
424
- # @return [Array<Token>]
425
- def self.tokenize: (String query) -> Array[Token]
426
-
427
- # JSONPath query expression lexical scanner.
428
- #
429
- # @see tokenize
430
- class Lexer
431
- @filter_depth: Integer
432
-
433
- @paren_stack: Array[Integer]
434
-
435
- @tokens: Array[Token]
436
-
437
- @start: Integer
438
-
439
- @query: String
440
-
441
- @scanner: untyped
442
-
443
- RE_INT: ::Regexp
444
-
445
- RE_NAME: ::Regexp
446
-
447
- RE_WHITESPACE: ::Regexp
448
-
449
- S_ESCAPES: Set[String]
450
-
451
- # @dynamic tokens
452
- attr_reader tokens: Array[Token]
453
-
454
- def initialize: (String query) -> void
455
-
456
- def run: () -> void
457
-
458
- # Generate a new token with the given type.
459
- # @param token_type [Symbol] one of the constants defined on the _Token_ class.
460
- # @param value [String | nil] a the token's value, if it is known, otherwise the
461
- # value will be sliced from @query. This is a performance optimization.
462
- def emit: (token_t token_type, ?untyped value) -> void
463
-
464
- def next: () -> String
465
-
466
- def ignore: () -> void
467
-
468
- def backup: () -> void
469
-
470
- def peek: () -> String
471
-
472
- # Advance the lexer if the next character is equal to _char_.
473
- def accept?: (String | Regexp pattern) -> bool
474
-
475
- # Accept a run of digits, possibly preceded by a negative sign.
476
- # Does not handle exponents.
477
- def accept_int?: () -> bool
478
-
479
- def ignore_whitespace?: () -> (false | true)
480
-
481
- def error: (String message) -> void
482
-
483
- def lex_root: () -> (nil | :lex_segment)
484
-
485
- def lex_segment: () -> (nil | :lex_shorthand_selector | untyped)
486
-
487
- def lex_descendant_segment: () -> untyped
488
-
489
- def lex_shorthand_selector: () -> (nil | :lex_segment)
490
-
491
- def lex_inside_bracketed_segment: () -> untyped
492
-
493
- def lex_inside_filter: () -> untyped
494
-
495
- def self.lex_string_factory: (String quote, Symbol state, token_t token) -> untyped
496
- end
497
- end
498
-
499
- type location_element = String | Integer | Array[location_element]
500
-
501
- module JSONP3
502
- # A JSON-like value and its location.
503
- class JSONPathNode
504
- @value: untyped
505
-
506
- @location: Array[location_element]
507
-
508
- @root: untyped
509
-
510
- # @dynamic value, location, root
511
- attr_reader value: untyped
512
-
513
- # @dynamic value, location, root
514
- attr_reader location: Array[location_element]
515
-
516
- # @dynamic value, location, root
517
- attr_reader root: untyped
518
-
519
- # @param value [JSON-like] the value at this node.
520
- # @param location [Array<String | Integer | Array<String | Integer>>] the sequence of
521
- # names and/or indices leading to _value_ in _root_.
522
- # @param root [JSON-like] the root value containing _value_ at _location_.
523
- def initialize: (untyped value, Array[location_element] location, untyped root) -> void
524
-
525
- # Return the normalized path to this node.
526
- # @return [String] the normalized path.
527
- def path: () -> ::String
528
-
529
- # Return a new node that is a child of this node.
530
- # @param value the JSON-like value at the new node.
531
- # @param key [Integer, String] the array index or hash key associated with _value_.
532
- def new_child: (untyped value, String | Integer key) -> JSONPathNode
533
-
534
- def to_s: () -> ::String
535
- end
536
-
537
- # An array of JSONPathNode instances.
538
- class JSONPathNodeList < Array[JSONPathNode]
539
- end
35
+ def self.apply!: (Array[Patch::Op | Hash[String, untyped]] ops, untyped value) -> untyped
540
36
  end
541
-
542
- module JSONP3
543
- # Step through tokens
544
- class Stream
545
- @tokens: Array[Token]
546
-
547
- @index: Integer
548
-
549
- @eoi: Token
550
-
551
- def initialize: (Array[Token] tokens) -> void
552
-
553
- def next: () -> Token
554
-
555
- def peek: () -> Token
556
-
557
- def expect: (token_t token_type) -> void
558
-
559
- def expect_not: (token_t token_type, String message) -> void
560
-
561
- def to_s: () -> ::String
562
- end
563
-
564
- class Precedence
565
- LOWEST: 1
566
-
567
- LOGICAL_OR: 3
568
-
569
- LOGICAL_AND: 4
570
-
571
- RELATIONAL: 5
572
-
573
- PREFIX: 7
574
- end
575
-
576
- # A JSONPath expression parser.
577
- class Parser
578
- @env: JSONPathEnvironment
579
-
580
- @name_selector: untyped
581
-
582
- @index_selector: untyped
583
-
584
- def initialize: (JSONPathEnvironment env) -> void
585
-
586
- # Parse an array of tokens into an abstract syntax tree.
587
- # @param tokens [Array<Token>] tokens from the lexer.
588
- # @return [Array<Segment>]
589
- def parse: (Array[Token] tokens) -> Array[Segment]
590
-
591
- def parse_query: (Stream stream) -> Array[Segment]
592
-
593
- def parse_selectors: (Stream stream) -> Array[Selector]
594
-
595
- def parse_bracketed_selection: (Stream stream) -> Array[Selector]
596
-
597
- def parse_index_or_slice: (Stream stream) -> Selector
598
-
599
- def parse_slice_selector: (Stream stream) -> Selector
600
-
601
- def parse_filter_selector: (Stream stream) -> Selector
602
-
603
- def parse_filter_expression: (Stream stream, ?Integer precedence) -> Expression
604
-
605
- def parse_integer_literal: (Stream stream) -> Expression
606
-
607
- def parse_float_literal: (Stream stream) -> Expression
608
-
609
- def parse_function_expression: (Stream stream) -> Expression
610
-
611
- def parse_grouped_expression: (Stream stream) -> Expression
612
-
613
- def parse_prefix_expression: (Stream stream) -> Expression
614
-
615
- def parse_root_query: (Stream stream) -> Expression
616
-
617
- def parse_relative_query: (Stream stream) -> Expression
618
-
619
- def parse_infix_expression: (Stream stream, Expression left) -> Expression
620
-
621
- def parse_i_json_int: (Token token) -> Integer
622
-
623
- def decode_string_literal: (Token token) -> String
624
-
625
- def raise_for_non_comparable_function: (Expression expression) -> void
626
-
627
- def raise_for_not_compared_literal: (Expression expression) -> void
628
-
629
- def validate_function_extension_signature: (Token token, untyped args) -> void
630
-
631
- def function_return_type: (Expression expression) -> (expression_t | nil)
632
-
633
- PRECEDENCES: ::Hash[token_t, Integer]
634
-
635
- BINARY_OPERATORS: ::Hash[token_t, "&&" | "||" | "==" | ">=" | ">" | "<=" | "<" | "!="]
636
-
637
- COMPARISON_OPERATORS: Set[String]
638
- end
639
- end
640
-
641
- module JSONP3
642
- # A compiled JSONPath expression ready to be applied to JSON-like values.
643
- class JSONPath
644
- @env: JSONPathEnvironment
645
-
646
- @segments: Array[Segment]
647
-
648
- def initialize: (JSONPathEnvironment env, Array[Segment] segments) -> void
649
-
650
- def to_s: () -> ::String
651
-
652
- # Apply this JSONPath expression to JSON-like value _root_.
653
- # @param root [Array, Hash, String, Integer] the root JSON-like value to apply this query to.
654
- # @return [Array<JSONPathNode>] the sequence of nodes found while applying this query to _root_.
655
- def find: (untyped root) -> JSONPathNodeList
656
-
657
- def find_enum: (untyped root) -> Enumerable[JSONPathNode]
658
-
659
- def match: (untyped root) -> (JSONPathNode | nil)
660
-
661
- def match?: (untyped root) -> bool
662
-
663
- def first: (untyped root) -> (JSONPathNode | nil)
664
-
665
- alias apply find
666
-
667
- # Return true if this JSONPath expression is a singular query.
668
- def singular?: () -> (false | true)
669
-
670
- # Return true if this JSONPath expression has no segments.
671
- def empty?: () -> (false | true)
672
- end
673
- end
674
-
675
- module JSONP3
676
- # Base class for all JSONPath segments.
677
- class Segment
678
- @env: JSONPathEnvironment
679
-
680
- @token: Token
681
-
682
- @selectors: Array[Selector]
683
-
684
- # @dynamic token, selectors
685
- attr_reader token: Token
686
-
687
- # @dynamic token, selectors
688
- attr_reader selectors: Array[Selector]
689
-
690
- def initialize: (JSONPathEnvironment env, Token token, Array[Selector] selectors) -> void
691
-
692
- # Select the children of each node in _nodes_.
693
- def resolve: (Array[JSONPathNode] _nodes) -> Array[JSONPathNode]
694
-
695
- # Select the children of each node in _nodes_.
696
- def resolve_enum: (Enumerable[JSONPathNode] _nodes) -> Enumerable[JSONPathNode]
697
- end
698
-
699
- # The child selection segment.
700
- class ChildSegment < Segment
701
- def resolve: (Array[JSONPathNode] nodes) -> Array[JSONPathNode]
702
-
703
- def resolve_enum: (Enumerable[JSONPathNode] nodes) -> Enumerable[JSONPathNode]
704
-
705
- def to_s: () -> ::String
706
-
707
- def ==: (untyped other) -> bool
708
-
709
- alias eql? ==
710
-
711
- def hash: () -> Integer
712
- end
713
-
714
- # The recursive descent segment
715
- class RecursiveDescentSegment < Segment
716
- def resolve: (Array[JSONPathNode] nodes) -> Array[JSONPathNode]
717
-
718
- def resolve_enum: (Enumerable[JSONPathNode] nodes) -> Enumerable[JSONPathNode]
719
-
720
- def to_s: () -> ::String
721
-
722
- def ==: (untyped other) -> bool
723
-
724
- alias eql? ==
725
-
726
- def hash: () -> Integer
727
-
728
- def visit: (JSONPathNode node, ?::Integer depth) -> Array[JSONPathNode]
729
- def visit_enum: (JSONPathNode node, ?::Integer depth) -> Enumerable[JSONPathNode]
730
- end
731
- end
732
-
733
- module JSONP3
734
- # Base class for all JSONPath selectors
735
- class Selector
736
- @env: JSONPathEnvironment
737
-
738
- @token: Token
739
-
740
- # @dynamic token
741
- attr_reader token: Token
742
-
743
- def initialize: (JSONPathEnvironment env, Token token) -> void
744
-
745
- # Apply this selector to _node_.
746
- # @return [Array<JSONPathNode>]
747
- def resolve: (JSONPathNode _node) -> Array[JSONPathNode]
748
- def resolve_enum: (JSONPathNode _node) -> Enumerable[JSONPathNode]
749
-
750
- # Return true if this selector is a singular selector.
751
- def singular?: () -> false
752
- end
753
-
754
- # The name selector select values from hashes given a key.
755
- class NameSelector < Selector
756
- @name: String
757
-
758
- # @dynamic name
759
- attr_reader name: String
760
-
761
- def initialize: (JSONPathEnvironment env, Token token, String name) -> void
762
-
763
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
764
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
765
-
766
- def singular?: () -> true
767
-
768
- def to_s: () -> String
769
-
770
- def ==: (untyped other) -> bool
771
-
772
- alias eql? ==
773
-
774
- def hash: () -> Integer
775
- end
776
-
777
- class SymbolNameSelector < NameSelector
778
- @name: String
779
-
780
- @sym: Symbol
781
-
782
- # @dynamic name
783
- attr_reader name: String
784
-
785
- def initialize: (JSONPathEnvironment env, Token token, String name) -> void
786
-
787
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
788
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
789
-
790
- def singular?: () -> true
791
-
792
- def to_s: () -> String
793
-
794
- def ==: (untyped other) -> bool
795
-
796
- alias eql? ==
797
-
798
- def hash: () -> Integer
799
- end
800
-
801
- # The index selector selects values from arrays given an index.
802
- class IndexSelector < Selector
803
- @index: Integer
804
-
805
- # @dynamic index
806
- attr_reader index: Integer
807
-
808
- def initialize: (JSONPathEnvironment env, Token token, Integer index) -> void
809
-
810
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
811
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
812
-
813
- def singular?: () -> true
814
-
815
- def to_s: () -> String
816
-
817
- def ==: (untyped other) -> bool
818
-
819
- alias eql? ==
820
-
821
- def hash: () -> Integer
822
-
823
- private
824
-
825
- def normalize: (Integer index, Integer length) -> Integer
826
- end
827
-
828
- # The wildcard selector selects all elements from an array or values from a hash.
829
- class WildcardSelector < Selector
830
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
831
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
832
-
833
- def to_s: () -> "*"
834
-
835
- def ==: (untyped other) -> bool
836
-
837
- alias eql? ==
838
-
839
- def hash: () -> Integer
840
- end
841
-
842
- # The slice selector selects a range of elements from an array.
843
- class SliceSelector < Selector
844
- @start: (Integer | nil)
845
-
846
- @stop: (Integer | nil)
847
-
848
- @step: Integer
849
-
850
- # @dynamic start, stop, step
851
- attr_reader start: (Integer | nil)
852
-
853
- # @dynamic start, stop, step
854
- attr_reader stop: (Integer | nil)
855
-
856
- # @dynamic start, stop, step
857
- attr_reader step: Integer
858
-
859
- def initialize: (JSONPathEnvironment env, Token token, (Integer | nil) start, (Integer | nil) stop, (Integer | nil) step) -> void
860
-
861
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
862
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
863
-
864
- def to_s: () -> ::String
865
-
866
- def ==: (untyped other) -> bool
867
-
868
- alias eql? ==
869
-
870
- def hash: () -> Integer
871
- end
872
-
873
- # Select array elements or hash values according to a filter expression.
874
- class FilterSelector < Selector
875
- @expression: Expression
876
-
877
- # @dynamic expression
878
- attr_reader expression: Expression
879
-
880
- def initialize: (JSONPathEnvironment env, Token token, Expression expression) -> void
881
-
882
- def resolve: (JSONPathNode node) -> ::Array[JSONPathNode]
883
- def resolve_enum: (JSONPathNode node) -> Enumerable[JSONPathNode]
884
-
885
- def to_s: () -> ::String
886
-
887
- def ==: (untyped other) -> bool
888
-
889
- alias eql? ==
890
-
891
- def hash: () -> Integer
892
- end
893
- end
894
-
895
- type token_t = (
896
- :token_eoi |
897
- :token_error |
898
- :token_colon |
899
- :token_comma |
900
- :token_double_dot |
901
- :token_filter |
902
- :token_index |
903
- :token_lbracket |
904
- :token_name |
905
- :token_rbracket |
906
- :token_root |
907
- :token_wild |
908
- :token_and |
909
- :token_current |
910
- :token_double_quote_string |
911
- :token_eq |
912
- :token_false |
913
- :token_float |
914
- :token_function |
915
- :token_ge |
916
- :token_gt |
917
- :token_int |
918
- :token_le |
919
- :token_lparen |
920
- :token_lt |
921
- :token_ne |
922
- :token_not |
923
- :token_null |
924
- :token_or |
925
- :token_rparen |
926
- :token_single_quote_string |
927
- :token_true
928
- )
929
-
930
-
931
- module JSONP3
932
- # Tokens are produced by the lexer and consumed by the parser. Each token contains sub
933
- # string from a JSONPath expression, its location within the JSONPath expression and a
934
- # symbol indicating what type of token it is.
935
- class Token
936
- @type: token_t
937
-
938
- @value: String
939
-
940
- @start: Integer
941
-
942
- @query: String
943
-
944
- @message: (String | nil)
945
-
946
- # @dynamic type, value, start, query
947
- attr_reader type: token_t
948
-
949
- # @dynamic type, value, start, query
950
- attr_reader value: String
951
-
952
- # @dynamic type, value, start, query
953
- attr_reader start: Integer
954
-
955
- # @dynamic type, value, start, query
956
- attr_reader query: String
957
-
958
- # @dynamic type, value, start, query
959
- attr_reader message: (String | nil)
960
-
961
- def initialize: (token_t type, String value, Integer start, String query, ?message: (String | nil)?) -> void
962
-
963
- def ==: (untyped other) -> bool
964
-
965
- alias eql? ==
966
-
967
- def hash: () -> Integer
968
- end
969
- end
970
-
971
- module JSONP3
972
- # Replace escape sequences with their equivalent Unicode code point.
973
- # @param value [String]
974
- # @param quote [String] one of '"' or "'".
975
- # @param token [Token]
976
- # @return [String] A new string without escape sequences.
977
- def self.unescape_string: (String value, String quote, Token token) -> String
978
-
979
- def self.decode_hex_char: (untyped value, untyped index, Token token) -> ::Array[untyped]
980
-
981
- def self.parse_hex_digits: (untyped digits, Token token) -> untyped
982
-
983
- def self.high_surrogate?: (untyped code_point) -> untyped
984
-
985
- def self.low_surrogate?: (untyped code_point) -> untyped
986
-
987
- def self.code_point_to_string: (untyped code_point, Token token) -> untyped
988
-
989
- def self.canonical_string: (untyped String) -> ::String
990
- end
991
-
992
- module JSONP3
993
- VERSION: String
994
- end
995
-
996
- module JSONP3
997
- # The standard `count` function.
998
- class Count < FunctionExtension
999
- ARG_TYPES: Array[expression_t]
1000
-
1001
- RETURN_TYPE: expression_t
1002
-
1003
- def call: (JSONPathNodeList node_list) -> untyped
1004
- end
1005
- end
1006
-
1007
- module JSONP3
1008
- # The standard `length` function.
1009
- class Length < FunctionExtension
1010
- ARG_TYPES: Array[expression_t]
1011
-
1012
- RETURN_TYPE: expression_t
1013
-
1014
- def call: (untyped obj) -> (:nothing | untyped)
1015
- end
1016
- end
1017
-
1018
- module JSONP3
1019
- # The standard `match` function.
1020
- class Match < FunctionExtension
1021
- @cache_size: Integer
1022
-
1023
- @raise_errors: (true | false)
1024
-
1025
- @cache: LRUCache
1026
-
1027
- ARG_TYPES: Array[expression_t]
1028
-
1029
- RETURN_TYPE: expression_t
1030
-
1031
- # @param cache_size [Integer] the maximum size of the regexp cache. Set it to
1032
- # zero or negative to disable the cache.
1033
- # @param raise_errors [bool] if _false_ (the default), return _false_ when this
1034
- # function causes a RegexpError instead of raising the exception.
1035
- def initialize: (?::Integer cache_size, ?raise_errors: bool) -> void
1036
-
1037
- # @param value [String]
1038
- # @param pattern [String]
1039
- # @return bool
1040
- def call: (untyped value, untyped pattern) -> untyped
1041
-
1042
- private
1043
-
1044
- def full_match: (String pattern) -> String
1045
- end
1046
- end
1047
-
1048
- module JSONP3
1049
- # Map I-Regexp pattern to Ruby regex pattern.
1050
- # @param pattern [String]
1051
- # @return [String]
1052
- def self.map_iregexp: (String pattern) -> String
1053
- end
1054
-
1055
- module JSONP3
1056
- # The standard `search` function.
1057
- class Search < FunctionExtension
1058
- @cache_size: Integer
1059
-
1060
- @raise_errors: (true | false)
1061
-
1062
- @cache: LRUCache
1063
-
1064
- ARG_TYPES: Array[expression_t]
1065
-
1066
- RETURN_TYPE: expression_t
1067
-
1068
- # @param cache_size [Integer] the maximum size of the regexp cache. Set it to
1069
- # zero or negative to disable the cache.
1070
- # @param raise_errors [bool] if _false_ (the default), return _false_ when this
1071
- # function causes a RegexpError instead of raising the exception.
1072
- def initialize: (?::Integer cache_size, ?raise_errors: bool) -> void
1073
-
1074
- # @param value [String]
1075
- # @param pattern [String]
1076
- # @return bool
1077
- def call: (untyped value, untyped pattern) -> untyped
1078
- end
1079
- end
1080
-
1081
- module JSONP3
1082
- # The standard `value` function.
1083
- class Value < FunctionExtension
1084
- ARG_TYPES: Array[expression_t]
1085
-
1086
- RETURN_TYPE: expression_t
1087
-
1088
- def call: (JSONPathNodeList node_list) -> (untyped | :nothing)
1089
- end
1090
- end
1091
-
1092
- module JSONP3
1093
- # Identify a single value in JSON-like data, as per RFC 6901.
1094
- class JSONPointer
1095
- @tokens: Array[String | Integer]
1096
-
1097
- @pointer: String
1098
-
1099
- UNDEFINED: :__undefined
1100
-
1101
- RE_INT: ::Regexp
1102
-
1103
- attr_reader tokens: Array[String | Integer]
1104
-
1105
- # Encode an array of strings and integers into a JSON pointer.
1106
- # @param tokens [Array<String | Integer>]
1107
- # @return [String]
1108
- def self.encode: (Array[String | Integer] tokens) -> String
1109
-
1110
- # @param pointer [String]
1111
- def initialize: (String pointer) -> void
1112
-
1113
- # Resolve this pointer against JSON-like data _value_.
1114
- def resolve: (untyped value, ?default: untyped) -> untyped
1115
-
1116
- def resolve_with_parent: (untyped value) -> ::Array[untyped]
1117
-
1118
- def relative_to?: (JSONPointer pointer) -> bool
1119
-
1120
- # @param parts [String]
1121
- def join: (*String parts) -> JSONPointer
1122
-
1123
- # Return _true_ if this pointer can be resolved against _value_, even if the resolved
1124
- # value is false or nil.
1125
- def exist?: (untyped value) -> bool
1126
-
1127
- # Return this pointer's parent as a new pointer. If this pointer points to the
1128
- # document root, self is returned.
1129
- def parent: () -> JSONPointer
1130
-
1131
- # @param rel [String | RelativeJSONPointer]
1132
- # @return [JSONPointer]
1133
- def to: ((String | RelativeJSONPointer) rel) -> JSONPointer
1134
-
1135
- def to_s: () -> String
1136
-
1137
- # @param pointer [String]
1138
- # @return [Array<String | Integer>]
1139
- def parse: (String pointer) -> Array[(String | Integer)]
1140
-
1141
- # @param value [Object]
1142
- # @param token [String | Integer]
1143
- # @return [Object] the "fetched" object from _value_ or UNDEFINED.
1144
- def get_item: (untyped value, (String | Integer) token) -> untyped
1145
-
1146
- # Like `#parse`, but assumes there's no leading slash.
1147
- # @param pointer [String]
1148
- # @return [Array<String | Integer>]
1149
- def _parse: (String pointer) -> Array[(String | Integer)]
1150
-
1151
- def _join: (String other) -> JSONPointer
1152
- end
1153
-
1154
- # A relative JSON Pointer.
1155
- # See https://datatracker.ietf.org/doc/html/draft-hha-relative-json-pointer
1156
- class RelativeJSONPointer
1157
- @origin: Integer
1158
-
1159
- @index: Integer
1160
-
1161
- @pointer: (String | JSONPointer)
1162
-
1163
- RE_RELATIVE_POINTER: ::Regexp
1164
-
1165
- RE_INT: ::Regexp
1166
-
1167
- # @param rel [String]
1168
- def initialize: (String rel) -> void
1169
-
1170
- def to_s: () -> ::String
1171
-
1172
- # @param pointer [String | JSONPointer]
1173
- # @return [JSONPointer]
1174
- def to: ((String | JSONPointer) pointer) -> JSONPointer
1175
-
1176
- private
1177
-
1178
- # @param token [String]
1179
- # @return [Integer]
1180
- def parse_int: (String token) -> Integer
1181
- end
1182
- end
1183
-
1184
- module JSONP3
1185
- # Base class for all JSON Patch operations
1186
- class Op
1187
- # Return the name of the patch operation.
1188
- def name: () -> String
1189
-
1190
- # Apply the patch operation to _value_.
1191
- def apply: (untyped _value, Integer _index) -> untyped
1192
-
1193
- # Return a JSON-like representation of this patch operation.
1194
- def to_h: () -> untyped
1195
- end
1196
-
1197
- # The JSON Patch _add_ operation.
1198
- class OpAdd < Op
1199
- @pointer: JSONPointer
1200
-
1201
- @value: untyped
1202
-
1203
- # @param pointer [JSONPointer]
1204
- # @param value [JSON-like value]
1205
- def initialize: (JSONPointer pointer, untyped value) -> void
1206
-
1207
- def name: () -> "add"
1208
- end
1209
-
1210
- # The JSON Patch _remove_ operation.
1211
- class OpRemove < Op
1212
- @pointer: JSONPointer
1213
-
1214
- # @param pointer [JSONPointer]
1215
- def initialize: (JSONPointer pointer) -> void
1216
-
1217
- def name: () -> "remove"
1218
- end
1219
-
1220
- # The JSON Patch _replace_ operation.
1221
- class OpReplace < Op
1222
- @pointer: JSONPointer
1223
-
1224
- @value: untyped
1225
-
1226
- # @param pointer [JSONPointer]
1227
- # @param value [JSON-like value]
1228
- def initialize: (JSONPointer pointer, untyped value) -> void
1229
-
1230
- def name: () -> "replace"
1231
- end
1232
-
1233
- # The JSON Patch _move_ operation.
1234
- class OpMove < Op
1235
- @from: JSONPointer
1236
-
1237
- @pointer: JSONPointer
1238
-
1239
- # @param from [JSONPointer]
1240
- # @param pointer [JSONPointer]
1241
- def initialize: (JSONPointer from, JSONPointer pointer) -> void
1242
-
1243
- def name: () -> "move"
1244
- end
1245
-
1246
- # The JSON Patch _copy_ operation.
1247
- class OpCopy < Op
1248
- @from: JSONPointer
1249
-
1250
- @pointer: JSONPointer
1251
-
1252
- # @param from [JSONPointer]
1253
- # @param pointer [JSONPointer]
1254
- def initialize: (JSONPointer from, JSONPointer pointer) -> void
1255
-
1256
- def name: () -> "copy"
1257
-
1258
- def deep_copy: (untyped obj) -> untyped
1259
- end
1260
-
1261
- # The JSON Patch _test_ operation.
1262
- class OpTest < Op
1263
- @pointer: JSONPointer
1264
-
1265
- @value: untyped
1266
-
1267
- # @param pointer [JSONPointer]
1268
- # @param value [JSON-like value]
1269
- def initialize: (JSONPointer pointer, untyped value) -> void
1270
-
1271
- def name: () -> "test"
1272
- end
1273
-
1274
- # A JSON Patch containing zero or more patch operations.
1275
- class JSONPatch
1276
- @ops: Array[Op]
1277
-
1278
- # @param ops [Array<Op>?]
1279
- def initialize: (?Array[Op | Hash[String, untyped]]? ops) -> void
1280
-
1281
- # @param pointer [String | JSONPointer]
1282
- # @param value [JSON-like value]
1283
- # @return [self]
1284
- def add: (JSONPointer | String pointer, untyped value) -> self
1285
-
1286
- # @param pointer [String | JSONPointer]
1287
- # @return [self]
1288
- def remove: (JSONPointer | String pointer) -> self
1289
-
1290
- # @param pointer [String | JSONPointer]
1291
- # @param value [JSON-like value]
1292
- # @return [self]
1293
- def replace: (JSONPointer | String pointer, untyped value) -> self
1294
-
1295
- # @param from [String | JSONPointer]
1296
- # @param pointer [String | JSONPointer]
1297
- # @return [self]
1298
- def move: (JSONPointer | String from, JSONPointer | String pointer) -> self
1299
-
1300
- # @param from [String | JSONPointer]
1301
- # @param pointer [String | JSONPointer]
1302
- # @return [self]
1303
- def copy: (JSONPointer | String from, JSONPointer | String pointer) -> self
1304
-
1305
- # @param pointer [String | JSONPointer]
1306
- # @param value [JSON-like value]
1307
- # @return [self]
1308
- def test: (JSONPointer | String pointer, untyped value) -> self
1309
-
1310
- # Apply this patch to JSON-like value _value_.
1311
- def apply: (untyped value) -> untyped
1312
-
1313
- def to_a: () -> Array[untyped]
1314
-
1315
- private
1316
-
1317
- def ensure_pointer: (JSONPointer | String pointer, Symbol op, Integer index) -> JSONPointer
1318
-
1319
- def build: (Array[Op | Hash[String, untyped]]) -> void
1320
-
1321
- def op_pointer: (Hash[String, untyped] obj, String key, String op, Integer index) -> JSONPointer
1322
-
1323
- def op_value: (Hash[String, untyped] obj, String key, String op, Integer index) -> untyped
1324
- end
1325
- end