title: How to serialize complex IEnumerable class in protobuf?
body: <p>I have a <code>RoaringBitmap</code> which represent billions of bits. It is bitmap which is faster/more compact version of <code>BitArray</code> and is unmanaged. It does implement IEnumerable, but more like extension to get positions, and I don't intend to copy their entire repo to fix implementation.
  In compact form it takes about <strong>100 bytes</strong>, in extended through <code>IEnumerable</code> - <strong>1 GB</strong>.</p>
  <p>I tried using converters with surrogates, but get exception:</p>
  <pre><code>public static class ProtobufSerializer
  {
      [ProtoContract]
      public sealed class RoaringBitmapSurrogate
      {
          [ProtoMember(1, OverwriteList = true)]
          public byte[] Data { get; set; }
  
          private const SerializationFormat Format = SerializationFormat.Portable;
  
          [ProtoConverter]
          public static RoaringBitmapSurrogate Convert(RoaringBitmap obj)
          {
              if (obj == null)
                  return null;
              return new RoaringBitmapSurrogate { Data = obj.Serialize(Format) };
          }
          [ProtoConverter]
          public static RoaringBitmap Convert(RoaringBitmapSurrogate obj)
          {
              if(obj == null)
                  return null;
              return RoaringBitmap.Deserialize(obj.Data, Format);
          }
      }
  
      static ProtobufSerializer()
      {
          var model = ProtoBuf.Meta.RuntimeTypeModel.Default;
          //model.Add(typeof(RoaringBitmapSurrogate), true);
          model.Add(typeof(RoaringBitmap), false).SetSurrogate(typeof(RoaringBitmapSurrogate));
      }
  
  
      public static byte[] Serialize&lt;T&gt;(T obj)
      {
          var ms = new MemoryStream();
          Serializer.Serialize(ms, obj);
          return ms.ToArray();
      }
  
      public static T Deserialize&lt;T&gt;(byte[] data)
      {
          return Serializer.Deserialize&lt;T&gt;(new MemoryStream(data));
      }
  }
  </code></pre>
  <blockquote>
  <p>System.InvalidOperationException : For repeated data declared as
  CRoaring.RoaringBitmap, the <em>underlying</em> collection
  (CRoaring.RoaringBitmap) must implement ICollection and must not
  declare itself read-only; alternative (more exotic) collections can be
  used, but must be declared using their well-known form (for example, a
  member could be declared as ImmutableHashSet)</p>
  </blockquote>
  <p>How to serialize it in <strong>protobuf-net</strong>? Obviously serializing <code>IEnumerable</code> is just stupid.
  And Im concerned about logic behind <code>IEnumerable</code> serialization overall, because it potentially can be infinite, be a generator or simply too big (like in my case).</p>