BackupJsonExtension<K, T> extension

Extension providing JSON backup and restore capabilities for BoxInterface.

This extension enables any BoxInterface implementation to export its contents as a JSON string and to restore its state from such a JSON backup. This is useful for data migration, backup, and interoperability with other systems.

Example

// Export box contents to JSON
final json = await box.generateBackupJson(
  keyToString: (key) => key.toString(),
  valueToJson: (value) => value.toJson(),
);

// Restore box contents from JSON
await box.restoreBackupJson(
  json,
  stringToKey: (str) => int.parse(str),
  jsonToValue: (json) => MyModel.fromJson(json),
);
on

Methods

generateBackupJson({String keyToString(K key)?, Object? valueToJson(T value)?}) Future<String>

Available on BoxInterface<K, T>, provided by the BackupJsonExtension extension

Generates a JSON string representing all key-value pairs in the box.
restoreBackupJson(String json, {required K stringToKey(String key), required T jsonToValue(dynamic json)}) Future<void>

Available on BoxInterface<K, T>, provided by the BackupJsonExtension extension

Restores the box contents from a JSON string backup.