parseBracketNotation function

JsonValue parseBracketNotation(
  1. String jsonPath,
  2. int index,
  3. JsonValue value
)

Parses a JSON path expression in bracket notation and returns the corresponding value.

Implementation

JsonValue parseBracketNotation(String jsonPath, int index, JsonValue value) {
  _log('Parsing bracket notation', jsonPath.substring(0, index), value);
  if (jsonPath[index] == "'") {
    index++;
    final fieldName = parseQuotedFieldName(jsonPath, index);
    index += fieldName.length + 1;
    _log(
      'Parsed quoted field name: $fieldName',
      jsonPath.substring(0, index),
      value,
    );
    expectChar(jsonPath, index, ']');
    index++;
    return parseExpression(jsonPath, index, value[fieldName]);
  } else if (jsonPath[index] == '*') {
    index++;
    expectChar(jsonPath, index, ']');
    index++;
    return parseWildcard(jsonPath, index, value);
  } else {
    final indexValue = parseIndex(jsonPath, index);
    index += indexValue.toString().length;
    _log('Parsed index: $indexValue', jsonPath.substring(0, index), value);
    expectChar(jsonPath, index, ']');
    index++;
    if (value is JsonArray) {
      _log(
        'Accessing array element at index: $indexValue',
        jsonPath.substring(0, index),
        value,
      );
      return parseExpression(jsonPath, index, value.value[indexValue]);
    } else {
      _log(
        'Value is not a JsonArray, returning Undefined',
        jsonPath.substring(0, index),
        value,
      );
      return const Undefined();
    }
  }
}