parseExpression function

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

Parses a JSON path expression and returns the corresponding value from the JSON.

Implementation

JsonValue parseExpression(String jsonPath, int index, JsonValue value) {
  _log('Parsing expression', jsonPath.substring(0, index), value);
  if (index >= jsonPath.length) {
    _log(
      'Reached end of JSON path and returning value',
      jsonPath.substring(0, index),
      value,
    );
    return value;
  }

  if (jsonPath[index] == '.') {
    index++;
    if (index >= jsonPath.length) {
      throw const FormatException('Invalid JSON path syntax');
    }
    if (jsonPath[index] == '.') {
      index++;
      final result = parseRecursiveDescent(jsonPath, index, value);
      _log('Recursive descent result', jsonPath.substring(0, index), result);
      return result;
    } else {
      return parseDotNotation(jsonPath, index, value);
    }
  } else if (jsonPath[index] == '[') {
    index++;
    return parseBracketNotation(jsonPath, index, value);
  } else if (jsonPath[index] == '*') {
    index++;
    return parseWildcard(jsonPath, index, value);
  } else {
    return parseDotNotation(jsonPath, index, value);
  }
}