-
Notifications
You must be signed in to change notification settings - Fork 1
/
Serializer.cs
63 lines (53 loc) · 1.86 KB
/
Serializer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Collections.Generic;
using System.Diagnostics;
namespace chainer.serializers
{
public abstract class Serializer
{
public abstract Serializer Traverse(string key);
public abstract void Communicate(string key, Variable value);
}
public class DictionarySerializer : Serializer
{
public Dictionary<string, Variable> Target;
private readonly string _path;
public DictionarySerializer(Dictionary<string, Variable> target = null, string path = "")
{
Target = target ?? new Dictionary<string, Variable>();
_path = path;
}
public override Serializer Traverse(string key)
{
return new DictionarySerializer(Target, _path + key + "/");
}
public override void Communicate(string key, Variable value)
{
Debug.Assert(!Target.ContainsKey(key));
Target[_path + key] = value;
}
}
public class DictionaryDeserializer : Serializer
{
private Dictionary<string, Variable> _source;
private readonly string _path;
public DictionaryDeserializer(Dictionary<string, Variable> source, string path = "")
{
_source = source;
_path = path;
}
public override Serializer Traverse(string key)
{
return new DictionaryDeserializer(_source, _path + key + "/");
}
public override void Communicate(string key, Variable value)
{
var fullpath = _path + key;
if (_source.ContainsKey(fullpath))
{
Debug.Assert(value.Value.RowCount == _source[fullpath].Value.RowCount);
Debug.Assert(value.Value.ColumnCount == _source[fullpath].Value.ColumnCount);
value.Value = _source[fullpath].Value.Clone();
}
}
}
}