How to handle null values ​​when serializing arrays with protobuf-net?

The following statement does not work with a NullReferenceException :

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

Examining the source code shows that an exception is thrown specifically, assuming that the null values ​​in the array violate the specification of the protocol buffers (it makes sense, null not a repetition of any value).

OK, the specification is correct, but what should we do if the collection still has a null value? Is there a solution besides the fact that null values ​​never creep in our collections?

Thanks.

+6
source share
2 answers

This is a difficult question; at the posting level, a collection is just a repeating tag - and each element represents an object. Simply put, there is no way to directly express null in the protobuf specification.

Now I could be dirty and have some kind of double tag for a collection with zeros, but - to be honest, I think it would be better to use a null view of a non-zero object (if you see what I mean). In the case of a string, perhaps "" will do (it really depends on the context).

I am also open to suggestions, but ... he will have to take into account the limitations of the specification.

+3
source

See this:

http://code.google.com/p/protobuf-net/issues/detail?id=217

Link to the relevant part :

Comment by 4 contributors to marc.gravell, Dec 14, 2011

I have not documented this parameter yet (it was added as a specific request), but at the moment you can enable this only through (for example):

RuntimeTypeModel.Default[typeof (YourType)][3].SupportNull = true;

where 3 is the field number.

This should probably be available for the attribute model ...

A note for the user “will be” - my last previous answer - I know this is not the best place, but I do not know another way to communicate. You’re just calm in this case, it’s wrong: I’m sorry for the “too short” answer for the first time, BUT would not editing be more constructive than a sudden deletion? The link I provided still DID answered the problem, as the comment also indicates ... I only accidentally discovered that you deleted it.

+5
source

Source: https://habr.com/ru/post/896776/


All Articles