Using boost serialization for streaming is not abusing it and not odd either.
In fact, Boost Serialization has nothing but the streaming archive interface. So yes, the applicable approach would be to do as you said:
archive << number_of_items;
for(auto it = input_iterator(); it != end(); ++it)
archive << *it;
In fact, very little stops you from doing the same in your serialize
method. You could possibly even make it "automatic" by wrapping your stream into something (like an iterator_range
?) and extending Boost Serialization to 'understand' these, like it 'understands' containers, arrays etc.
The file format approach is definitely not cleaner (from the library perspective) since it ruins the archive format isolation. The serialization library has been carefully designed to avoid knowledge about the archive representation, and it would be a breach of abstraction to circumvent this. Also see