将对象序列化为最小的 UTF8 兼容大小
我有一个非常简单的类,其中包含一些原始类型和一些主要是枚举的集合。现在我需要将此对象序列化为 UTF8 兼容的最小可能大小。
这是我需要序列化的类
public class Context
{
public Hashtable UserModuleRoles { get; set; }
public Dictionary<string, object> CustomSettings { get; set; }
public int Uid { get; set; }
public int Id { get; set; }
public int ActiveId { get; set; }
public byte Default { get; set; }
public SetEnum Ident { get; set; }
public string Name { get; set; }
public sbyte State { get; set; }
public DateTime Date { get; set; }
}
。
这就是我序列化对象的方式
public string Serialize(object serializeObject)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, serializeObject);
byte[] data = stream.ToArray();
stream.Dispose();
stream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("data", data);
zip.Save(stream);
}
data = stream.ToArray();
stream.Dispose();
return Convert.ToBase64String(data);
}
在我的第一次尝试中,我序列化了对象,压缩该内容(大约小 1/3)并将其转换为 Base64 字符串。但是base64的开销相当大,有1/3,而且我知道有base128但我不知道如何开始,我对base128编码的搜索不成功。
或者还有其他方法可以做到这一点吗?
如果没有最好的方法是什么
这是 base128?
编辑:
我使用整个“Context”对象测试了 ObjectStateFormatter 类,这会导致多 8 个字节且序列化/反序列化速度变慢。也许我只能在属性上而不是整个类上使用它?
I've got an quite simple class that contains some primitive types and some collections with mostly enums. Now I need to serialize this object to the smallest possible size that is UTF8 compatible.
This is the class I need to serialize
public class Context
{
public Hashtable UserModuleRoles { get; set; }
public Dictionary<string, object> CustomSettings { get; set; }
public int Uid { get; set; }
public int Id { get; set; }
public int ActiveId { get; set; }
public byte Default { get; set; }
public SetEnum Ident { get; set; }
public string Name { get; set; }
public sbyte State { get; set; }
public DateTime Date { get; set; }
}
.
This is how I serialize the object
public string Serialize(object serializeObject)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter b = new BinaryFormatter();
b.Serialize(stream, serializeObject);
byte[] data = stream.ToArray();
stream.Dispose();
stream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("data", data);
zip.Save(stream);
}
data = stream.ToArray();
stream.Dispose();
return Convert.ToBase64String(data);
}
In my first attempt I serialize the object, zip that content (about 1/3 smaller) and convert it to a base64 string. But base64 has a quite big overhead of 1/3 and I know there is base128 but I don't know how to start and my search for base128 encoding was unsuccessful.
Or is there any other way to do this?
And if not how is the best way to
this as base128?
Edit:
I tested the ObjectStateFormatter Class with the whole "Context" object which results in 8byte more and slower serialization/deserialization. Maybe I had to use it just on the properties instead of the whole class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,如果您知道 Base64 是如何完成的,那么 Base128 并不难。荷兰语维基百科很好地描述了该过程(为您翻译):
该转换表包含 128 个兼容的 UTF8 字符,例如:
唯一的要求是发送方和接收方的转换表相同。
Well, base128 is not that hard, if you know how base64 is done. The dutch wikipedia describes the process well (translated it for you):
That translation table contains 128 compatible UTF8 characters, for example:
The only requirement is that translation table is the same at both sender and reciever.
尝试使用
objectstateformatter
而不是BinaryFormatter
,可能会得到更小的大小。这一切都取决于您序列化的数据。Try using the
objectstateformatter
instead of theBinaryFormatter
, might give you a smaller size. It all depends on the data your serializing.