Class: Datadog::DI::SnapshotPruner::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/di/snapshot_pruner/parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Builds the tree of JSON objects of an encoded snapshot in one scan, recording each object's byte range and nesting level. Creates a node for each JSON object.

Constant Summary collapse

STRUCTURAL_BYTE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Bytes the scanner stops at outside strings.

/[{}"]/
STRING_REMAINDER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Remainder of a string after its opening quote, through the closing quote, honoring backslash escapes.

/[^"\\]*+(?:\\.[^"\\]*+)*+"/m
NOT_CAPTURED_REASON =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Member name whose presence flags an object as not captured.

"notCapturedReason"
NOT_CAPTURED_REASON_REMAINDER_SIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Encoded length of the notCapturedReason name after its opening quote, including the closing quote.

NOT_CAPTURED_REASON.bytesize + 1
MEMBER_SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Separator between a member name and its value.

/\s*:\s*/
DEPTH_VALUE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The notCapturedReason value that flags an object as truncated for depth.

/"depth"/
OPEN_BRACE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Byte value of the opening brace.

"{".ord
CLOSE_BRACE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Byte value of the closing brace.

"}".ord

Instance Method Summary collapse

Instance Method Details

#parse(json) ⇒ Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Builds the object tree of an encoded snapshot.

A notCapturedReason member name flags its object: a string value with the same text is skipped like any other value.

Parameters:

  • json (String) —

    the encoded snapshot, a JSON object

Returns:

  • (Node) —

    the root object

Raises:

  • (ArgumentError) —

    when the document holds an unclosed string, a closing brace with no open object, or no closed root object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/datadog/di/snapshot_pruner/parser.rb', line 51

def parse(json)
  scanner = StringScanner.new(json)
  open_nodes = []
  while scanner.skip_until(STRUCTURAL_BYTE)
    offset = scanner.pos - 1
    case json.getbyte(offset)
    when OPEN_BRACE
      node = Node.new(start_offset: offset, level: open_nodes.size, parent: open_nodes.last)
      open_nodes.last&.add_child(node)
      open_nodes.push(node)
    when CLOSE_BRACE
      node = open_nodes.pop
      raise ArgumentError, "closing brace at byte #{offset} without an open object" unless node

      node.close(scanner.pos)
      return node if open_nodes.empty?
    else
      unless skip_string(scanner, open_nodes.last)
        raise ArgumentError, "unclosed string starting at byte #{offset}"
      end
    end
  end
  raise ArgumentError, "no closed root object in the document"
end