move serialization methods into vg function

Abstraction lost, but performance gained. This could also be solved by
providing the method in stream.h with an iterator. Crucially, this allows us to
an extra copy of the graph.