Changeset 1362


Ignore:
Timestamp:
03/19/09 14:02:35 (11 years ago)
Author:
epitzer
Message:

Add serializer class information to type cache. (#506)

Location:
branches/New Persistence Exploration/Persistence
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs

    r1361 r1362  
    7575
    7676    private readonly Dictionary<int, object> id2obj;
     77    private readonly Dictionary<Type, object> serializerMapping;
    7778    private readonly Dictionary<Type, Handler> handlers;
    78     private readonly Stack<IAccessibleObject> parentStack;
    79     private readonly Configuration configuration;
     79    private readonly Stack<IAccessibleObject> parentStack;   
    8080    private readonly Dictionary<int, Type> typeIds;   
    8181    private List<Thunk> finalFixes;
    8282
    8383    public DeSerializer(
    84       IEnumerable<KeyValuePair<string, int>> typeCache,
    85       Configuration configuration) {
    86       this.configuration = configuration;
     84      IEnumerable<TypeMapping> typeCache) {     
    8785      id2obj = new Dictionary<int, object>();
    8886      parentStack = new Stack<IAccessibleObject>();
     
    9593                   };     
    9694      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        }
    100103      }
    101104    }
     
    116119      object instance;     
    117120      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) {
    119125        instance = new ParentReference();
    120126        parentStack.Push(new CustomComposite(instance));       
     
    132138      EndToken end = (EndToken)token;
    133139      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;           
    135143      if (decomposer != null) {
    136144        CustomComposite customComposite = (CustomComposite)parentStack.Pop();
     
    150158      PrimitiveToken primitive = (PrimitiveToken)token;
    151159      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);
    155161      if ( ! value.GetType().IsValueType )
    156162        id2obj[(int)primitive.Id] = value;
     
    172178      NullReferenceToken nil = (NullReferenceToken)token;
    173179      SetValue(nil.Name, null);
    174     }
     180    }   
    175181
    176182    private void SetValue(string name, object value) {
  • branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs

    r1361 r1362  
    55
    66namespace 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  }
    724
    825  public class Serializer : IEnumerable<ISerializationToken> {
     
    1431    private readonly Configuration configuration;
    1532
    16     public Dictionary<string, int> TypeCache {
     33    public List<TypeMapping> TypeCache {
    1734      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        }
    2148        return result;                                     
    2249      }
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs

    r1361 r1362  
    33using System.Text;
    44using HeuristicLab.Persistence.Interfaces;
     5using HeuristicLab.Persistence.Core;
    56
    67namespace HeuristicLab.Persistence.Default.Xml {
     
    108109    }
    109110
    110     public IEnumerable<string> Format(Dictionary<string, int> typeCache) {
     111    public IEnumerable<string> Format(List<TypeMapping> typeCache) {
    111112      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);                               
    116115      yield return "</" + XmlStrings.TYPECACHE + ">";
    117116    }
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlParser.cs

    r1361 r1362  
    44using System.Collections;
    55using System.IO;
     6using HeuristicLab.Persistence.Core;
    67using HeuristicLab.Persistence.Interfaces;
    78
    89namespace HeuristicLab.Persistence.Default.Xml {
    9 
    1010
    1111  public class XmlParser : IEnumerable<ISerializationToken> {
     
    9191    }
    9292
    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>();
    9595      XmlReader xmlReader = XmlReader.Create(reader);
    9696      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")));
    100102        }
    101103      }
  • branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs

    r1361 r1362  
    172172      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
    173173      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")));       
    176175      object o = deSerializer.DeSerialize(parser);     
    177176      Console.Out.WriteLine(Util.AutoFormat(o, true));     
     
    192191      XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
    193192      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")));       
    196194      object o = deSerializer.DeSerialize(parser);     
    197195      Console.Out.WriteLine(Util.AutoFormat(o, true));     
Note: See TracChangeset for help on using the changeset viewer.