Protobuf.net object graph serialization for lists
I understand that AsReference is not supported for lists with protobuf.net, so I have attempted a work-around for this limitation. I have created a custom list called SuperList that contains items wrapped in objects of type SuperListItem as follows:
[ProtoContract]
public class SuperList<T> where T : class
{
[ProtoMember(1)]
private List<SuperListItem<T>> _items = new List<SuperListItem<T>>();
public SuperList()
{
}
public int IndexOf(T item)
{
int indexOf = -1;
for (int index = 0; index < _items.Count; index++)
{
if (_items[index].Item == item)
{
indexOf = index;
break;
}
}
return indexOf;
}
public void Insert(int index, T item)
{
_items.Insert(index, new SuperListItem<T>(item));
}
public void RemoveAt(int index)
{
_items.RemoveAt(index);
}
public T this[int index]
{
get
{
return _items[index].Item;
}
set
{
_items[index] = new SuperListItem<T>(value);
}
}
public void Add(T item)
{
_items.Add(new SuperListItem<T>(item));
}
public void Clear()
{
_items.Clear();
}
public bool Contains(T item)
{
bool contains = false;
foreach (var listItem in _items)
{
if (listItem.Item == item)
{
contains = true;
break;
}
}
return contains;
}
public void CopyTo(T[] array, int arrayIndex)
{
for (int index = arrayIndex; index < _items.Count; index++)
array[index] = _items[index].Item;
}
public int Count
{
get { return _items.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
SuperListItem<T> itemToRemove = null;
foreach (var listItem in _items)
{
if (listItem.Item == item)
{
itemToRemove = listItem;
break;
}
}
if (itemToRemove != null)
_items.Remove(itemToRemove);
return itemToRemove != null;
}
public IEnumerator<T> GetEnumerator()
{
foreach(var listItem in _items)
yield return listItem.Item;
}
}
[ProtoContract]
public class SuperListItem<T>
{
[ProtoMember(1, AsReference = true)]
private readonly T _item;
public T Item { get { return _item; } }
private SuperListItem() { }
public SuperListItem(T item)
{
_item = item;
}
}
I have the following test code for the serialization:
[ProtoContract]
public class Thing
{
[ProtoMember(1)]
private readonly string _name;
public string Name { get { return _name; } }
private Thing() { }
public Thing(string name)
{
_name = name;
}
}
public class ProtoTest3
{
public void Serialize()
{
SuperList<Thing> list = GetListOfThings();
using (var fs = File.Create(@"c:\temp\things.bin"))
{
ProtoBuf.Serializer.Serialize(fs, list);
fs.Close();
}
using (var fs = File.OpenRead(@"c:\temp\things.bin"))
{
list = ProtoBuf.Serializer.Deserialize<SuperList<Thing>>(fs);
Debug.Assert(list[0] == list[2]);
fs.Close();
}
}
private SuperList<Thing> GetListOfThings()
{
var thing1 = new Thing("thing1");
var thing2 = new Thing("thing2");
var list = new SuperList<Thing>();
list.Add(thing1);
list.Add(thing2);
list.Add(thing1);
return list;
}
}
开发者_JAVA百科
However, when I run the code, it I get the exception "No parameterless constructor defined for this object". Is it simply a limitation in ProtoBuf.Net, or have I done something wrong. Is there a way around this problem?
To clarify; the limitation on lists is simply that the list itself is not treated as a reference. The items are, as long as they are on a member marked AsReference
- for example:
[Test]
public void SerializeTheEasyWay()
{
var list = GetListOfThings();
using (var fs = File.Create(@"things.bin"))
{
ProtoBuf.Serializer.Serialize(fs, list);
fs.Close();
}
using (var fs = File.OpenRead(@"things.bin"))
{
list = ProtoBuf.Serializer.Deserialize<MyDto>(fs);
Assert.AreEqual(3, list.Things.Count);
Assert.AreNotSame(list.Things[0], list.Things[1]);
Assert.AreSame(list.Things[0], list.Things[2]);
fs.Close();
}
}
[ProtoContract]
public class MyDto
{
[ProtoMember(1, AsReference = true)]
public List<Thing> Things { get; set; }
}
private MyDto GetListOfThings()
{
var thing1 = new Thing("thing1");
var thing2 = new Thing("thing2");
var list = new List<Thing>();
list.Add(thing1);
list.Add(thing2);
list.Add(thing1);
return new MyDto {Things = list};
}
(and fun fact - this is actually exactly the same on the wire)
There does, however, seem to be a bug with how it creates the instance in this case; it is using the .ctor
and failing. I will investigate and fix this, however the following also work:
1: make the parameterless .ctor
public:
public Thing()
{
}
2: or alternatively, disable the .ctor
:
[ProtoContract(SkipConstructor = true)]
public class Thing
{ ...
I will investigate why private parameterless constructors aren't happy in this scenario.
精彩评论