如何解码原始原型字节数组 [英] How to decode raw proto byte array
问题描述
使用 HttpClient
我可以获得字节数组数据,它是协议缓冲区.如何在不知道类型的情况下使用 protobuf-net 对其进行解码?这个 stackoverflow 答案不再起作用在我无法实例化 ProtoReader
的最新版本中.
Using HttpClient
I can get the byte array data where it's a protocol buffer. How to decode it using protobuf-net without knowing the type? This stackoverflow answer doesn't work anymore in the latest version where I can't instantiate the ProtoReader
.
推荐答案
您仍然可以在 v3 中实例化阅读器 API.唯一的区别是您应该使用Create
而不是new
,如果可能的话:改用新的ProtoReader.State
API.除此之外,它应该基本保持不变.此外,您不再需要 MemoryStream
(您可以直接使用诸如 byte[]
之类的原始缓冲区).
You can still instantiate the reader API in v3. The only difference is that you should useCreate
rather than new
, and if at all possible: use the new ProtoReader.State
API instead. Other than that, it should stay largely the same. Plus you don't need a MemoryStream
any more (you can use raw buffers such as a byte[]
directly).
这是使用 v3 和 State
API 的翻译版本:
Here's a translated version using v3 and the State
API:
static void Main()
{
byte[] arr = // ...
var reader = ProtoReader.State.Create(arr, null, null);
try
{
WriteTree(ref reader);
}
finally
{ // the rules on when you can "using" a ref-type
// are ... complicated
reader.Dispose();
}
}
static void WriteTree(ref ProtoReader.State reader)
{
while (reader.ReadFieldHeader() > 0)
{
Console.WriteLine(reader.FieldNumber);
Console.WriteLine(reader.WireType);
switch (reader.WireType)
{
case WireType.Varint:
// warning: this appear to be wrong if the
// value was written signed ("zigzag") - to
// read zigzag, add: pr.Hint(WireType.SignedVariant);
Console.WriteLine(reader.ReadInt64());
break;
case WireType.String:
// note: "string" here just means "some bytes"; could
// be UTF-8, could be a BLOB, could be a "packed array",
// or could be sub-object(s); showing UTF-8 for simplicity
Console.WriteLine(reader.ReadString());
break;
case WireType.Fixed32:
// could be an integer, but probably floating point
Console.WriteLine(reader.ReadSingle());
break;
case WireType.Fixed64:
// could be an integer, but probably floating point
Console.WriteLine(reader.ReadDouble());
break;
case WireType.StartGroup:
// one of 2 sub-object formats
var tok = reader.StartSubItem();
WriteTree(ref reader);
reader.EndSubItem(tok);
break;
default:
reader.SkipField();
break;
}
}
}
(非State
API 仍然可用,但这是首选)
(the non-State
API is still usable, but this is preferred)
这篇关于如何解码原始原型字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!