在使用Web Service/WCF获取一组对象集合时,这时必须对返回的对象进行序列化.
View Code
public class SerializerHelper
{
/// <summary>
/// 反序列化对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dataString"></param>
/// <param name="compress"></param>
/// <returns></returns>
public static T DeserializeObject<T>(string dataString, bool compress = true)
{
T obj = default(T);
if (!string.IsNullOrEmpty(dataString))
{
if (!(string.IsNullOrEmpty(dataString) || !compress))
{
dataString = CompressionHelper.DeCompress(dataString);
}
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(dataString)))
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
obj = (T)ser.ReadObject(ms);
ms.Close();
}
}
return obj;
}
/// <summary>
/// 反序列化对象
/// </summary>
/// <param name="serializeType"></param>
/// <param name="dataString"></param>
/// <param name="compress"></param>
/// <returns></returns>
public static object DeserializeObject(Type serializeType, string dataString, bool compress = true)
{
object obj = null;
if (!string.IsNullOrEmpty(dataString))
{
if (!(string.IsNullOrEmpty(dataString) || !compress))
{
dataString = CompressionHelper.DeCompress(dataString);
}
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(dataString)))
{
obj = new DataContractJsonSerializer(serializeType).ReadObject(ms);
ms.Close();
}
}
return obj;
}
/// <summary>
/// 序列化对象
/// </summary>
/// <param name="obj"></param>
/// <param name="compress"></param>
/// <returns></returns>
public static string SerializeObject(object obj, bool compress = true)
{
string result = "";
if (obj != null)
{
using (MemoryStream ms = new MemoryStream())
{
new DataContractJsonSerializer(obj.GetType()).WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
result = Encoding.UTF8.GetString(json, 0, json.Length);
}
if (!compress)
{
return result;
}
if (!string.IsNullOrEmpty(result))
{
result = CompressionHelper.Compress(result);
}
}
return result;
}
/// <summary>
/// 序列化对象
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] SerializeObjectToByte(object obj)
{
byte[] result = null;
string tempString = "";
using (MemoryStream ms = new MemoryStream())
{
new DataContractJsonSerializer(obj.GetType()).WriteObject(ms, obj);
byte[] json = ms.ToArray();
ms.Close();
tempString = Encoding.UTF8.GetString(json, 0, json.Length);
}
if (!string.IsNullOrEmpty(tempString))
{
result = CompressionHelper.CompressToByte(tempString);
}
return result;
}
压缩对象:
View Code
public class CompressionHelper
{
// Fields
public static CompressionType CompressionProvider = CompressionType.GZip;
// Methods
public static byte[] Compress(byte[] bytesToCompress)
{
MemoryStream ms = new MemoryStream();
Stream s = OutputStream(ms);
s.Write(bytesToCompress, 0, bytesToCompress.Length);
s.Close();
return ms.ToArray();
}
public static string Compress(string stringToCompress)
{
return Convert.ToBase64String(CompressToByte(stringToCompress));
}
public static byte[] CompressToByte(string stringToCompress)
{
return Compress(Encoding.Unicode.GetBytes(stringToCompress));
}
public static string DeCompress(string stringToDecompress)
{
string outString = string.Empty;
if (stringToDecompress == null)
{
throw new ArgumentNullException("stringToDecompress", "You tried to use an empty string");
}
try
{
byte[] inArr = Convert.FromBase64String(stringToDecompress.Trim());
outString = Encoding.Unicode.GetString(DeCompress(inArr));
}
catch (NullReferenceException nEx)
{
return nEx.Message;
}
return outString;
}
public static byte[] DeCompress(byte[] bytesToDecompress)
{
byte[] writeData = new byte[0x1000];
Stream s2 = InputStream(new MemoryStream(bytesToDecompress));
MemoryStream outStream = new MemoryStream();
while (true)
{
int size = s2.Read(writeData, 0, writeData.Length);
if (size > 0)
{
outStream.Write(writeData, 0, size);
}
else
{
s2.Close();
byte[] outArr = outStream.ToArray();
outStream.Close();
return outArr;
}
}
}
private static Stream InputStream(Stream inputStream)
{
switch (CompressionProvider)
{
case CompressionType.GZip:
return new GZipInputStream(inputStream);
case CompressionType.BZip2:
return new BZip2InputStream(inputStream);
case CompressionType.Zip:
return new ZipInputStream(inputStream);
}
return new GZipInputStream(inputStream);
}
private static Stream OutputStream(Stream inputStream)
{
switch (CompressionProvider)
{
case CompressionType.GZip:
return new GZipOutputStream(inputStream);
case CompressionType.BZip2:
return new BZip2OutputStream(inputStream);
case CompressionType.Zip:
return new ZipOutputStream(inputStream);
}
return new GZipOutputStream(inputStream);
}
}