1 | using System.Collections.Generic;
|
---|
2 | using System;
|
---|
3 | using HeuristicLab.Persistence.Interfaces;
|
---|
4 | using HeuristicLab.Persistence.Core.Tokens;
|
---|
5 | using HeuristicLab.Persistence.Auxiliary;
|
---|
6 | using HeuristicLab.Tracing;
|
---|
7 | using System.Reflection;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Persistence.Core {
|
---|
10 |
|
---|
11 | public class Deserializer {
|
---|
12 |
|
---|
13 | class Midwife {
|
---|
14 |
|
---|
15 | public int? Id { get; private set; }
|
---|
16 | public bool MetaMode { get; set; }
|
---|
17 | public object Obj { get; private set; }
|
---|
18 |
|
---|
19 | private List<Tag> metaInfo;
|
---|
20 | private List<Tag> customValues;
|
---|
21 | private Type type;
|
---|
22 | private ICompositeSerializer compositeSerializer;
|
---|
23 |
|
---|
24 | public Midwife(object value) {
|
---|
25 | this.Obj = value;
|
---|
26 | }
|
---|
27 |
|
---|
28 | public Midwife(Type type, ICompositeSerializer compositeSerializer, int? id) {
|
---|
29 | this.type = type;
|
---|
30 | this.compositeSerializer = compositeSerializer;
|
---|
31 | this.Id = id;
|
---|
32 | MetaMode = false;
|
---|
33 | metaInfo = new List<Tag>();
|
---|
34 | customValues = new List<Tag>();
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void CreateInstance() {
|
---|
38 | if (Obj != null)
|
---|
39 | throw new PersistenceException("object already instantiated");
|
---|
40 | Obj = compositeSerializer.CreateInstance(type, metaInfo);
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void AddValue(string name, object value) {
|
---|
44 | if (MetaMode) {
|
---|
45 | metaInfo.Add(new Tag(name, value));
|
---|
46 | } else {
|
---|
47 | customValues.Add(new Tag(name, value));
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void Populate() {
|
---|
52 | compositeSerializer.Populate(Obj, customValues, type);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private readonly Dictionary<int, object> id2obj;
|
---|
57 | private readonly Dictionary<Type, object> serializerMapping;
|
---|
58 | private readonly Stack<Midwife> parentStack;
|
---|
59 | private readonly Dictionary<int, Type> typeIds;
|
---|
60 |
|
---|
61 | public Deserializer(
|
---|
62 | IEnumerable<TypeMapping> typeCache) {
|
---|
63 | id2obj = new Dictionary<int, object>();
|
---|
64 | parentStack = new Stack<Midwife>();
|
---|
65 | typeIds = new Dictionary<int, Type>();
|
---|
66 | serializerMapping = CreateSerializers(typeCache);
|
---|
67 | }
|
---|
68 |
|
---|
69 | private Dictionary<Type, object> CreateSerializers(IEnumerable<TypeMapping> typeCache) {
|
---|
70 | Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>();
|
---|
71 | try {
|
---|
72 | var map = new Dictionary<Type, object>();
|
---|
73 | foreach (var typeMapping in typeCache) {
|
---|
74 | Type type = TypeLoader.Load(typeMapping.TypeName);
|
---|
75 | typeIds.Add(typeMapping.Id, type);
|
---|
76 | Type serializerType = TypeLoader.Load(typeMapping.Serializer);
|
---|
77 | object serializer;
|
---|
78 | if (serializerInstances.ContainsKey(serializerType))
|
---|
79 | serializer = serializerInstances[serializerType];
|
---|
80 | else
|
---|
81 | serializer = Activator.CreateInstance(serializerType, true);
|
---|
82 | map.Add(type, serializer);
|
---|
83 | }
|
---|
84 | return map;
|
---|
85 | } catch (PersistenceException) {
|
---|
86 | throw;
|
---|
87 | } catch (Exception e) {
|
---|
88 | throw new PersistenceException(
|
---|
89 | "The serialization type cache could not be loaded.\r\n" +
|
---|
90 | "This usualy happens when you are missing an Assembly or Plugin.", e);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | public object Deserialize(IEnumerable<ISerializationToken> tokens) {
|
---|
96 | foreach (ISerializationToken token in tokens) {
|
---|
97 | Type t = token.GetType();
|
---|
98 | if (t == typeof(BeginToken)) {
|
---|
99 | CompositeStartHandler((BeginToken)token);
|
---|
100 | } else if (t == typeof(EndToken)) {
|
---|
101 | CompositeEndHandler((EndToken)token);
|
---|
102 | } else if (t == typeof(PrimitiveToken)) {
|
---|
103 | PrimitiveHandler((PrimitiveToken)token);
|
---|
104 | } else if (t == typeof(ReferenceToken)) {
|
---|
105 | ReferenceHandler((ReferenceToken)token);
|
---|
106 | } else if (t == typeof(NullReferenceToken)) {
|
---|
107 | NullHandler((NullReferenceToken)token);
|
---|
108 | } else if (t == typeof(MetaInfoBeginToken)) {
|
---|
109 | MetaInfoBegin((MetaInfoBeginToken)token);
|
---|
110 | } else if (t == typeof(MetaInfoEndToken)) {
|
---|
111 | MetaInfoEnd((MetaInfoEndToken)token);
|
---|
112 | } else {
|
---|
113 | throw new PersistenceException("invalid token type");
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return parentStack.Pop().Obj;
|
---|
117 | }
|
---|
118 |
|
---|
119 | private void InstantiateParent() {
|
---|
120 | if (parentStack.Count == 0)
|
---|
121 | return;
|
---|
122 | Midwife m = parentStack.Peek();
|
---|
123 | if (!m.MetaMode && m.Obj == null)
|
---|
124 | CreateInstance(m);
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void CompositeStartHandler(BeginToken token) {
|
---|
128 | InstantiateParent();
|
---|
129 | Type type = typeIds[(int)token.TypeId];
|
---|
130 | try {
|
---|
131 | parentStack.Push(new Midwife(type, (ICompositeSerializer)serializerMapping[type], token.Id));
|
---|
132 | } catch (Exception e) {
|
---|
133 | if (e is InvalidCastException || e is KeyNotFoundException) {
|
---|
134 | throw new PersistenceException(String.Format(
|
---|
135 | "Invalid composite serializer configuration for type \"{0}\".",
|
---|
136 | type.AssemblyQualifiedName), e);
|
---|
137 | } else {
|
---|
138 | throw new PersistenceException(String.Format(
|
---|
139 | "Unexpected exception while trying to compose object of type \"{0}\".",
|
---|
140 | type.AssemblyQualifiedName), e);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void CompositeEndHandler(EndToken token) {
|
---|
146 | Type type = typeIds[(int)token.TypeId];
|
---|
147 | Midwife midwife = parentStack.Pop();
|
---|
148 | if (midwife.Obj == null)
|
---|
149 | CreateInstance(midwife);
|
---|
150 | midwife.Populate();
|
---|
151 | SetValue(token.Name, midwife.Obj);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void PrimitiveHandler(PrimitiveToken token) {
|
---|
155 | Type type = typeIds[(int)token.TypeId];
|
---|
156 | try {
|
---|
157 | object value = ((IPrimitiveSerializer)serializerMapping[type]).Parse(token.SerialData);
|
---|
158 | if (token.Id != null)
|
---|
159 | id2obj[(int)token.Id] = value;
|
---|
160 | SetValue(token.Name, value);
|
---|
161 | } catch (Exception e) {
|
---|
162 | if (e is InvalidCastException || e is KeyNotFoundException) {
|
---|
163 | throw new PersistenceException(String.Format(
|
---|
164 | "Invalid primitive serializer configuration for type \"{0}\".",
|
---|
165 | type.AssemblyQualifiedName), e);
|
---|
166 | } else {
|
---|
167 | throw new PersistenceException(String.Format(
|
---|
168 | "Unexpected exception while trying to parse object of type \"{0}\".",
|
---|
169 | type.AssemblyQualifiedName), e);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | private void ReferenceHandler(ReferenceToken token) {
|
---|
175 | object referredObject = id2obj[token.Id];
|
---|
176 | SetValue(token.Name, referredObject);
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void NullHandler(NullReferenceToken token) {
|
---|
180 | SetValue(token.Name, null);
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void MetaInfoBegin(MetaInfoBeginToken token) {
|
---|
184 | parentStack.Peek().MetaMode = true;
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void MetaInfoEnd(MetaInfoEndToken token) {
|
---|
188 | Midwife m = parentStack.Peek();
|
---|
189 | m.MetaMode = false;
|
---|
190 | CreateInstance(m);
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void CreateInstance(Midwife m) {
|
---|
194 | m.CreateInstance();
|
---|
195 | if (m.Id != null)
|
---|
196 | id2obj.Add((int)m.Id, m.Obj);
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void SetValue(string name, object value) {
|
---|
200 | if (parentStack.Count == 0) {
|
---|
201 | parentStack.Push(new Midwife(value));
|
---|
202 | } else {
|
---|
203 | Midwife m = parentStack.Peek();
|
---|
204 | if (m.MetaMode == false && m.Obj == null) {
|
---|
205 | CreateInstance(m);
|
---|
206 | }
|
---|
207 | m.AddValue(name, value);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | } |
---|