示例#1
0
        /// <summary>
        /// Deserializes provided json string into a <see cref="WebMessageDTO{T}"/> object.
        /// Before attempting to invoke this method on an aquired web message you should first use <see cref="TryParseRequest"/>
        ///     to check if it is a web request instead of a json web message.
        /// This method also verifies that the type of the web message data is appropriate for its <see cref="WebMessageType"/>.
        /// Throws an exception if the deserialization fails.
        /// </summary>
        /// <param name="jsonData">A json string representation of <see cref="WebMessageDTO{T}"/>.</param>
        /// <returns>A valid <see cref="WebMessageDTO{T}"/> instance.</returns>
        public WebMessageDTO <object> DeserializeWebMessage(string jsonData)
        {
            if (string.IsNullOrWhiteSpace(jsonData))
            {
                throw new ArgumentException("Provided JSON data is either empty or null!");
            }
            WebMessageType messageType = GetMessageType(jsonData);

            if (!MessageDataTypes.ContainsKey(messageType))
            {
                throw new InvalidEnumArgumentException($"Encountered unsupported message type '{messageType}'!");
            }

            Type genericType = MessageDTOTypes[messageType];

            return(RecastDeserializedMessage(JsonConvert.DeserializeObject(jsonData, genericType, SerializerSettings)));
        }
示例#2
0
        /// <summary>
        /// Returns a json string representing a <see cref="WebMessageDTO{T}"/> with given <see cref="WebMessageType"/> and <see cref="data"/> object.
        /// Throws an exception if provided data is null or of an invalid type.
        /// </summary>
        /// <param name="messageType">Web message type.</param>
        /// <param name="data">Web message data.</param>
        /// <returns>A json string representing a <see cref="WebMessageDTO{T}"/>.</returns>
        public string SerializeWebMessage(WebMessageType messageType, object data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            if (!MessageDataTypes.ContainsKey(messageType))
            {
                throw new InvalidEnumArgumentException($"Encountered unsupported message type '{messageType}'!");
            }

            Type dataType = MessageDataTypes[messageType];

            if (data.GetType() != dataType && !data.GetType().IsInstanceOfType(dataType))
            {
                throw new ArgumentException($"Invalid data type! Expected {dataType}, got {data.GetType()}.");
            }

            Type genericType = MessageDTOTypes[messageType];

            return(JsonConvert.SerializeObject(
                       Activator.CreateInstance(genericType, messageType, data), SerializerSettings));
        }
 public MessageDataTypesCreateOrUpdateCommand(MessageDataTypes ent)
 {
     this.ent = ent;
 }