使用 Protobuf.net 序列化内存流元组(锯齿状数组)
是否可以使用 Protobuf.net 序列化由二进制编写器创建的内存流元组?如果我尝试这样做,Protobuf 会说它无法序列化流。
我所追求的是序列化锯齿状数组的元组。我不想将它们转换为一维数组,因为我认为这是没有必要的。所以我想我可以自己将锯齿状数组序列化到内存流,但我希望 Protobuf.net 为我序列化元组。我需要尽快进行整体序列化/反序列化。
编辑:
我的总体目标是无论如何尽快通过 WCF 发送以下锯齿状数组元组。 从锯齿状数组到锯齿状数组的总时间
计数。
// F# interactive code
#time "on"
#r @"d:\Protobuf\protobuf-net.dll"
let getJaggedArray () =
let size = 100
let rnd = System.Random 1
Array.init size (fun i ->
Array.init size (fun j ->
Array.init size (fun k ->
rnd.NextDouble() )))
let wcfData = (getJaggedArray (), getJaggedArray ()), getJaggedArray ()
let file = new System.IO.FileStream ("test.bin", System.IO.FileMode.Create)
ProtoBuf.Serializer.Serialize (file, wcfData)
file.Close()
// === Future use ===
//[<ServiceContract>]
//type IWCF =
//
// [<OperationContract>]
// [<ProtoBehavior>]
// abstract Send: wcfData -> unit
Is it possible to serialize a tuple of memory streams created by a binarywriter using Protobuf.net? If I try that, the Protobuf says that it cannot serialize a stream.
What I am after is to serialize a tuple of jagged arrays. I do not want to convert them into 1D arrays because I believe it is not necessary. So I thought that I would serialize the jagged arrays to the memory stream on my own, but I want the Protobuf.net to serialize the tuple for me. I need to have the overall serialization / deserialization as fast as possible.
EDIT:
My overall goal is to send the following jagged array tuple through WCF as fast as possible no matter how. The overall time from jagged array to jagged array
counts.
// F# interactive code
#time "on"
#r @"d:\Protobuf\protobuf-net.dll"
let getJaggedArray () =
let size = 100
let rnd = System.Random 1
Array.init size (fun i ->
Array.init size (fun j ->
Array.init size (fun k ->
rnd.NextDouble() )))
let wcfData = (getJaggedArray (), getJaggedArray ()), getJaggedArray ()
let file = new System.IO.FileStream ("test.bin", System.IO.FileMode.Create)
ProtoBuf.Serializer.Serialize (file, wcfData)
file.Close()
// === Future use ===
//[<ServiceContract>]
//type IWCF =
//
// [<OperationContract>]
// [<ProtoBehavior>]
// abstract Send: wcfData -> unit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Protobuf-net 当前没有任何特定的流处理(尽管它会处理
byte[]
),尽管我猜它可以简单地消耗Stream (序列化)并重现
MemoryStream
(反序列化)。如果您不想更改模型,应该可以为Stream
注册一个“代理”,只需几行代码即可完成此操作(如果您感兴趣,请告诉我) )。它自动支持最常见的“元组”模式,但锯齿状数据又是一个问题 - 尽管同样,如果我们确实需要的话,可以处理这个问题(更常见的是,有一个对象的外部列表它们本身有一个内部列表是“锯齿状”的实用解决方法,并且目前有效)。
如果您可以发布更完整(最好是可运行)的示例,我也许可以提供更多指导。
Protobuf-net does not current have any specific handling of streams (although it will handle
byte[]
), although I guess it could simply consume theStream
(serialize) and reproduce aMemoryStream
(deserialize). If you didn't want to change your model, it should be possible to register a "surrogate" forStream
that does exactly that in only a few lines of code (let me know if that is of interest).It has automatic support for most common "tuple" patterns, but jagged data is again an issue - although again, this could be handled if we really needed (more commonly, having an outer- list of objects that themselves have an inner-list is a pragmatic workaround to "jagged", and works currently).
If you can post a more complete (ideally runnable) example, I may be able to offer more guidance.