我想确定要反序列化的文件是否是 protobuf 序列化的。这是因为我想提供多个选项来为用户序列化和反序列化文件。我正在使用 protobuf.net 序列化为 protobuf 格式。
我需要存储大数据(以千兆字节为单位)以使用 protobuf-net 2.4.0 进行流式传输。
目前,我使用的策略是使用 PrefixStyle.Base128 编写带有 LengthPrefix 的小标头,并使用以下代码使用标准 protobuf 序列化方法编写大主体,它的工作原理非常神奇。
private void Serialize(Stream stream)
{
Model.SerializeWithLengthPrefix(stream, FileHeader, typeof(FileHeader), PrefixStyle.Base128, 1);
if (FileHeader.SerializationMode == serializationType.Compressed)
{
using (var gzip = new GZipStream(stream, CompressionMode.Compress, true))
using (var bs = new BufferedStream(gzip, GZIP_BUFFER_SIZE))
{
Model.Serialize(bs, FileBody);
}
}
else
Model.Serialize(stream, FileBody);
}
Run Code Online (Sandbox Code Playgroud)
现在我需要将主体拆分为 2 个不同的对象,因此我也必须对它们使用 LengthPrefix 方法,但我不知道在这种情况下最好的PrefixStyle是什么。我可以继续使用Base128吗?Fix32描述中的“对兼容性有用”是什么意思?
更新
我发现这篇文章Marc Gravell 解释说可以选择使用开始标记和结束标记,但我不确定它是否可以与 LengthPrefix方法一起使用。更清楚地说,下面代码中显示的方法是否有效?
[ProtoContract]
public class FileHeader
{
[ProtoMember(1)]
public int Version { …Run Code Online (Sandbox Code Playgroud) 我刚刚在分布式缓存应用程序中用协议缓冲区替换了.NET序列化,结果非常令人印象深刻.我唯一不喜欢的是我需要在基本消息类和它的子节点之间添加依赖关系,从而创建循环依赖关系.有没有用ProtoInclude标记基类的替代方法?
我创建了一个.proto文件,并且ProtoBufTool成功创建了.cs文件.我是csharp的新手,我正在尝试设置扩展字段.但不知道怎么做?有没有人有任何使用protobuf-net使用扩展的例子.
我的.proto文件:
package messages;
message DMsg
{
optional int32 msgtype = 1;
extensions 100 to max;
}
extend DMsg
{
optional string fltColumns = 101;
}
Run Code Online (Sandbox Code Playgroud)
这是创建的类:
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
// Generated from: message.proto
namespace messages
{
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"DMsg")]
public partial class DMsg : global::ProtoBuf.IExtensible
{
public DMsg() {}
private int …Run Code Online (Sandbox Code Playgroud) 我测试了protobuf序列化,似乎对于一定数量的对象,它比常规的datacontract序列化慢.使用DataContractSerializer传输大小更大但在序列化和反序列化期间使用DataContractSerializer更快
你认为这是正常的还是我犯了错误?
[DataContract]
public partial class Toto
{
[DataMember]
public string NomToto { get; set; }
[DataMember]
public string PrenomToto { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的datacontract课程,这与protobuf相同
[ProtoContract]
public partial class Titi
{
[ProtoMember(1)]
public string NomTiti { get; set; }
[ProtoMember(2)]
public string PrenomTiti { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我使用protobuf进行WCF服务的方法(对于没有ms的datacontract,相同)
public class TitiService : ITitiService
{
public byte[] GetAllTitis()
{
List<Titi> titiList = new List<Titi>();
for (int i = 0; i < 20000; i++)
{
var titi = new Titi …Run Code Online (Sandbox Code Playgroud) 使用最新的2.0 beta版ProtoBuf.net我试图序列化派生类(只是示例),我得到空文件.为什么基类属性没有序列化?
[ProtoContract]
[Serializable]
public class Web2PdfClient : Web2PdfEntity
{
}
[ProtoContract]
[Serializable]
public class Web2PdfEntity : EngineEntity
{
[ProtoMember(1)]
public string Title { get; set; }
[ProtoMember(2)]
public string CUrl { get; set; }
[ProtoMember(3)]
public string FileName { get; set; }
}
[ProtoContract]
[Serializable]
public class EngineEntity
{
public bool Result { get; set; }
public string ErrorMessage { get; set; }
public bool IsMembershipActive { get; set; }
public int ConversionTimeout { get; set; }
public byte[] …Run Code Online (Sandbox Code Playgroud) 鉴于:
需要:能够检查每个请求/响应的解码HTTP流量.
Fiddler似乎是一个理想的工具,事实上我一直在使用它.但是很容易理解,它无法解码用协议缓冲区编码的请求/响应.另一方面,我知道:
我的问题是,是否有其他人遇到过同样的问题以及是否有办法解决问题.
当然,我喜欢快速而简单的解决方案,而不是长期和艰难的解决方案(比如从头开始编写Fiddler扩展),但在前者的缺席中,我想我也会接受后者.
对那里所有优秀的撒玛利亚人 - 先谢谢.
我是序列化的新手,甚至是protobuf的新手.这是我的问题,我有这些课程:
[ProtoContract]
class Controle
{
[ProtoMember(1, AsReference=true)]
public HashSet<Controle> ControlesInternes { get; set; }
[ProtoMember(2)]
public string TypeControle { get; set; }
[ProtoMember(3)]
public Dictionary<string, string> Attributs { get; set; }
[ProtoMember(4)]
public int Ligne { get; set; }
[ProtoMember(5)]
public string InnerText { get; set; }
[ProtoMember(6)]
public Controle Parent { get; set; }
public Controle()
{
ControlesInternes = new HashSet<Controle>();
Attributs = new Dictionary<string, string>();
}
}
Run Code Online (Sandbox Code Playgroud)
[ProtoContract(SkipConstructor=true)]
class PageAspx
{
[ProtoMember(1)]
public string PrefixeControleOnilait { get; set; …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用protobuf-net来序列化对象.我不确定是否支持我正在尝试继承的内容,但我想我会检查并查看它是否存在或者我是否只是做错了什么.
本质上,我正在尝试序列化一些子类,然后反序列化它,但只使用基类引用.展示:
using UnityEngine;
using System.Collections;
using ProtoBuf;
public class main : MonoBehaviour
{
// If I don't put "SkipConstructor = true" I get
// ProtoException: No parameterless constructor found for Parent
// Ideally, I wouldn't have to put "SkipConstructor = true" but I can if necessary
[ProtoContract(SkipConstructor = true)]
[ProtoInclude(1, typeof(Child))]
abstract class Parent
{
[ProtoMember(2)]
public float FloatValue
{
get;
set;
}
public virtual void Print()
{
UnityEngine.Debug.Log("Parent: " + FloatValue);
}
}
[ProtoContract]
class Child …Run Code Online (Sandbox Code Playgroud) 在他的回答中,对于哪些场景是protobuf-net不合适?马克提到:
没有中间类型的锯齿状数组/嵌套列表也不行 - 您可以通过在中间引入中间类型来对此进行填充
我希望这表明有一种方法可以在不更改我的底层代码的情况下完成,也许使用代理?有没有人找到一个很好的方法来序列化/反序列化嵌套/锯齿状数组