연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
JSON Pointer (RFC 6901) implementation in Dart.
Features:
- Reads, writes, removes values
- Fully implements the standard, including the
- character
- Can build the entire path, e.g.
/foo/bar/-/baz
- No external dependencies
- 100% test coverage
Constructing pointers
A JSON Pointer object can be parsed from a string expression.
The expression MUST be a valid non-empty JSON Pointer expression with "/" and "~" escaped.
final pointer = JsonPointer('/foo/0/bar');
final alsoPointer = JsonPointer('/foo/-');
final emptyPointer = JsonPointer();
JsonPointer('oops');
JsonPointer('/foo/~');
It can also be built from individual reference tokens. In such case, it will escape the tokens automatically.
final pointer = JsonPointer.build(['foo', '0', 'bar/']);
final longerPointer = JsonPointerSegment('-', pointer);
final evenLongerPointer = JsonPointerSegment('wow', longerPointer);
Reading and writing values
The read() method returns the referenced value. The write() method returns a copy of the document with
the referenced value replaced by the new value.
import 'dart:convert';
import 'package:rfc_6901/rfc_6901.dart';
void main() {
const json = '{"foo": [{"bar": 42}]}';
final document = jsonDecode(json);
print('Original document: $document');
[
'/foo',
'/foo/0',
'/foo/0/bar',
].forEach((expression) {
final pointer = JsonPointer(expression);
print('Pointer "$pointer" reads ${pointer.read(document)}');
});
[
'/foo/0/bar',
'/foo/-',
'/a/b/-/c/d',
].forEach((expression) {
final pointer = JsonPointer(expression);
final d = pointer.write(document, 'banana');
print('Add a banana at "$pointer": $d');
});
}