我遇到了protobuf-net的问题,希望它是用户错误,而不是protobuf-net的错误。
我可以序列化一个空集合(例如IDictionary<string, string>()
),然后反序列化该集合。反序列化导致一个非空对象(正是我序列化的对象)。
但是,如果该集合属于另一种类型,事情就会变得不妙。使用非空的空集合序列化自定义类型会导致反序列化,其中集合为null。
下面是我正在研究的示例以及说明问题的单元测试。
测试Test_Protobuf_EmptyDictionary_SerializeDeserialize
通过。测试Test_Protobuf_EmptyCollectionAndPopulatedCollection_SerializeDeserialize
失败。
[ProtoContract]
[ProtoInclude(100, typeof(ConcreteA))]
public abstract class AbstractClass
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public IDictionary<string, string> FieldsA { get; set; }
[ProtoMember(3)]
public IDictionary<string, string> FieldsB { get; set; }
[ProtoMember(4)]
public ICollection<string> FieldsC { get; set; }
[ProtoMember(5)]
public ICollection<string> FieldsD { get; set; }
}
[ProtoContract]
[ProtoInclude(110, typeof(ConcreteB))]
public class ConcreteA : AbstractClass
{
public ConcreteA() {}
}
[ProtoContract]
[ProtoInclude(120, typeof(ConcreteC))]
public class ConcreteB : ConcreteA
{
[ProtoMember(1)]
public int Age { get; set; }
public ConcreteB() {}
}
[ProtoContract]
public class ConcreteC : ConcreteB
{
[ProtoMember(1)]
public string HairColor { get; set; }
}
[TestFixture]
public class ProtobufTests
{
[Test]
public void Test_Protobuf_EmptyDictionary_SerializeDeserialize()
{
IDictionary<string,string> dictionary = new Dictionary<string, string>();
ICollection<string> collection = new List<string>();
Assert.IsNotNull(dictionary);
Assert.IsNotNull(collection);
Assert.AreEqual(0, dictionary.Keys.Count);
Assert.AreEqual(0, collection.Count);
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, dictionary);
ms.Position = 0;
var deserialized = Serializer.Deserialize<IDictionary<string, string>>(ms);
Assert.IsNotNull(deserialized);
Assert.AreEqual(0, deserialized.Keys.Count);
}
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, collection);
ms.Position = 0;
var deserialized = Serializer.Deserialize<ICollection<string>>(ms);
Assert.IsNotNull(deserialized);
Assert.AreEqual(0, deserialized.Count);
}
}
[Test]
public void Test_Protobuf_EmptyCollectionAndPopulatedCollection_SerializeDeserialize()
{
ConcreteC c = new ConcreteC {
FieldsA = new Dictionary<string, string>(),
FieldsB = new Dictionary<string, string> {{"john", "elway"}},
FieldsC = new List<string>(),
FieldsD = new List<string>{"james", "jones"}
};
Assert.IsNotNull(c);
Assert.IsNotNull(c.FieldsA);
Assert.IsNotNull(c.FieldsB);
Assert.IsNotNull(c.FieldsC);
Assert.IsNotNull(c.FieldsD);
Assert.AreEqual(0, c.FieldsA.Keys.Count);
Assert.AreEqual(1, c.FieldsB.Keys.Count);
Assert.AreEqual(0, c.FieldsC.Count);
Assert.AreEqual(2, c.FieldsD.Count);
using (MemoryStream ms = new MemoryStream())
{
Serializer.Serialize(ms, c);
ms.Position = 0;
var deserialized = Serializer.Deserialize<ConcreteC>(ms);
Assert.IsNotNull(deserialized);
Assert.IsNotNull(deserialized.FieldsA); // first failing test
Assert.IsNotNull(deserialized.FieldsB);
Assert.IsNotNull(deserialized.FieldsC);
Assert.IsNotNull(deserialized.FieldsD);
Assert.AreEqual(0, deserialized.FieldsA.Keys.Count);
Assert.AreEqual(1, deserialized.FieldsB.Keys.Count);
Assert.AreEqual(0, deserialized.FieldsC.Count);
Assert.AreEqual(1, deserialized.FieldsD.Count);
}
}
}
最终,这归因于protobuf规范没有null
的概念,也没有表达null
的方式。集合实际上只是repeated
块(根据.proto规范); 0个项目的repeated
块是无任何内容;没有字节;没有。由于protobuf中的收集成员本身不出现,所以没有地方可以说该对象是否为null或not-null。
用xml来说,如果就像使用[XmlElement]
将子项嵌入父项(而不是[XmlArray]
/ [XmlArrayItem]
-即]
<Foo>
<Name>abc</Name>
<Item>x</Item>
<Item>y</Item>
<Item>z</Item>
</Foo>
[这里,有[0]的Foo
为:
Item
由此,无法说出集合本身是<Foo>
<Name>abc</Name>
</Foo>
还是空。显然protobuf实际上不是xml,但是以上内容仅是示例性的示例。
因此:protobuf无法表达这种情况,因此protobuf-net也无法表达。
在另一种情况下,您表示:空列表(作为根元素)的序列化表示是:零字节。反序列化时,如果发现流为空,它将始终返回非null值作为根对象,该值是您想要的内容的最可能版本。
确保您具有公共默认构造函数,以便protobuf-net可以为您提供一个空对象。