I'm using protobuf-net library to serialize some objects. I have a field
[ProtoMember(3, DataFormat = DataFormat.FixedSize, IsPacked = true)]
ushort[] foo;
When I set it to say ushort[] { 3 }
and then serialize the whole object and read it back it's fine. Then when I set foo to either an e开发者_Python百科mpty array or null and serialize and read it back again, it's not erased. I end up getting the array with 3 in it. To make sure it's not some file system problem I changed other properties and did the roundtrip and they all behaved as expected.
Does anyone know if this is a bug in the implementation or a feature?
It isn't entirely clear without a reproducible example, but IMO the most likely answer here is that you are overwriting an output stream (such as a file) without truncating it. Because the protobuf spec does not include terminators (etc) for the root object, it reads to the end of the file. If you then overwrite with less data, any contents not truncated are garbage.
Now, if the change means the garbage at the end doesn't parse as protobuf, you'll get an exception, like this. However, if the data is still valid (because, for example foo
is the last item in the stream) then it'll just read it and process it. In particular, setting a collection results in that item not being written, hence why setting it to null still leaves a valid stream (if it is the final field to be serialized).
If you aren't already doing so, ensure you trim your outputs (2 simple ways of doing this are shown in the post linked above).
If this is unrelated, then I apologise - but without further information / a reproducible example I have to use a little guesswork ;p
精彩评论