我有一个List<object>不同类型的对象,如整数,字符串和自定义类型.所有自定义类型都是protobuf调整.我现在要做的是使用protobuf.net序列化/反序列化此列表.到目前为止,我怀疑我必须明确地声明每个类型,遗憾的是这些混合列表结构是不可能的.因为二元形成器没有问题做这些事情我希望我错过了一些东西,你可以帮助我.所以我的问题是如何处理protobuf.net中的对象.
使用Google Protocol Buffers,我可以为我编码的所有邮件设置最大大小吗?
如果我知道我编码的内容永远不会大于X字节,那么Google Protobuffs总会产生一个大小为Y的缓冲区,如果我给它一个较小的数据量,请将它填充到Y大小?
对于我的生活,我无法从Open Street Maps反序列化protobuf文件.
我试图反序列化以下提取:http://download.geofabrik.de/osm/north-america/us-northeast.osm.pbf以获取节点,我正在使用http://code.google.com/p/protobuf-net /作为图书馆.我试图反序列化一堆不同的对象,但它们都是null.
原型文件可以在这里找到:http://trac.openstreetmap.org/browser/applications/utils/export/osm2pgsql/protobuf
有什么建议?
我正在使用ProtoBuf-net的第2版,目前我正在发出错误"无法确定成员:A"
当我们使用ClassOfType <T>时,是否可以为Protobuf-net创建运行时模型?如果是这样,有人可以在下面的代码中发现我缺少的东西吗?
顺便说一句:这个请求的模型是使用protobuf-net反序列化未知类型 我可以得到一个版本的这个很好......但是它们使用的是抽象基类,而不是T的泛型类.
这是一个工作示例(删除了无效的东西).
using System;
using System.IO;
using NUnit.Framework;
using ProtoBuf;
using ProtoBuf.Meta;
namespace ProtoBufTestA2
{
[TestFixture]
public class Tester
{
[Test]
public void TestMsgBaseCreateModel()
{
var BM_SD = new Container<SomeDerived>();
using (var o = BM_SD) {
o.prop1 = 42;
o.payload = new SomeDerived();
using (var d = o.payload) {
d.SomeBaseProp = -42;
d.SomeDerivedProp = 62;
}
}
var BM_SB = new Container<SomeBase>();
using (var o = BM_SB) {
o.prop1 = 42;
o.payload = new SomeBase(); …Run Code Online (Sandbox Code Playgroud) 在Unity 3D中,有一个带有Transform属性的GameObject.变换具有位置(Vector3)和旋转(四元数).我想通过protobuf-net将这些GameObjects发送到后备存储中.我目前正在使用以下代码,但GameObject.transform.position.x,y,z和transform.rotation.x,y,z,w似乎没有存储在序列化文件中?
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation");
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
model.SerializeWithLengthPrefix(fs, go, typeof(GameObject), PrefixStyle.Base128, 0);
Run Code Online (Sandbox Code Playgroud)
反序列化:
using (FileStream fs = new FileStream(path, FileMode.Open))
{
fs.Position = 0;
RuntimeTypeModel model = TypeModel.Create();
model.AutoAddMissingTypes = true;
model.Add(typeof(Vector3), true).Add("x","y","z");
model.Add(typeof(Transform), true).Add("position").Add("rotation"); ;
model.Add(typeof(Quaternion), true).Add("x","y","z","w");
model.Add(typeof(GameObject), true).Add("transform").Add("name");
do
{
len = ProtoReader.ReadLengthPrefix(fs, false, PrefixStyle.Base128, out fieldNumber, out bytesRead);
if (bytesRead <= 0) continue;
gos.Add((GameObject)model.Deserialize(fs, null, typeof(GameObject), len));
} while (bytesRead > 0);
}
Run Code Online (Sandbox Code Playgroud)
我似乎回到了正确数量的GameObjects,但反序列化唯一正确的是.name属性.我没有使用当前代码转换Transform类的子属性.任何想法都会非常有用!
谢谢-
根据我的评论,这里是堆栈跟踪: …
如何让protobuf-net的v2忽略我的类实现ICollection,IEnumerable等的事实?
对于这种特殊情况,我只希望标记为[ProtoMember]的字段被序列化.
我目前正在使用protobuf-net v1转换为使用v2.我有一个特殊的结构,由于更改,现在正在序列化错误.它看起来像下面这样:
[ProtoContract]
public class FileTree : ICollection<FilePath>, IEnumerable<FilePath>, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged {
private FileTreeNode _Root;
[ProtoMember (1)]
public FileTreeNode Root {
get { return _Root; }
set { _Root = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
编写FileTree类是为了将文件路径(如"C:\ happy.txt""C:\ history.txt")折叠成更像的东西
"C:\h"
???? "appy.txt"
???? "istory.txt"
Run Code Online (Sandbox Code Playgroud)
该结构消除了路径串中的冗余.所以,我真的不希望FileTree类通过IEnumerable函数被序列化,因为它只是存储为"C:\ happy.txt","C:\ history.txt"等.现在,在序列化中对于FileTree对象,每个路径都将完全打印出来.
编辑:我要提到的最后一件事 - 我在FileTree中有一个On_Deserialization函数,它用[ProtoAfterDeserialization]标记.我在函数中放了一个断点,但它没有被击中.这与此类序列化的方式有关吗?
Heyup.protobuf.net的长期爱好者.
但是问题很快.我有一个高度多线程的C#应用程序,可以反序列化大约100个对象/秒,大约50MB /秒.我看到非常大的内存使用量,远远超过我正在反序列化的内存.我通过'Red Gate ANTS Memory Profiler'运行应用程序,由于protobuf(超过50%的应用程序使用),它向我展示了大量的第2代内存对象.大多数对象都是int值,并与以下内容链接:
- TypeModel.TryDeserializeList()
- ProtoBuf.Meta.BasicList
Run Code Online (Sandbox Code Playgroud)
任何帮助减少这个第2代内存使用将是值得赞赏的.
渣
我在Protobuf-net上看到了SetFactory方法.但是,它似乎可以按类型提供.(和MethodInfo或字符串?我能听到Func<T>吗?)
我想要的是能够使用我的IoC容器的对象解析和构造能力进行所有反序列化.(我正在使用Autofac.)Protobuf-net应该首先尝试在任何构造上使用我的IoC.如果返回null,那么我想要默认行为.目前有办法做到这一点吗?谢谢你的时间.
有没有办法让protobuf序列化/反序列化F#的歧视联盟?
我正在尝试使用protobuf序列化消息.消息是F#记录和受歧视的联合.
序列化似乎适用于记录,但我不能让它与歧视的联合工作.
在下面的代码中,测试testMessageA和testMessageB是绿色的.测试testMessageDU为红色.
module ProtoBufSerialization
open FsUnit
open NUnit.Framework
open ProtoBuf
type MessageA = {
X: string;
Y: int;
}
type MessageB = {
A: string;
B: string;
}
type Message =
| MessageA of MessageA
| MessageB of MessageB
let serialize msg =
use ms = new System.IO.MemoryStream()
Serializer.SerializeWithLengthPrefix(ms, msg, PrefixStyle.Fixed32)
ms.ToArray()
let deserialize<'TMessage> bytes =
use ms = new System.IO.MemoryStream(buffer=bytes)
Serializer.DeserializeWithLengthPrefix<'TMessage>(ms, PrefixStyle.Fixed32)
[<Test>]
let testMessageA() =
let msg = {X="foo"; Y=32}
msg |> serialize |> deserialize<MessageA> |> …Run Code Online (Sandbox Code Playgroud) 我试图在网上找到一些建议,但找不到任何相关的建议.
假设我正在创建一个包含很多字段(50+)的协议缓冲区消息.最好是将所有字段保持在同一级别还是将它们组织在子消息中?这种或那种方式对表演有什么影响吗?
例:
message myMessage{
string field1 = 1;
string field2 = 2;
....
string fieldn = n;
}
Run Code Online (Sandbox Code Playgroud)
VS
message myMessage{
SubMessage1 groupedfieldsbasedonsomebusinesslogic1 = 1;
SubMessage2 groupedfieldsbasedonsomebusinesslogic2 = 2;
message SubMessage1{
string field1 = 1;
string field2 = 2;
...
string fieldx = x;
}
message SubMessage2{
string fieldxplus1 = x+1;
...
string fieldn = n;
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里并没有考虑可读性,因为在反序列化以获得平坦数据或嵌套数据时存在利弊.我的问题是真正关注技术影响.