jsonPointer static method

Object? jsonPointer(
  1. Object? value,
  2. String pointer
)

Resolves a JSON Pointer against value.

Implementation

static Object? jsonPointer(Object? value, String pointer) {
  if (pointer.isEmpty || pointer == '/') return value;
  var current = value;
  for (final token in pointer.split('/').skip(1)) {
    final key = token.replaceAll('~1', '/').replaceAll('~0', '~');
    if (current is Map) {
      current = current[key];
    } else if (current is List) {
      final index = int.tryParse(key);
      if (index == null) return null;
      if (index < 0 || index >= current.length) return null;
      current = current[index];
    } else {
      return null;
    }
  }
  return current;
}