Deserializing protobuf from C++ and reserializing in C# gives different output -
i have file protobuf message in in byte format, when read file , deserialize protobuf works fine (i can read objects fields , correct), however, when reserialize , save file, bytes different original (causing compatibility issues).
more specifically, after string , before bool theres bytes '18 00' added.
i tried playing around dataformat options protobuf-net exact same result original, no avail.
does know of options in protobuf-net in regards saving string or bool explain 2 bytes?
also, ulong being saved differently in bytes aswell.
i don't have control on original file, know it's compiled/serialized c++
my goal recreate exact same file (bytewise) serializing in c#, identical file serialized c++.
this comes down how properties handled. default,
protogen -i:my.proto -o:my.cs
generates simple properties, of form:
private bool _some_value = default(bool); [global::protobuf.protomember(3, isrequired = false, name=@"some_value", dataformat = global::protobuf.dataformat.default)] [global::system.componentmodel.defaultvalue(default(bool))] public bool some_value { { return _some_value; } set { _some_value = value; } }
this fine scenarios, doesn't quite support every "was value specified?" scenario.
however, can instead:
protogen -i:my.proto -o:my.cs -p:detectmissing
which generates more thorough:
private bool? _some_value; [global::protobuf.protomember(3, isrequired = false, name=@"some_value", dataformat = global::protobuf.dataformat.default)] public bool some_value { { return _some_value?? default(bool); } set { _some_value = value; } } [global::system.xml.serialization.xmlignore] [global::system.componentmodel.browsable(false)] public bool some_valuespecified { { return this._some_value != null; } set { if (value == (this._some_value== null)) this._some_value = value ? this.some_value : (bool?)null; } } private bool shouldserializesome_value() { return some_valuespecified; } private void resetsome_value() { some_valuespecified = false; }
this has full support tracking explicit assignment, including support ui-binding , serialization frameworks (not protobuf-net).
the 2-byte difference difference between "false, not serialized due implicit default value" , "false, known explicitly specified false, , serialized".
so fix is: include -p:detectmissing
when using protogen.
Comments
Post a Comment