views:

485

answers:

1

I've been looking to do some binary serialization to file and protobuf-net seems like a well-performing alternative. I'm a bit stuck in getting started though. Since I want to decouple the definition of the classes from the actual serialization I'm not using attributes but opting to go with .proto files, I've got the structure for the object down (I think)

message Post {

  required uint64 id = 1;

  required int32 userid = 2;

  required string status= 3;

  required datetime created = 4;

  optional string source= 5;

}

(is datetime valid or should I use ticks as int64?)

but I'm stuck on how to use protogen and then serialize a IEnumerable of Post to a file and read it back. Any help would be appreciated

Another related question, is there any best practices for detecting corrupted binary files, like if the computer is shut down while serializing

+1  A: 

Re DateTime... this isn't a standard proto type; I have added a BCL.DateTime (or similar) to my own library, which is intended to match the internal serialization that protobuf-net uses for DateTime, but I'm fairly certain I haven't (yet) updated the code-generator to detect this as a special-case. It would be fairly easy to add if you want me to try... If you want maximum portability, a "ticks" style approach might be pragmatic. Let me know...

Re serializing to a file - if should be about the same as the Getting Started example, but note that protobuf-net wants to work with data it can reconstruct; just IEnumerable<T> might cause problems - IList<T> should be fine, though (it'll default to List<T> as a concrete type when reconstructing).

Re corruption - perhaps use SerializeWithLengthPrefix - it can then detect issues even at a message boundary (where they are otherwise undetectable as an EOF). This (as the name suggests) writes the length first, so it knows whether is has enough data (via DeserializeWithLengthPrefix). Alternatively, reserve the first [n] bytes in your file for a hash / checksum. Write this blank spacer, then the data, calculate the hash / checksum and overwrite the start. Verify during deserialization. Much more work.

Marc Gravell
Hi mark, thanks for the answer. What I was wondering was about the .proto file declaration (rather than attributes) for a list of Post and then how to use protogen and the generated class... havent seen any example of thatHmm, perhaps I could put a dummy Post with a certain value last in the file always, and if it isnt there fall back to the last backup
MattiasK