Exemple #1
0
        public byte[] EncodeMessage(object message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("The message cannot be null.");
            }

            var jsonString = JsonConvert.SerializeObject(message);

            if (jsonString == null)
            {
                throw new Exception("Serialize message gets empty content.");
            }

            var uncompress = Encoding.UTF8.GetBytes(jsonString);

            return(GZipCompression.Compress(uncompress));
        }
        public T DecodeMessage <T>(byte[] data, int dataOffset, int dataLength)
        {
            if (data == null)
            {
                throw new ArgumentNullException("The message cannot be null.");
            }

            if (dataOffset >= data.Length)
            {
                throw new ArgumentException("The parameter 'dataOffset' should less then length of data");
            }

            string jsonStr = Encoding.UTF8.GetString(GZipCompression.Decompress(data, dataOffset, dataLength));

            if (string.IsNullOrWhiteSpace(jsonStr))
            {
                throw new Exception("Deserialize message gets empty content.");
            }

            return(JsonConvert.DeserializeObject <T>(jsonStr));
        }