.NET C# 数据网络传输前后的序列及反序列化

1.工具类

using System.Collections.Generic;
using Newtonsoft.Json;

namespace Infrastructure
{
    public static class DataTransferExtensions
    {
        /// <summary>
        /// 获取服务端传输过来的数据,将其Base64解码,解压缩,恢复为List T
        /// </summary>
        public static List<T> GetCompressTransferDatas<T>(string sourceString) where T : class
        {
            if (string.IsNullOrEmpty(sourceString)) return new List<T>();
            List<T> list = JsonConvert.DeserializeObject<List<T>>(CompressionExtensions.DecompressString(CompressionExtensions.FromBase64String(sourceString)));
            return list;
        }

        /// <summary>
        /// 获取服务端传输过来的数据,将其Base64解码,解压缩,恢复为T
        /// </summary>
        public static T GetCompressTransferData<T>(string sourceString) where T : class
        {
            if (string.IsNullOrEmpty(sourceString)) return default(T);
            T result = JsonConvert.DeserializeObject<T>(CompressionExtensions.DecompressString(CompressionExtensions.FromBase64String(sourceString)));
            return result;
        }

        /// <summary>
        /// 获取服务端传输过来的数据,将其Base64解码,解压缩,恢复为JSON格式字串
        /// </summary>
        public static string GetCompressTransferDataJson(string sourceString)
        {
            if (string.IsNullOrEmpty(sourceString)) return string.Empty;
            return CompressionExtensions.DecompressString(CompressionExtensions.FromBase64String(sourceString));
        }

        /// <summary>
        /// 根据传值过来的List T 将其转换为对应的压缩字串,并将其Base64编码
        /// </summary>
        public static string SetCompressTransferDataCompressed<T>(List<T> list) where T : class
        {
            if (list == null || list.Count == 0) return string.Empty;
            string json = JsonConvert.SerializeObject(list);
            return CompressionExtensions.ToBase64String(CompressionExtensions.CompressString(json));
        }

        /// <summary>
        /// 根据传值过来的T 将其转换为对应的压缩字串,并将其Base64编码
        /// </summary>
        public static string SetCompressTransferDataCompressed<T>(T instance) where T : class
        {
            if (instance == null) return string.Empty;
            string json = JsonConvert.SerializeObject(instance);
            return CompressionExtensions.ToBase64String(CompressionExtensions.CompressString(json));
        }

        /// <summary>
        /// JSON数据转List T 数据
        /// </summary>
        public static List<T> GetTransferDatas<T>(string sourceString) where T : class
        {
            if (string.IsNullOrEmpty(sourceString)) return new List<T>();
            List<T> list = JsonConvert.DeserializeObject<List<T>>(sourceString);
            return list;
        }

        /// <summary>
        /// JSON数据转T 数据
        /// </summary>
        public static T GetTransferData<T>(string sourceString) where T : class
        {
            if (string.IsNullOrEmpty(sourceString)) return default(T);
            T instance = JsonConvert.DeserializeObject<T>(sourceString);
            return instance;
        }

        /// <summary>
        /// List T 数据转JSON数据
        /// </summary>
        public static string SetTransferDataJson<T>(List<T> list) where T : class
        {
            if (list == null || list.Count == 0) return string.Empty;
            return JsonConvert.SerializeObject(list);
        }

        /// <summary>
        /// T 数据转JSON数据
        /// </summary>
        public static string SetTransferDataJson<T>(T instance) where T : class
        {
            if (instance == null) return string.Empty;
            return JsonConvert.SerializeObject(instance);
        }
    }
}
2.使用方法

List<T> items = new List<T>();

xxx //items赋值

string strs = DataTransferExtensions.SetCompressTransferDataCompressed(items);//序列化

List<T> temps = DataTransferExtensions.GetTransferDatas<T>(strs);//反序列化

.NET C# 数据网络传输前后的序列及反序列化

上一篇:windows7 php 环境架设


下一篇:c# 转16进制