fromJson static method

TomSecretsPairingHandover? fromJson(
  1. Object? json
)

The handover in json, or null when it is not a pairing handover. Throws FormatException for a pairing handover that misses a member.

Implementation

static TomSecretsPairingHandover? fromJson(Object? json) {
  if (json is! Map || json['mode'] != 'pair') return null;
  final port = json['port'];
  for (final key in ['dataDirectory', 'address', 'code', 'device']) {
    if (json[key] is! String || (json[key] as String).isEmpty) {
      throw FormatException('the pairing handover names $key');
    }
  }
  if (port is! int || port < 1 || port > 65535) {
    throw const FormatException('the pairing handover names a port');
  }
  final link = json['approvalLink'];
  if (link != null) {
    if (link is! String) {
      throw const FormatException('approvalLink is address:port');
    }
    final problem = tomSecretsApprovalLinkProblem(link);
    if (problem != null) throw FormatException(problem);
  }
  return TomSecretsPairingHandover(
    dataDirectory: json['dataDirectory'] as String,
    address: json['address'] as String,
    port: port,
    code: json['code'] as String,
    device: json['device'] as String,
    deviceKey: json['deviceKey'] as String?,
    approvalLink: link as String?,
  );
}