Class: JSONP3::JSONPathEnvironment

Inherits:
Object
  • Object
show all
Defined in:
lib/json_p3/environment.rb

Overview

JSONPath configuration

Configure an environment by inheriting from JSONPathEnvironment and setting one or more constants and/or overriding #setup_function_extensions.

Constant Summary collapse

MAX_INT_INDEX =

The maximum integer allowed when selecting array items by index.

(2**53) - 1
MIN_INT_INDEX =

The minimum integer allowed when selecting array items by index.

-(2**53) + 1
MAX_RECURSION_DEPTH =

The maximum number of arrays and hashes the recursive descent segment will traverse before raising a JSONP3::JSONPathRecursionError.

100
NAME_SELECTOR =

One of the available implementations of the name selector.

  • NameSelector (the default) will select values from hashes using string keys.
  • SymbolNameSelector will select values from hashes using string or symbol keys.

Implement your own name selector by inheriting from NameSelector and overriding #resolve.

NameSelector
INDEX_SELECTOR =

An implementation of the index selector. The default implementation will select values from arrays only. Implement your own by inheriting from IndexSelector and overriding #resolve.

IndexSelector

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJSONPathEnvironment

Returns a new instance of JSONPathEnvironment.



44
45
46
47
48
# File 'lib/json_p3/environment.rb', line 44

def initialize
  @parser = Parser.new(self)
  @function_extensions = {}
  setup_function_extensions
end

Instance Attribute Details

#function_extensionsObject

Returns the value of attribute function_extensions.



42
43
44
# File 'lib/json_p3/environment.rb', line 42

def function_extensions
  @function_extensions
end

Instance Method Details

#compile(query) ⇒ JSONPath

Prepare JSONPath expression query for repeated application.

Parameters:

  • query (String)

Returns:



53
54
55
56
# File 'lib/json_p3/environment.rb', line 53

def compile(query)
  tokens = JSONP3.tokenize(query)
  JSONPath.new(self, @parser.parse(tokens))
end

#find(query, value) ⇒ Array<JSONPath>

Apply JSONPath expression query to value.

Parameters:

  • query (String)

    the JSONPath expression

  • value (JSON-like data)

    the target JSON "document"

Returns:



62
63
64
# File 'lib/json_p3/environment.rb', line 62

def find(query, value)
  compile(query).find(value)
end

#setup_function_extensionsObject

Override this function to configure JSONPath function extensions. By default, only the standard functions described in RFC 9535 are enabled.



68
69
70
71
72
73
74
# File 'lib/json_p3/environment.rb', line 68

def setup_function_extensions
  @function_extensions["length"] = Length.new
  @function_extensions["count"] = Count.new
  @function_extensions["value"] = Value.new
  @function_extensions["match"] = Match.new
  @function_extensions["search"] = Search.new
end