使用 protobuf-net 时空字符串反序列化为空字符串
我正在使用 protobuf-net 来序列化和反序列化我的消息。我的消息还包含可以为空的字符串。但是,当我在另一侧反序列化它们时,我得到空字符串(“”)。
根据谷歌文档,空字符串中字符串类型的默认值。 这个问题的解决方案是什么?
这是我正在使用的代码:
Command message = new Command();
message.s_value = null;
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<Command>(stream, message);
stream.Close();
}
反序列化相同的流时,我得到 s_value = ""
I am using protobuf-net to serialize and deserialize my messages. My message also contain come strings that can be null. However when I deserialize them on the other side, I get empty strings ("").
According to google docs, the default value for string type in empty string.
What could be the resolution for this issue?
Here is the code I am using:
Command message = new Command();
message.s_value = null;
using (MemoryStream stream = new MemoryStream())
{
Serializer.Serialize<Command>(stream, message);
stream.Close();
}
Upon deserializing the same stream, I get s_value = ""
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜测您的类型在无参数构造函数中将字符串显式设置为“”。你能检查一下吗?
protobuf-net 处理此问题的方式是:
null
,不发送任何内容(protobuf 有线格式无法显式表达null code>,但我们可以将其视为可选并省略它)
""
""
,发送 0 长度模式,对于 a 非空字符串、长度和字符串被发送,并被反序列化。在反序列化期间,在
null
情况下,它只会保留您的字段/属性,因为它没有要处理的数据。如果类型将默认值设置为""
,它将保持为""
。请注意,在“v2”中(我希望在接下来的两周内发布它),您可以选择告诉它使用“不运行任何构造函数”的 WCF 方法,这将具有将其保留为
null
即使默认构造函数分配了它。您还可以使用一些技巧(使用“v1”)来发送
bool
标志(作为单独的属性)来表示null
;如果您想要这方面的示例,请告诉我。编辑:这里有一个“v1”技巧的例子来解决这个问题;不过,从长远来看,“忽略构造函数”的“v2”方法可能是更好的选择:
I would guess that your type is explicitly setting the string to "" in the parameterless constructor. Could you check?
The way that protobuf-net handles this is:
null
, nothing is sent (the protobuf wire-format has no way of explicitly expressing anull
, but we can treat it as optional and omit it)""
, a 0-length pattern is sent, which should be deserialized as""
During deserialization, in the
null
case it simply leaves your field/property alone, since it has no data to process. If the type sets the default to""
it will stay as""
.Note that in "v2" (I expect to release this in the next two weeks), you can optionally tell it to use the WCF approach of "don't run any constructor", which will have the effect of leaving it as
null
even if the default constructor assigns it.There are also some tricks you can do (with "v1") to send a
bool
flag (as a separate property) to meannull
; let me know if you want an example of this.Edit: here's an example of a "v1" trick to get around this; the "v2" approach of "ignore the constructor" is probably a better option long-term, though: