parseRecursiveDescent function

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

Parses a JSON path expression with recursive descent and returns the corresponding value.

Implementation

JsonValue parseRecursiveDescent(String jsonPath, int index, JsonValue value) {
  _log('Parsing recursive descent', jsonPath.substring(0, index), value);

  if (value is JsonObject) {
    if (index < jsonPath.length && jsonPath[index] == '[') {
      index++;
      return parseBracketNotation(jsonPath, index, value);
    } else {
      final fieldName = parseFieldName(jsonPath, index);
      index += fieldName.length;
      final result =
          parseRecursiveDescent(jsonPath, index, value.getValue(fieldName));
      if (result is! Undefined) {
        return result;
      }
      return const Undefined();
    }
  } else if (value is JsonArray) {
    if (index < jsonPath.length && jsonPath[index] == '[') {
      index++;
      return parseBracketNotation(jsonPath, index, value);
    } else {
      return const Undefined();
    }
  } else {
    // Return the scalar value itself
    _log('Returning scalar value', jsonPath.substring(0, index), value);
    return value;
  }
}