Search code examples
c#protobuf-net

Protobuf-net Append Object Instances into Stream, Deserialize as List


I store multiple instances of a class in a List and when I close my program I save the List into a file using Serialize method.

However, the size of the List can be huge so I wonder if there is a method to Serialize and append object instances into a file during runtime and then Deserialize the file as List when needed.

If there is a way, can you please share an example?


Solution

  • Instead of serializing the entire list, you can add individual instances of the object to the file using the WriteDelimitedTo method.

    var persons = new List<Person>();
    

    When adding an element, save it to a file. Note FileMode.Append.

    Person person = ...
    persons.Add(person);
    
    using (var fileStream = new FileStream("persons.dat", FileMode.Append))
    {
        person.WriteDelimitedTo(fileStream);
    }
    

    Don't forget to delete or truncate the file before saving first item, if necessary.


    During deserialization, we read objects one by one using the ParseDelimitedFrom method and add them to the list manually.

    using (var fileStream = new FileStream("persons.dat", FileMode.Open))
    {
        while (fileStream.Position != fileStream.Length)
        {
            var person = Person.Parser.ParseDelimitedFrom(fileStream);
            persons.Add(person);
        }
    }
    

    Don't forget to clear the list before adding items, if necessary.