- Timestamp:
- 04/16/09 12:58:14 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs
r1556 r1566 4 4 using HeuristicLab.Persistence.Core.Tokens; 5 5 6 namespace HeuristicLab.Persistence.Core { 6 namespace HeuristicLab.Persistence.Core { 7 7 8 public class ParentReference { }8 public class ParentReference { } 9 9 10 10 class Midwife { 11 12 public int? Id { get; private set; } 11 12 public int? Id { get; private set; } 13 13 public bool MetaMode { get; set; } 14 14 public object Obj { get; private set; } … … 22 22 this.Obj = value; 23 23 } 24 25 public Midwife(Type type, IDecomposer decomposer, int? id) { 24 25 public Midwife(Type type, IDecomposer decomposer, int? id) { 26 26 this.type = type; 27 this.decomposer = decomposer; 27 this.decomposer = decomposer; 28 28 this.Id = id; 29 MetaMode = false; 29 MetaMode = false; 30 30 metaInfo = new List<Tag>(); 31 customValues = new List<Tag>(); 31 customValues = new List<Tag>(); 32 32 } 33 33 … … 35 35 if (Obj != null) 36 36 throw new ApplicationException("object already instantiated"); 37 Obj = decomposer.CreateInstance(type, metaInfo); 37 Obj = decomposer.CreateInstance(type, metaInfo); 38 38 } 39 39 … … 49 49 decomposer.Populate(Obj, customValues, type); 50 50 } 51 } 51 } 52 52 53 53 public class Deserializer { 54 54 55 55 private readonly Dictionary<int, object> id2obj; 56 56 private readonly Dictionary<Type, object> serializerMapping; 57 private readonly Stack<Midwife> parentStack; 57 private readonly Stack<Midwife> parentStack; 58 58 private readonly Dictionary<int, Type> typeIds; 59 59 … … 79 79 } 80 80 81 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 81 public object Deserialize(IEnumerable<ISerializationToken> tokens) { 82 82 foreach (ISerializationToken token in tokens) { 83 83 Type t = token.GetType(); 84 if ( t == typeof(BeginToken)) {84 if (t == typeof(BeginToken)) { 85 85 CompositeStartHandler((BeginToken)token); 86 } else if ( t == typeof(EndToken)) {87 CompositeEndHandler((EndToken) 88 } else if ( t == typeof(PrimitiveToken)) {89 PrimitiveHandler((PrimitiveToken) 90 } else if ( t == typeof(ReferenceToken)) {91 ReferenceHandler((ReferenceToken) 86 } else if (t == typeof(EndToken)) { 87 CompositeEndHandler((EndToken)token); 88 } else if (t == typeof(PrimitiveToken)) { 89 PrimitiveHandler((PrimitiveToken)token); 90 } else if (t == typeof(ReferenceToken)) { 91 ReferenceHandler((ReferenceToken)token); 92 92 } else if (t == typeof(NullReferenceToken)) { 93 93 NullHandler((NullReferenceToken)token); … … 106 106 Type type = typeIds[(int)token.TypeId]; 107 107 IDecomposer decomposer = null; 108 if ( serializerMapping.ContainsKey(type))108 if (serializerMapping.ContainsKey(type)) 109 109 decomposer = serializerMapping[type] as IDecomposer; 110 110 if (decomposer == null) … … 124 124 private void PrimitiveHandler(PrimitiveToken token) { 125 125 Type type = typeIds[(int)token.TypeId]; 126 object value = ((IFormatter) 127 if ( token.Id != null )126 object value = ((IFormatter)serializerMapping[type]).Parse(token.SerialData); 127 if (token.Id != null) 128 128 id2obj[(int)token.Id] = value; 129 129 SetValue(token.Name, value); … … 144 144 145 145 private void MetaInfoEnd(MetaInfoEndToken token) { 146 Midwife m = parentStack.Peek(); 146 Midwife m = parentStack.Peek(); 147 147 m.MetaMode = false; 148 148 CreateInstance(m); … … 152 152 m.CreateInstance(); 153 153 if (m.Id != null) 154 id2obj.Add((int)m.Id, m.Obj); 154 id2obj.Add((int)m.Id, m.Obj); 155 155 } 156 156 157 157 private void SetValue(string name, object value) { 158 if (parentStack.Count == 0) { 158 if (parentStack.Count == 0) { 159 159 parentStack.Push(new Midwife(value)); 160 160 } else { … … 162 162 if (m.MetaMode == false && m.Obj == null) 163 163 CreateInstance(m); 164 m.AddValue(name, value); 164 m.AddValue(name, value); 165 165 } 166 166 }
Note: See TracChangeset
for help on using the changeset viewer.