fromJson static method

Message? fromJson(
  1. Map<String, dynamic> json
)
override

Decodes a relay message from json.

Returns null for type codes outside the relay range, so it can be chained with other decoders (like Message.fromJson).

Implementation

static Message? fromJson(Map<String, dynamic> json) {
  final type = json['type'] as int;
  if (type < RelayMessageType.relayHello.value ||
      type > RelayMessageType.relayStateRequest.value) {
    return null;
  }

  switch (RelayMessageType.values[type - RelayMessageType.relayHello.value]) {
    case RelayMessageType.relayHello:
      return RelayHelloMessage.fromJson(json);
    case RelayMessageType.relayWelcome:
      return RelayWelcomeMessage.fromJson(json);
    case RelayMessageType.relayPush:
      return RelayPushMessage.fromJson(json);
    case RelayMessageType.relayAck:
      return RelayAckMessage.fromJson(json);
    case RelayMessageType.relayChanges:
      return RelayChangesMessage.fromJson(json);
    case RelayMessageType.relaySnapshotUpload:
      return RelaySnapshotUploadMessage.fromJson(json);
    case RelayMessageType.relayStateRequest:
      return RelayStateRequestMessage.fromJson(json);
  }
}