Issues with serializing a zero doubling list using Protobuf-net

I managed to serialize double values ​​with extension without a problem and can serialize null lists of other types, but cannot serialize lists with zero doubles.

If I do this:

List<double?> aList = new List<double?>(); aList.Add(0.1); aList.Add(null); Serializer.Serialize(ms, aList); 

I get this error:

System.NullReferenceException: An object reference is not set to an object instance. in ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType (protoWriter writer, type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList) in c: \ Dev \ protobuf-net \ protobuf-net \ Meta \ TypeModel.cs: line 169 ProtoBuf.Meta.TypeModel.SerializeCore (ProtoWriter writer, object value) in c: \ Dev \ protobuf-net \ protobuf-net \ Meta \ TypeModel.cs: line 188 in ProtoBuf.Meta.TypeModel.Serialize (Stream dest, Object value , SerializationContext context) in c: \ Dev \ protobuf-net \ protobuf-net \ Meta \ TypeModel.cs: line 217 in ProtoBuf.Meta.TypeModel.Serialize (Stream dest, Object value) in c: \ Dev \ protobuf-net \ protobuf-net \ Meta \ TypeModel.cs: line 201 on ProtoBuf.Serializer.Serialize [T] (stream destination, instance T) in c: \ Dev \ protobuf-net \ protobuf-net \ Serializer.cs: line 87

if it works? Am I doing something wrong?

+4
source share
1 answer

The main problem here is that the protobuf specification simply does not have the concept of null - explicitly null / missing values ​​cannot be expressed in protobuf format.

In each library, the library itself can choose to fake an extra layer to allow this thing, but:

  • more bytes will be required for posting
  • this will complicate the code and require additional configuration
  • he (if necessary) would disable optimizations such as "packed" coding

It should probably detect zero and behave better, though!

I would advise you to serialize a list of things with a null value, not a list of null values. For instance:

 [ProtoContract] public class Foo { [ProtoMember(1)] public double? Value {get;set;} } 

The above list may contain null values. And basically it’s the same as me if I added the built-in support for zero substitution.

+2
source

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


All Articles