protobuf-net 中的反序列化顺序 [英] Order of deserialization in protobuf-net

查看:48
本文介绍了protobuf-net 中的反序列化顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protobuf-net 中成员的反序列化顺序是什么?是按照标签号的顺序,还是按照声明的顺序,还是其他的,还是不能保证的?

What's the order of deserialization of members in protobuf-net? Is it in order of tag numbers, in order of declaration, something else, or not guaranteed?

我依靠它来解决 protobuf-net 不支持大多数类型的锯齿状数组的问题.我有一个自定义的 LinearAdapter 类,它将锯齿状数组呈现为 IEnumerable(使用 Add 方法).我的主要数据表示在两侧用零填充,填充大小可变,因此,在反序列化时,在 LinearAdapter 中,我首先读取已反序列化的填充大小,因此我可以正确地反序列化数组.

I'm relying on this for my workaround for the problem where protobuf-net doesn't support most types of jagged arrays. I have a custom LinearAdapter class that presents the jagged array as an IEnumerable<T> (with an Add method). My main data representation is padded with zeros at the sides, with a variable padding size, so, on deserializing, in LinearAdapter I first read the already-deserialized padding size, so I can deserialize the array correctly.

推荐答案

您的标题询问了反序列化 - 答案通常是按照它们在流中遇到的顺序" - 即将数据直接分配给成员(或添加到列表),因为它看到它.具体来说,列表处理如下

Your title asks about deserialization - for which the answer is usually "in the order they are encountered in the stream" - i.e. it assigns data directly to members (or adds to lists) as it sees it. Specifically, lists are processed as follows

  • (读取映射到列表字段的标题)
    • 从成员那里获取现有列表,或创建新列表
    • [*] 从流中读取值
    • 添加到列表
    • 查看下一个标题;如果是相同的字段,则使用标题并从 [*] 恢复
    • 如有必要,将列表分配给成员
    • (read header that maps to a list field)
      • get existing list from member, or create new
      • [*] read value from stream
      • add to list
      • peek at next header; if the same field, consume header and resume from [*]
      • assign list to member if necessary

      单个基本值,另一方面,被处理为:

      single basic values, on the other hand, are processed as:

      • (读取映射到值字段的标头)
        • 从流中读取值
        • 为成员赋值

        (注意没有循环)

        更多复杂值(对象)将被处理为:

        more complex values (objects) would be processed as:

        • (读取映射到对象字段的标头)
          • 从成员读取现有对象
          • 从子流合并子对象,或者如果微不足道则创建新对象
          • 必要时将对象分配给成员

          对此有一个微妙的例外,在类元组"数据(带有隐式契约)的情况下,出于实现原因,这些值都保存在本地,直到对象的流被消耗,然后构造函数是被调用(构造函数的参数顺序是这里的标签顺序):

          There is a subtle exception to this, in the case of "tuple-like" data (with implicit contracts), where for implementation reasons the values are all held in locals until the object's stream is consumed, and then the constructor is invoked (the order of the parameters to the constructor is implicitly the tag ordering here):

          • 为每个字段准备一个局部变量(默认值等)
          • 使用子流,使用上述逻辑,但分配给局部变量,而不是成员
          • 调用构造函数

          如果你的意思是它是如何序列化的",那么它是按升序的标签顺序序列化的(按照线路规范中的指导)——但是,在继承的情况下,这里有一个轻微的例外(它在通过子类的子对象连接).

          If you mean "how is it serialized", then it is serialized in ascending tag-order (as per guidance in the wire spec) - however, it makes a slight exception here in the case of inheritance (which it implements on the wire via a sub-object for the sub-class).

          这篇关于protobuf-net 中的反序列化顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆