Changeset 1362 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/19/09 14:02:35 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1361 r1362 75 75 76 76 private readonly Dictionary<int, object> id2obj; 77 private readonly Dictionary<Type, object> serializerMapping; 77 78 private readonly Dictionary<Type, Handler> handlers; 78 private readonly Stack<IAccessibleObject> parentStack; 79 private readonly Configuration configuration; 79 private readonly Stack<IAccessibleObject> parentStack; 80 80 private readonly Dictionary<int, Type> typeIds; 81 81 private List<Thunk> finalFixes; 82 82 83 83 public DeSerializer( 84 IEnumerable<KeyValuePair<string, int>> typeCache, 85 Configuration configuration) { 86 this.configuration = configuration; 84 IEnumerable<TypeMapping> typeCache) { 87 85 id2obj = new Dictionary<int, object>(); 88 86 parentStack = new Stack<IAccessibleObject>(); … … 95 93 }; 96 94 typeIds = new Dictionary<int, Type>(); 97 foreach ( var pair in typeCache ) { 98 Type type = Type.GetType(pair.Key); 99 typeIds.Add(pair.Value, type); 95 serializerMapping = new Dictionary<Type, object>(); 96 foreach ( var typeMapping in typeCache ) { 97 Type type = Type.GetType(typeMapping.TypeName); 98 typeIds.Add(typeMapping.Id, type); 99 if (typeMapping.Serializer != null) { 100 Type serializerType = Type.GetType(typeMapping.Serializer); 101 serializerMapping.Add(type, Activator.CreateInstance(serializerType, true)); 102 } 100 103 } 101 104 } … … 116 119 object instance; 117 120 Type type = typeIds[(int)start.TypeId]; 118 if (configuration.GetDecomposer(type) != null) { 121 IDecomposer decomposer = null; 122 if ( serializerMapping.ContainsKey(type) ) 123 decomposer = serializerMapping[type] as IDecomposer; 124 if (decomposer != null) { 119 125 instance = new ParentReference(); 120 126 parentStack.Push(new CustomComposite(instance)); … … 132 138 EndToken end = (EndToken)token; 133 139 Type type = typeIds[(int)end.TypeId]; 134 IDecomposer decomposer = configuration.GetDecomposer(type); 140 IDecomposer decomposer = null; 141 if (serializerMapping.ContainsKey(type)) 142 decomposer = serializerMapping[type] as IDecomposer; 135 143 if (decomposer != null) { 136 144 CustomComposite customComposite = (CustomComposite)parentStack.Pop(); … … 150 158 PrimitiveToken primitive = (PrimitiveToken)token; 151 159 Type type = typeIds[(int)primitive.TypeId]; 152 object value = configuration 153 .GetFormatter(type) 154 .Parse(primitive.SerialData); 160 object value = ((IFormatter) serializerMapping[type]).Parse(primitive.SerialData); 155 161 if ( ! value.GetType().IsValueType ) 156 162 id2obj[(int)primitive.Id] = value; … … 172 178 NullReferenceToken nil = (NullReferenceToken)token; 173 179 SetValue(nil.Name, null); 174 } 180 } 175 181 176 182 private void SetValue(string name, object value) { -
branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs
r1361 r1362 5 5 6 6 namespace HeuristicLab.Persistence.Core { 7 8 public struct TypeMapping { 9 public readonly int Id; 10 public readonly string TypeName; 11 public readonly string Serializer; 12 public TypeMapping(int id, string typeName, string serializer) { 13 Id = id; 14 TypeName = typeName; 15 Serializer = serializer; 16 } 17 public Dictionary<string, object> GetDict() { 18 return new Dictionary<string, object> { 19 {"id", Id}, 20 {"typeName", TypeName}, 21 {"serializer", Serializer}}; 22 } 23 } 7 24 8 25 public class Serializer : IEnumerable<ISerializationToken> { … … 14 31 private readonly Configuration configuration; 15 32 16 public Dictionary<string, int> TypeCache {33 public List<TypeMapping> TypeCache { 17 34 get { 18 Dictionary<string, int> result = new Dictionary<string, int>(); 19 foreach ( var pair in typeCache ) 20 result.Add(pair.Key.AssemblyQualifiedName, pair.Value); 35 List<TypeMapping> result = new List<TypeMapping>(); 36 foreach (var pair in typeCache) { 37 string serializer = null; 38 IFormatter f = configuration.GetFormatter(pair.Key); 39 if (f != null) { 40 serializer = f.GetType().AssemblyQualifiedName; 41 } else { 42 IDecomposer d = configuration.GetDecomposer(pair.Key); 43 if (d != null) 44 serializer = d.GetType().AssemblyQualifiedName; 45 } 46 result.Add(new TypeMapping(pair.Value, pair.Key.AssemblyQualifiedName, serializer)); 47 } 21 48 return result; 22 49 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs
r1361 r1362 3 3 using System.Text; 4 4 using HeuristicLab.Persistence.Interfaces; 5 using HeuristicLab.Persistence.Core; 5 6 6 7 namespace HeuristicLab.Persistence.Default.Xml { … … 108 109 } 109 110 110 public IEnumerable<string> Format( Dictionary<string, int> typeCache) {111 public IEnumerable<string> Format(List<TypeMapping> typeCache) { 111 112 yield return "<" + XmlStrings.TYPECACHE + ">"; 112 foreach ( var pair in typeCache ) 113 yield return FormatNode(XmlStrings.TYPE, 114 new Dictionary<string, object> {{"id", pair.Value}, {"name", pair.Key}}, 115 NodeType.Inline); 113 foreach (var mapping in typeCache) 114 yield return FormatNode(XmlStrings.TYPE, mapping.GetDict(), NodeType.Inline); 116 115 yield return "</" + XmlStrings.TYPECACHE + ">"; 117 116 } -
branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlParser.cs
r1361 r1362 4 4 using System.Collections; 5 5 using System.IO; 6 using HeuristicLab.Persistence.Core; 6 7 using HeuristicLab.Persistence.Interfaces; 7 8 8 9 namespace HeuristicLab.Persistence.Default.Xml { 9 10 10 11 11 public class XmlParser : IEnumerable<ISerializationToken> { … … 91 91 } 92 92 93 public static Dictionary<string, int> ParseTypeCache(TextReader reader) {94 Dictionary<string, int> typeCache = new Dictionary<string, int>();93 public static List<TypeMapping> ParseTypeCache(TextReader reader) { 94 var typeCache = new List<TypeMapping>(); 95 95 XmlReader xmlReader = XmlReader.Create(reader); 96 96 while ( xmlReader.Read() ) { 97 if (xmlReader.Name == XmlStrings.TYPE) { 98 typeCache.Add(xmlReader.GetAttribute("name") 99 , int.Parse(xmlReader.GetAttribute("id"))); 97 if (xmlReader.Name == XmlStrings.TYPE) { 98 typeCache.Add(new TypeMapping( 99 int.Parse(xmlReader.GetAttribute("id")), 100 xmlReader.GetAttribute("typeName"), 101 xmlReader.GetAttribute("serializer"))); 100 102 } 101 103 } -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1361 r1362 172 172 XmlParser parser = new XmlParser(new StreamReader("test.xml")); 173 173 DeSerializer deSerializer = new DeSerializer( 174 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 175 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 174 XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 176 175 object o = deSerializer.DeSerialize(parser); 177 176 Console.Out.WriteLine(Util.AutoFormat(o, true)); … … 192 191 XmlParser parser = new XmlParser(new StreamReader("test2.xml")); 193 192 DeSerializer deSerializer = new DeSerializer( 194 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 195 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 193 XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 196 194 object o = deSerializer.DeSerialize(parser); 197 195 Console.Out.WriteLine(Util.AutoFormat(o, true));
Note: See TracChangeset
for help on using the changeset viewer.