Protocol buffers Apache thrift,结构包含自身
我正在研究数据序列化的节俭方法。但文件说 循环结构-结构只能包含在其前面声明的结构。结构也不能包含自身 我们的要求之一是Protocol buffers Apache thrift,结构包含自身,protocol-buffers,thrift,thrift-protocol,Protocol Buffers,Thrift,Thrift Protocol,我正在研究数据序列化的节俭方法。但文件说 循环结构-结构只能包含在其前面声明的结构。结构也不能包含自身 我们的要求之一是 结构A 子项列表 项目(项目为结构A) 所以阅读要求我不能在任何层次上拥有结构本身?我可以把它放在循环模型中,就像上面一样。Struct不是Struct的直接成员,但它有一些其他成员,并且它包含Struct 他们的文件描述得不太好 节俭有可能吗?protobuf支持它吗?根据,节约是不可能的。但是,有一种解决方法,可以使用整数索引到主列表中。本质上,这是一种穷人的
- 结构A
- 子项列表
- 项目(项目为结构A)
- 子项列表
struct A
{
1: list<i32> subitems;
}
struct AllAs
{
1: list<A> items;
}
是,从Thrift 0.9.2开始支持此方案 对于任何早期版本的thrift,以下(故意)导致编译器错误消息:
struct Foo {
1 : Foo foo // error - Foo not fully defined yet
2 : Bar bar // error - Bar not defined yet
}
struct Bar {
1 : Foo left // ok, Foo has been defined earlier
2 : Foo right // ok, Foo has been defined earlier
}
但仍有一些警告。开发者负责不产生无休止的循环或菱形引用,如
var foo = new Foo();
foo.foo = foo; // will crash on serialization with stack overflow
var bar = new Bar();
bar.left = foo;
bar.right = foo; // points to same object only BEFORE deserialization
var foo = new Foo();
foo.foo = foo; // will crash on serialization with stack overflow
var bar = new Bar();
bar.left = foo;
bar.right = foo; // points to same object only BEFORE deserialization