fromJson static method

SaveEnvelope fromJson(
  1. Map<String, dynamic> json
)

Parses an envelope from a JSON-safe map.

Implementation

static SaveEnvelope fromJson(Map<String, dynamic> json) {
  final schemaVersion = json['schemaVersion'];
  final createdAtMs = json['createdAtMs'];
  final updatedAtMs = json['updatedAtMs'];
  final payload = json['payload'];
  final changeSet = json['changeSet'];
  final saveReason = json['saveReason'];
  final checksum = json['checksum'];

  if (schemaVersion is! int || createdAtMs is! int || updatedAtMs is! int) {
    throw const FormatException('Invalid envelope metadata');
  }
  if (payload is! Map<String, dynamic>) {
    throw const FormatException('Invalid envelope payload');
  }
  SaveChangeSet? parsedChangeSet;
  if (changeSet != null) {
    if (changeSet is! Map<String, dynamic>) {
      throw const FormatException('Invalid envelope change set');
    }
    parsedChangeSet = SaveChangeSet.fromJson(changeSet);
  }
  if (saveReason != null && saveReason is! String) {
    throw const FormatException('Invalid envelope save reason');
  }
  if (checksum != null && checksum is! String) {
    throw const FormatException('Invalid envelope checksum');
  }

  return SaveEnvelope(
    schemaVersion: schemaVersion,
    createdAtMs: createdAtMs,
    updatedAtMs: updatedAtMs,
    payload: payload,
    changeSet: parsedChangeSet,
    saveReason: saveReason as String?,
    checksum: checksum as String?,
  );
}