序列化前缀树
当序列化一个树结构时,我得到一个ProtoException
(“检测到可能的递归(偏移:4个级别):EOW”):
var tree = new PrefixTree();
tree.Add("racket".ToCharArray());
tree.Add("rambo".ToCharArray());
using (var stream = File.Open("test.prefix", FileMode.Create))
{
Serializer.Serialize(stream, tree);
}
树的实现:
[ProtoContract]
public class PrefixTree
{
public PrefixTree()
{
_nodes = new Dictionary<char, PrefixTree>();
}
public PrefixTree(char[] chars, PrefixTree parent)
{
if (chars == null) throw new ArgumentNullException("chars");
if (parent == null) throw new ArgumentNullException("parent");
if (chars.Length == 0) throw new ArgumentException();
_parent = parent;
_nodes = new Dictionary<char, PrefixTree>();
_value = chars[0];
var overflow = chars.SubSet(1);
if (!overflow.Any()) _endOfWord = true;
else Add(overflow.ToArray());
}
[ProtoMember(1)]
private readonly char _value;
[ProtoMember(2)]
private readonly bool _endOfWord;
[ProtoMember(3)]
private readonly IDictionary<char, PrefixTree> _nodes;
[ProtoMember(4, AsReference = true)]
private readonly PrefixTree _parent;
public void Add(char[] word)
{
if (word == null) throw new ArgumentNullException("word");
if (word.Length == 0) return;
var character = word[0];
PrefixTree node;
if (_nodes.TryGetValue(character, out node))
{
node.Add(word.SubSet(1));
}
else
{
node = new PrefixTree(word, this);
_nodes.Add(character, node);
}
}
public override string ToString()
{
return _endOfWord ? _value + " EOW" : _value.ToString();
}
}
public static class ListHelper
{
public static char[] SubSet(this char[] source, int start)
{
return source.SubSet(start, source.Length - start);
}
public static char[] SubSet(this char[] source, int start, int length)
{
if (start < 0) throw new ArgumentOutOfRangeException();
if (start > source.Length) throw new ArgumentOutOfRangeException();
if (length < 0) throw new ArgumentOutOfRangeException();
var result = new char[length];
Array.Copy(source, start, result, 0, length);
return result;
}
}
我装饰了错误的属性还是仅仅设计了一个不可序列化的树?
编辑:试过这个无济于事:
var typeModel = RuntimeTypeModel.Default;
var type = typeModel.Add(typeof(PrefixTree), false);
type.AsReferenceDefault = true;
type.Add("_value", "_endOfWord", "_nodes", "_parent");
var tree = new PrefixTree();
tree.Add("racket".ToCharArray());
tree.Add("rambo".ToCharArray());
using (var stream = File.Open("test.prefix", FileMode.Create))
{
typeModel.Serialize(stream, tree);
}
_node的_parent和Value都指向相同类型(PrefixTree),但只有_parent被标记为“AsReference”。
如果您使用序列化堆栈,将会看到字典值的值是独立于_parent项目进行序列化的,并且不会检查重复实例。
当它走过树时,会有一个25的内部序列化深度检查,在此处它开始检测重复的实例。 如果此值较大,则不会抛出异常,如果它较小,则会抛出树上较高的节点。
我也不认为这将是反序列化的,当然,如果它确实每个子节点的_parent字段的值不会与_nodes容器相同。
您需要创建自己的字典类型(子类Dictionary <,>或实现IDictionary <,>),以便添加[ProtoContract]属性并控制字典项目的序列化。
即
[ProtoContract]
public class NodeItem
{
[ProtoMember(1)]
public char Key { get; set; }
[ProtoMember(2, AsReference = true)]
public PrefixTree Value { get; set; }
}
[ProtoContract]
public class Nodes : IDictionary<char, PrefixTree>
{
private readonly IDictionary<char, PrefixTree> inner;
[ProtoMember(1)]
public NodeItem[] Items
{
get
{
return this.inner.Select(item => new NodeItem() {Key = item.Key, Value = item.Value}).ToArray();
}
set
{
foreach( NodeItem item in value)
{
this.inner.Add(item.Key, item.Value);
}
}
}
... // Omitted IDictionary members for clarity
这里的关键是将AsReference元数据附加到节点的PrefixTree。 还要注意,如果你想要它作为一个列表,Items正在返回一个数组,那么你需要使用设置OverwriteList属性成员。
我还需要删除PrefixTree类型中每个字段的readonly关键字。 这个单元测试通过了我。
[TestMethod]
public void TestMethod1()
{
var tree = new PrefixTree();
tree.Add("racket".ToCharArray());
tree.Add("rambo".ToCharArray());
PrefixTree tree2 = null;
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, tree);
stream.Position = 0;
tree2 = Serializer.Deserialize<PrefixTree>(stream);
}
Assert.IsNotNull(tree2);
Assert.AreEqual(tree._nodes.Count, tree2._nodes.Count);
Assert.AreEqual(2, tree2._nodes['r']._nodes['a']._nodes.Count); // 'c' and 'm'
Assert.AreEqual('c', tree2._nodes['r']._nodes['a']._nodes.Values.First().Value);
Assert.AreEqual('m', tree2._nodes['r']._nodes['a']._nodes.Values.Last().Value);
}
链接地址: http://www.djcxy.com/p/10765.html