Null String in C# (CheckForNull)

1,526 views
Skip to first unread message

Everton Araujo

unread,
Oct 3, 2017, 2:35:20 PM10/3/17
to Protocol Buffers
Hi all,

I'm facing the situation where a string value is not allowed to be null in Protobuf 3 using C# (CheckNotNull).
I do not understand it considering that fields are optional by default in proto3.
Does anyone knows the original reason for this constraint? As far as I could see, it happens for string and bytearrays.

Regards,

Everton


Marc Gravell

unread,
Oct 3, 2017, 5:06:26 PM10/3/17
to Everton Araujo, Protocol Buffers
Fields are optional but the implicit default for a string is a zero length string, not a null length string. To be honest, either approach seems perfectly reasonable as long as it is documented and any exception is clear and obvious. For my separate implementation I chose to interpret nulls as "meh, not set, ignore it", but... either is fine IMO.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+unsubscribe@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc

Jon Skeet

unread,
Oct 4, 2017, 9:06:00 AM10/4/17
to Protocol Buffers
Exactly. In an early implementation I treated null and empty string as the same - but it ends up being weird in all kinds of ways. (Either setting a property and then fetching it isn't a roundtrip, or serializing and deserializing isn't a roundtrip.)

In the end, I went with "stick to protobuf semantics: every string has a value, which is empty by default."

If you use the StringValue wrapper type, you then can use null, which means "there's no StringValue here".


On Tuesday, 3 October 2017 22:06:26 UTC+1, Marc Gravell wrote:
Fields are optional but the implicit default for a string is a zero length string, not a null length string. To be honest, either approach seems perfectly reasonable as long as it is documented and any exception is clear and obvious. For my separate implementation I chose to interpret nulls as "meh, not set, ignore it", but... either is fine IMO.
On 3 October 2017 at 19:35, Everton Araujo <everto...@gmail.com> wrote:
Hi all,

I'm facing the situation where a string value is not allowed to be null in Protobuf 3 using C# (CheckNotNull).
I do not understand it considering that fields are optional by default in proto3.
Does anyone knows the original reason for this constraint? As far as I could see, it happens for string and bytearrays.

Regards,

Everton


--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.



--
Regards,

Marc
Reply all
Reply to author
Forward
0 new messages