Changeset 3005 for trunk/sources/HeuristicLab.Persistence/3.3/Core
- Timestamp:
- 03/11/10 15:50:50 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Core
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Core/DeSerializer.cs
r3004 r3005 77 77 parentStack = new Stack<Midwife>(); 78 78 typeIds = new Dictionary<int, Type>(); 79 serializerMapping = CreateSerializers(typeCache); 80 } 81 82 private Dictionary<Type, object> CreateSerializers(IEnumerable<TypeMapping> typeCache) { 83 Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>(); 79 serializerMapping = new Dictionary<Type, object>(); 80 foreach (var typeMapping in typeCache) { 81 AddTypeInfo(typeMapping); 82 } 83 } 84 85 private Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>(); 86 87 public void AddTypeInfo(TypeMapping typeMapping) { 88 if (typeIds.ContainsKey(typeMapping.Id)) 89 return; 84 90 try { 85 var map = new Dictionary<Type, object>(); 86 foreach (var typeMapping in typeCache) { 87 Type type = TypeLoader.Load(typeMapping.TypeName); 88 typeIds.Add(typeMapping.Id, type); 89 Type serializerType = TypeLoader.Load(typeMapping.Serializer); 90 object serializer; 91 if (serializerInstances.ContainsKey(serializerType)) 92 serializer = serializerInstances[serializerType]; 93 else 94 serializer = Activator.CreateInstance(serializerType, true); 95 map.Add(type, serializer); 96 } 97 return map; 91 Type type = TypeLoader.Load(typeMapping.TypeName); 92 typeIds.Add(typeMapping.Id, type); 93 Type serializerType = TypeLoader.Load(typeMapping.Serializer); 94 object serializer; 95 if (serializerInstances.ContainsKey(serializerType)) 96 serializer = serializerInstances[serializerType]; 97 else 98 serializer = Activator.CreateInstance(serializerType, true); 99 serializerMapping.Add(type, serializer); 98 100 } catch (PersistenceException) { 99 101 throw; 100 102 } catch (Exception e) { 101 throw new PersistenceException( 102 " The serialization type cache could not be loaded.\r\n" +103 "This usualy happens when you are missing an Assembly or Plugin.", e);103 throw new PersistenceException(string.Format( 104 "Could not add type info for {0} ({1})", 105 typeMapping.TypeName, typeMapping.Serializer), e); 104 106 } 105 107 } … … 125 127 } else if (t == typeof(MetaInfoEndToken)) { 126 128 MetaInfoEnd((MetaInfoEndToken)token); 129 } else if (t == typeof(TypeToken)) { 130 Type((TypeToken)token); 127 131 } else { 128 132 throw new PersistenceException("invalid token type"); … … 138 142 if (!m.MetaMode && m.Obj == null) 139 143 CreateInstance(m); 144 } 145 146 private void Type(TypeToken token) { 147 AddTypeInfo(new TypeMapping(token.Id, token.TypeName, token.Serializer)); 140 148 } 141 149 -
trunk/sources/HeuristicLab.Persistence/3.3/Core/GeneratorBase.cs
r3004 r3005 34 34 if (type == typeof(MetaInfoEndToken)) 35 35 return Format((MetaInfoEndToken)token); 36 if (type == typeof(TypeToken)) 37 return Format((TypeToken)token); 36 38 throw new ApplicationException("Invalid token of type " + type.FullName); 37 39 } … … 44 46 protected abstract T Format(MetaInfoBeginToken metaInfoBeginToken); 45 47 protected abstract T Format(MetaInfoEndToken metaInfoEndToken); 48 protected abstract T Format(TypeToken typeToken); 46 49 47 50 } -
trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs
r3004 r3005 49 49 private readonly bool isTestRun; 50 50 private readonly List<Exception> exceptions; 51 52 public bool InterleaveTypeInformation { get; set; } 51 53 52 54 /// <summary> … … 106 108 /// don't stop at the first exception</param> 107 109 public Serializer(object obj, Configuration configuration, string rootName, bool isTestRun) { 110 this.InterleaveTypeInformation = false; 108 111 this.obj = obj; 109 112 this.rootName = rootName; … … 144 147 if (obj2id.ContainsKey(value)) 145 148 return ReferenceEnumerator(accessor.Name, obj2id[value]); 146 if (!typeCache.ContainsKey(type)) 149 bool emitTypeInfo = false; 150 if (!typeCache.ContainsKey(type)) { 147 151 typeCache.Add(type, typeCache.Count); 152 emitTypeInfo = InterleaveTypeInformation; 153 } 148 154 int typeId = typeCache[type]; 149 155 int? id = null; … … 155 161 IPrimitiveSerializer primitiveSerializer = configuration.GetPrimitiveSerializer(type); 156 162 if (primitiveSerializer != null) 157 return PrimitiveEnumerator(accessor.Name, typeId, primitiveSerializer.Format(value), id); 163 return PrimitiveEnumerator( 164 accessor.Name, 165 typeId, 166 primitiveSerializer.Format(value), 167 id, 168 emitTypeInfo); 158 169 ICompositeSerializer compositeSerializer = configuration.GetCompositeSerializer(type); 159 170 if (compositeSerializer != null) 160 return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value)); 171 return CompositeEnumerator( 172 accessor.Name, 173 compositeSerializer.Decompose(value), 174 id, 175 typeId, 176 compositeSerializer.CreateMetaInfo(value), 177 emitTypeInfo); 161 178 throw CreatePersistenceException(type); 162 179 } catch (Exception x) { … … 200 217 201 218 private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name, 202 int typeId, ISerialData serializedValue, int? id) { 219 int typeId, ISerialData serializedValue, int? id, bool emitTypeInfo) { 220 if (emitTypeInfo) { 221 var mapping = TypeCache[typeId]; 222 yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer); 223 } 203 224 yield return new PrimitiveToken(name, typeId, id, serializedValue); 204 225 } 205 226 206 227 private IEnumerator<ISerializationToken> CompositeEnumerator( 207 string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo) { 228 string name, IEnumerable<Tag> tags, int? id, int typeId, IEnumerable<Tag> metaInfo, 229 bool emitTypeInfo) { 230 if (emitTypeInfo) { 231 var mapping = TypeCache[typeId]; 232 yield return new TypeToken(mapping.Id, mapping.TypeName, mapping.Serializer); 233 } 208 234 yield return new BeginToken(name, typeId, id); 209 235 bool first = true;
Note: See TracChangeset
for help on using the changeset viewer.