[英]Record and replay human-readable protobuf messages using a file with protobuf-csharp-port
I'm using protobuf-csharp-port and I need the ability to record some protobuf messages for replay later. 我正在使用protobuf-csharp-port,并且需要能够记录一些protobuf消息以供以后重播。 XML would be ideal for me, but I'm flexible as long as a human could go into the file, make changes, then replay the messages from the file.
XML对我来说是理想的选择,但是我很灵活,只要有人可以进入文件,进行更改,然后重播文件中的消息即可。
Using this C# code: 使用此C#代码:
MyRequest req =
MyRequest.CreateBuilder().SetStr("Lafayette").Build();
using (var stringWriter = new StringWriter())
{
var xmlWriterSettings = new XmlWriterSettings
{
ConformanceLevel = ConformanceLevel.Fragment
};
var xmlWriter = XmlWriter.Create(stringWriter, xmlWriterSettings);
ICodedOutputStream output = XmlFormatWriter.CreateInstance(xmlWriter);
req.WriteTo(output);
output.Flush();
string xml = stringWriter.ToString();
using (var streamWriter = new StreamWriter(@"Requests.txt"))
{
streamWriter.WriteLine(xml);
streamWriter.WriteLine(xml);
streamWriter.WriteLine(xml);
}
}
I produce the Requests.txt file containing: 我生成包含以下内容的Requests.txt文件:
<str>Lafayette</str>
<str>Lafayette</str>
<str>Lafayette</str>
However when I try to deserialize them back using: 但是,当我尝试使用以下方法反序列化它们时:
var xmlReaderSettings = new XmlReaderSettings
{
ConformanceLevel = ConformanceLevel.Fragment
};
using (var xmlReader = XmlReader.Create(@"Requests.txt", xmlReaderSettings))
{
ICodedInputStream input = XmlFormatReader.CreateInstance(xmlReader);
MyRequest reqFromFile;
while(!input.IsAtEnd)
{
reqFromFile =
ReverseRequest.CreateBuilder().MergeFrom(input).Build();
}
}
Only one MyRequest
gets deserialized and the other two are ignored. 只有一个
MyRequest
被反序列化,而另两个则被忽略。 (After reqFromFile
is built, input.IsAtEnd == true
.) (后
reqFromFile
建成, input.IsAtEnd == true
)。
So back to my question: is there some way to read multiple human-readable protobuf messages from a file? 回到我的问题:有什么方法可以从文件中读取多个人类可读的protobuf消息?
So back to my question: is there some way to read multiple human-readable protobuf messages from a file?
回到我的问题:有什么方法可以从文件中读取多个人类可读的protobuf消息?
Well you're currently creating a single text file with multiple root elements - it's not a valid XML file. 好吧,您当前正在创建带有多个根元素的单个文本文件-它不是有效的XML文件。
The simplest approach would probably be to create a single XML document with all those requests in, then load it (eg with LINQ to XML) and create an XmlReader
positioned at each of the root element's children. 最简单的方法可能是创建一个包含所有这些请求的XML文档,然后将其加载(例如,使用LINQ to XML),并在每个根元素的子元素中创建一个
XmlReader
。 Each of those XmlReader
objects should be able to then deserialize to a single protobuf message. 然后,这些
XmlReader
对象中的每一个都应该能够反序列化为单个protobuf消息。
You could look at the Protocol buffer Editor 您可以看一下协议缓冲区编辑器
Alternatively there is Google own Text format which is a bit like JSon. 另外,还有Google自己的文本格式,有点像JSon。 You can convert between the 2 using the protoc command (look at encode / decode options, you also need the proto definition)
您可以使用protoc命令在2之间进行转换(查看编码/解码选项,还需要原始定义)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.