使用protobuf-net序列化数组时如何处理空值? [英] How to deal with null values when serializing arrays with protobuf-net?

查看:46
本文介绍了使用protobuf-net序列化数组时如何处理空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下语句失败,并出现 NullReferenceException :

The following statement fails with NullReferenceException:

TypeModel.Create().DeepClone(new string[1]);

检查源代码会发现该异常是有意抛出的,这表明数组中的 null 值违反了协议缓冲区规范(很有意义, null 不是重复任何值).

Examining the source code reveals that the exception is thrown on purpose, implying that null values in an array violate the protocol buffers spec (makes sense, null is not a repetition of any value).

好的,规范是正确的,但是如果集合中仍然存在null值,我们该怎么办?除了确保没有 null 值进入我们的集合之外,还有解决方案吗?

OK, the spec is right, but what should we do if there is still a null value in a collection? Is there a solution, besides making sure no null values ever creep into our collections ?

谢谢.

推荐答案

这是一个棘手的问题;在导线级别,集合只是重复的标签-每个元素代表一个对象.很简单,在protobuf规范中没有直接直接表达 null 的方法.

It is a tricky one; at the wire level, a collection is simply a repeated tag - and each element represents an object. Quite simply, there is no way of directly expressing a null in the protobuf spec.

现在,我可能会变得肮脏,并为带有null的集合添加某种双重标记,但是-老实说,我认为使用看起来为null的非null对象通常会更好(如果您看到我的意思是).对于字符串,也许 " 可以(实际上取决于上下文).

Now, I could get dirty and have some kind of dual tag for a collection-with-nulls, but - to be honest I think it would generally be better to use a null-looking non-null object (if you see what I mean). In the case of string, maybe "" would do (it depends on context, really).

我也欢迎提出建议,但是...它需要牢记规范的局限性.

I'm open to suggestions too, but... it would need to keep the limitations of the spec in mind.

这篇关于使用protobuf-net序列化数组时如何处理空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆