transformJsonToMessage static method

Message transformJsonToMessage(
  1. Map<String, dynamic> json
)

Returns a new created Message with the information stored in the json.

The returned message is ready constructed and could be checked for it's type by using the is keyword.

Currently supported message types: UserMessage, ServiceRequest, ServiceReply, GetValueRequest, GetValueReply.

Throws a JsonMissingKeyException if there is a missing key in the json. Throws a InvalidJsonSchemaException if some values doesn't match the expected value type or if the BrokerKeys.messageType is unknown/unsupported.

Implementation

static Message transformJsonToMessage(Map<String, dynamic> json) {
  final String messageType = json.containsKey(BrokerKeys.messageType)
      ? json[BrokerKeys.messageType] as String
      : throw JsonMissingKeyException(
          BrokerKeys.messageType, json.toString());
  switch (messageType) {
    case BrokerKeys.userMessage:
      return UserMessage.fromJson(json);
    case BrokerKeys.serviceRequest:
      return ServiceRequest.fromJson(json);
    case BrokerKeys.serviceReply:
      return ServiceReply.fromJson(json);
    case BrokerKeys.getValueRequest:
      return GetValueRequest.fromJson(json);
    case BrokerKeys.getValueReply:
      return GetValueReply.fromJson(json);
    default:
      throw InvalidJsonSchemaException(
          'unknown/unsupported message type', json.toString());
  }
}