I’m having a recursion issue in this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using ProtoBuf;
namespace ConsoleApplication4
{
[Serializable]
[ProtoContract(ImplicitFields = ImplicitFields.AllFields)]
public class GeneralWrapper<T> where T : new()
{
public GeneralWrapper()
{
Datas = new T();
}
public T Datas { get; set; }
}
class Program
{
static void Main(string[] args)
{
List<Document> _documents = new List<Document>();
for (int i = 0; i < 100000; i++)
{
Document _document = new Document()
{
DocumentName = "asdadsf"
};
_documents.Add(_document);
}
BinaryFormatter _formatter = new BinaryFormatter();
FileStream fs = new FileStream
("Person1.bin", FileMode.OpenOrCreate);
ProtoBuf.Serializer.Serialize(fs, _documents);
fs.Close();
// Deserialize.
fs = new FileStream
("Person1.bin", FileMode.OpenOrCreate);
List<Document> _document22 = ProtoBuf.Serializer.Deserialize<List<Document>>(fs);
fs.Close();
}
}
[ProtoContract]
public class Document
{
public Document()
{
_count = 234234924;
Section = new Section();
Section.SectionName = "sdfasd";
}
[ProtoMember(1)]
public string DocumentName { get; set; }
[ProtoMember(2)]
Dictionary<string, List<string>> Hello { get; set; }
[ProtoMember(3, AsReference=true)]
public Section Section { get; set; }
[ProtoMember(4)]
private string _sectionName;
[ProtoMember(5)]
public string SectionName
{
get
{
return Section.SectionName;
}
set
{
_sectionName = value;
Section.SectionName = _sectionName;
}
}
public int _count;
public int Count
{
get
{
return _count;
}
}
}
[Serializable]
[ProtoContract]
public class Section
{
public Section()
{
Section1 = new SectionInner(this);
Hellos = new List<GeneralWrapper<List<string>>>();
GeneralWrapper<List<string>> _hello = new GeneralWrapper<List<string>>();
_hello.Datas.Add("hello");
Hellos.Add(_hello);
DHello = new Dictionary<string, List<Section>>();
if (!DHello.ContainsKey("asdf"))
{
List<Section> _dhello1 = new List<Section>();
_dhello1.Add(this);
DHello.Add("asdf", _dhello1);
}
}
[ProtoMember(1, AsReference=true)]
public SectionInner Section1 { get; set; }
[ProtoMember(2)]
public string SectionName { get; set; }
[ProtoMember(3, AsReference=true)]
public Dictionary<string, List<Section>> DHello { get; set; }
List<GeneralWrapper<List<string>>> Hellos { get; set; }
}
[Serializable]
[ProtoContract]
public class SectionInner
{
public SectionInner(Section section)
{
Section = section;
}
[ProtoMember(1, AsReference=true)]
public Section Section { get; set; }
}
}
I obviously made the code very recursive to begin with because it is the same thing my real project is doing. The problem seems to be this:
Dictionary<string, List<Section>>
When nothing is added to this dictionary, everything serializes fine. If a list is added to the dictionary with a certain key, recursion happens.
Is this code/syntax supported in protobuf-net?:
Dictionary<string, List<Section>>
Do I need to put the List in a outer wrapper class like:
Dictionary<string, Wrapper<List<Section>>>
Thanks for your help. I’m new to protobuf-net.
Firstly – I must note that performing that much setup in the constuctors is not a good idea. If that is indicative of your actual code , you might want to skip the constructors during deserialization (
[ProtoContract(SkipConstructor=true)]). If it is just illustrative, fine.Yes, things like dictionaries and lists are supported, however *directly nested lists are not – so a
List<List<...>>is not ok at the moment. You’ll probably get away with it with aDictionary<TKey,List<...>>because its he key-value-pair already acts as a wrapper in the middle.Re recursion: protobuf-net has support for many recursion scenarios, but this is as an extension to the formal specification. As such, you need to explicitly enable it, and note: it won’t be easy to interop this scenario, since there is no formal specification for this; but:
[ProtoMember(n, AsReference=true)]enables object tracking on an individual member. Note that all places that use this object must be marked as such, else they will use tree serialization.Re “why isn’t recursion supported directly” – because: protobuf (the formal spec) behaves like most serializers here, and is a tree serializer. Note: things like XmlSerializer, JavascriptSerializer and DataContractSerializer (in default mode) are also tree serializers, and will blow up if given a recursive structure. This is normal. protobuf-net goes out of its way to allow this in a few scenarios, but can’t enable it by default since it would require a different data layout, defeating the purpose of a cross-platform data specification.
If I’ve missed any of your questions, please say.