https://github.com/oddbit/dart-json-delta
Dart extension for serializing and an object to a JSON delta.
https://github.com/oddbit/dart-json-delta
dart flutter json json-diff json-serialization
Last synced: 4 months ago
JSON representation
Dart extension for serializing and an object to a JSON delta.
- Host: GitHub
- URL: https://github.com/oddbit/dart-json-delta
- Owner: oddbit
- License: apache-2.0
- Created: 2024-03-22T06:29:50.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-25T10:28:18.000Z (almost 2 years ago)
- Last Synced: 2025-07-02T00:09:32.796Z (8 months ago)
- Topics: dart, flutter, json, json-diff, json-serialization
- Language: Dart
- Homepage: https://pub.dev/packages/json_delta
- Size: 12.7 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Introducing a mixin class that implements a `toJsonDelta()` method that serializes
the object to a JSON map of values of the changes in the object.
## Usage
```dart
final person = Person("John", "Doe", 30);
person.lastName = "Smith";
person.age = 31;
final delta = person.toJsonDelta(); // {"lastName": "Smith", "age": 31}
```
### Implement in your models
An important note is that you call the `saveJsonDeltaState()` in the model's
constructor to set an initial state to compare against later.
```dart
class Person extends JsonSerializable with JsonDelta {
String firstName;
String lastName;
int age;
Person({
required this.firstName,
required this.lastName,
required this.age,
}) {
// Save the initial state of your object in the constructor
saveJsonDeltaState();
}
@override
Map toJson() {
return {
'firstName': firstName,
'lastName': lastName,
'age': age,
};
}
}
```
## Additional information
This package is compatible with [`json_serializable`](https://pub.dev/packages/json_serializable)
which facilitates your generation of JSON.