https://github.com/f3ath/rfc-6902-dart
JSON Patch (RFC 6902) implementation in Dart
https://github.com/f3ath/rfc-6902-dart
Last synced: 9 months ago
JSON representation
JSON Patch (RFC 6902) implementation in Dart
- Host: GitHub
- URL: https://github.com/f3ath/rfc-6902-dart
- Owner: f3ath
- License: mit
- Created: 2021-01-22T22:32:28.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2024-10-31T17:09:34.000Z (about 1 year ago)
- Last Synced: 2025-04-17T10:26:26.160Z (9 months ago)
- Language: Dart
- Size: 17.6 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# [RFC 6902] JSON Patch
JSON Patch ([RFC 6902]) implementation in Dart.
## Building and encoding to JSON
```dart
import 'dart:convert';
import 'package:rfc_6902/rfc_6902.dart';
void main() {
final patch = JsonPatch.build([
Test(JsonPointer('/a/b/c'), 'foo'),
Remove(JsonPointer('/a/b/c')),
Add(JsonPointer('/a/b/c'), ['foo', 'bar']),
Replace(JsonPointer('/a/b/c'), 42),
Move(JsonPointer('/a/b/c'), JsonPointer('/a/b/d')),
Copy(JsonPointer('/a/b/d'), JsonPointer('/a/b/e')),
]);
print(jsonEncode(patch));
}
```
produces
```
[
{"op":"test","path":"/a/b/c","value":"foo"},
{"op":"remove","path":"/a/b/c"},
{"op":"add","path":"/a/b/c","value":["foo","bar"]},
{"op":"replace","path":"/a/b/c","value":42},
{"op":"move","from":"/a/b/c","path":"/a/b/d"},
{"op":"copy","from":"/a/b/d","path":"/a/b/e"}
]
```
## Parsing JSON and applying to the document
```dart
import 'dart:convert';
import 'package:rfc_6902/rfc_6902.dart';
void main() {
const document = {
'a': {
'b': {'c': 'foo'}
}
};
const json = '''
[
{ "op": "test", "path": "/a/b/c", "value": "foo" },
{ "op": "remove", "path": "/a/b/c" },
{ "op": "add", "path": "/a/b/c", "value": [ "foo", "bar" ] },
{ "op": "replace", "path": "/a/b/c", "value": 42 },
{ "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
{ "op": "copy", "from": "/a/b/d", "path": "/a/b/e" }
]
''';
final patch = JsonPatch(jsonDecode(json));
final result = patch.applyTo(document);
print(result); // {a: {b: {d: 42, e: 42}}}
}
```
[RFC 6902]: https://tools.ietf.org/html/rfc6902