将对象序列化为最小的 UTF8 兼容大小

发布于 2024-11-03 07:34:23 字数 1451 浏览 0 评论 0原文

我有一个非常简单的类,其中包含一些原始类型和一些主要是枚举的集合。现在我需要将此对象序列化为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-11-10 07:34:23

好吧,如果您知道 Base64 是如何完成的,那么 Base128 并不难。荷兰语维基百科很好地描述了该过程(为您翻译):

  1. 将数据转换为二进制。
  2. 继续获取 7 位块 (2^7 = 128)
  3. 将这 7 位转换为整数。
  4. 在您定义的转换表中查找该整数,并将找到的字符附加到 base128 字符串中。

该转换表包含 128 个兼容的 UTF8 字符,例如:

0: a
1: b
..
25: z
26: 0

唯一的要求是发送方和接收方的转换表相同。

Well, base128 is not that hard, if you know how base64 is done. The dutch wikipedia describes the process well (translated it for you):

  1. Convert the data to binary.
  2. Keep taking chunks of 7 bits (2^7 = 128)
  3. Convert those 7 bits to an integer.
  4. Look up that integer in a translation table that you defined and append the character found to the base128 string.

That translation table contains 128 compatible UTF8 characters, for example:

0: a
1: b
..
25: z
26: 0

The only requirement is that translation table is the same at both sender and reciever.

老街孤人 2024-11-10 07:34:23

尝试使用 objectstateformatter 而不是 BinaryFormatter,可能会得到更小的大小。这一切都取决于您序列化的数据。

Try using the objectstateformatter instead of the BinaryFormatter, might give you a smaller size. It all depends on the data your serializing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文