数据序列化处理之经验积累(3) -- ObjectStateFormatter
2009-03-29 15:16
375 查看
这个序列化类,优势在于对基础类型存储在hashtable,pair,tripet等数据结构里的时候,序列化相对于binaryformatter和datacontractserializer有明显的优势。
public static class ObjectStateFormatSerializer
{
private static readonly ObjectStateFormatter InnerStateFormatter = new ObjectStateFormatter();
private readonly static MemoryStreamStack MemoryStreamStacker = new MemoryStreamStack();
public static string SerializeToBase64String(object obj, bool compress)
{
byte[] bytes = Serialize(obj, compress);
return Convert.ToBase64String(bytes);
}
public static T DeserializeFromBase64String<T>(string baseString, bool compress)
{
if (String.IsNullOrEmpty(baseString))
return default(T);
byte[] buf = Convert.FromBase64String(baseString);
return Deserialize<T>(buf, compress);
}
public static byte[] Serialize(object obj, bool compress)
{
if (obj == null)
{
return null;
}
byte[] buf = null;
MemoryStream stream = MemoryStreamStacker.GetMemoryStream();
try
{
InnerStateFormatter.Serialize(stream, obj);
stream.SetLength(stream.Position);
buf = stream.ToArray();
if (compress)
{
buf = CompressHelper.CompressBytes(buf);
}
}
finally
{
MemoryStreamStacker.ReleaseMemoryStream(stream);
}
return buf;
}
public static T Deserialize<T>(byte[] info, bool compress)
{
T ret = default(T);
if (info == null || info.Length <= 0)
{
return ret;
}
if (compress)
{
info = CompressHelper.DeCompressBytes(info);
}
MemoryStream stream = MemoryStreamStacker.GetMemoryStream();
try
{
stream.Write(info, 0, info.Length);
stream.Position = 0L;
ret = (T)InnerStateFormatter.Deserialize(stream);
}
finally
{
MemoryStreamStacker.ReleaseMemoryStream(stream);
}
return ret;
}
}
最后对我写的这3篇关于序列化的文章,做个小结,序列化和压缩要慎用,因为它们是吃cpu和内存的大户,除非非不得已或者业务量不是非常大的情况下可以使用,在高性能高吞吐量的系统中,与其直接用stirng的format来做对象的值的序列化后来传递和保存,也不要用它们,否则你就会看到你的服务器的cpu一直在 100%,而这个时候你并不知道你的服务器到底已经超负荷到什么程度了~~~
public static class ObjectStateFormatSerializer
{
private static readonly ObjectStateFormatter InnerStateFormatter = new ObjectStateFormatter();
private readonly static MemoryStreamStack MemoryStreamStacker = new MemoryStreamStack();
public static string SerializeToBase64String(object obj, bool compress)
{
byte[] bytes = Serialize(obj, compress);
return Convert.ToBase64String(bytes);
}
public static T DeserializeFromBase64String<T>(string baseString, bool compress)
{
if (String.IsNullOrEmpty(baseString))
return default(T);
byte[] buf = Convert.FromBase64String(baseString);
return Deserialize<T>(buf, compress);
}
public static byte[] Serialize(object obj, bool compress)
{
if (obj == null)
{
return null;
}
byte[] buf = null;
MemoryStream stream = MemoryStreamStacker.GetMemoryStream();
try
{
InnerStateFormatter.Serialize(stream, obj);
stream.SetLength(stream.Position);
buf = stream.ToArray();
if (compress)
{
buf = CompressHelper.CompressBytes(buf);
}
}
finally
{
MemoryStreamStacker.ReleaseMemoryStream(stream);
}
return buf;
}
public static T Deserialize<T>(byte[] info, bool compress)
{
T ret = default(T);
if (info == null || info.Length <= 0)
{
return ret;
}
if (compress)
{
info = CompressHelper.DeCompressBytes(info);
}
MemoryStream stream = MemoryStreamStacker.GetMemoryStream();
try
{
stream.Write(info, 0, info.Length);
stream.Position = 0L;
ret = (T)InnerStateFormatter.Deserialize(stream);
}
finally
{
MemoryStreamStacker.ReleaseMemoryStream(stream);
}
return ret;
}
}
最后对我写的这3篇关于序列化的文章,做个小结,序列化和压缩要慎用,因为它们是吃cpu和内存的大户,除非非不得已或者业务量不是非常大的情况下可以使用,在高性能高吞吐量的系统中,与其直接用stirng的format来做对象的值的序列化后来传递和保存,也不要用它们,否则你就会看到你的服务器的cpu一直在 100%,而这个时候你并不知道你的服务器到底已经超负荷到什么程度了~~~
相关文章推荐
- 数据序列化处理之经验积累(3) -- ObjectStateFormatter
- 数据序列化处理之经验积累(3) -- ObjectStateFormatter
- 数据序列化处理之经验积累(1)-BinaryFormatter
- 数据序列化处理之经验积累(1)-BinaryFormatter
- 数据序列化处理之经验积累(2) -- Datacontract formatter
- 数据序列化处理之经验积累(1)-BinaryFormatter
- 数据序列化之经验积累(2) -- Datacontract formatter
- 处理:SqlServer FOR XML 无法对节点 'NoName' 的数据进行序列化,因为它包含在 XML 中不允许出现的字符(0x0000)。
- 数据序列化之经验积累(2) -- Datacontract formatter
- 用JSONObject解析和处理json数据
- 用JSONObject解析和处理json数据
- iOS积累--objectForKey与valueForKey在NSDictionary中的差异
- 将表单序列化类型的数据转化成对象的处理(允许对象中包含对象)
- 基于JavaScript将表单序列化类型的数据转化成对象的处理(允许对象中包含对象)
- 基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理
- MPAndroidChart 教程:数据格式器 ValueFormatter(五)
- 对于Packet for query is too large(mysql写入数据过大) 问题的处理
- linux shell 读取for循环中出现难处理的数据之单引号错误实例
- 将表单序列化类型的数据转化成对象的处理(允许对象中包含对象)
- 用JSONObject解析和处理json数据