Changeset 1437


Ignore:
Timestamp:
03/27/09 12:50:34 (12 years ago)
Author:
epitzer
Message:

Cleanup DeSerializer code. (#506)

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

Legend:

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

    r1435 r1437  
    1717    }
    1818
    19     public void AddValue(string name, object value, List<DeSerializer.Thunk> finalFixes) {
     19    public void AddValue(string name, object value, List<Thunk> finalFixes) {
    2020      Tag t = new Tag(name, value);
    2121      t.finalFixes = finalFixes;
     
    2323    }
    2424
    25     public Setter GetSetter(string name) {           
     25    public Setter GetSetterForLastAddedValue(string name) {           
    2626      Tag t = customValues[customValues.Count - 1];     
    2727      return value => t.Value = value;
    2828    }
    29   } 
     29  }
     30
     31  public delegate void Thunk();
    3032
    3133  public class DeSerializer {
    32 
    33     private delegate void Handler(ISerializationToken token);
    34     public delegate void Thunk();
    35 
     34   
    3635    private readonly Dictionary<int, object> id2obj;
    37     private readonly Dictionary<Type, object> serializerMapping;
    38     private readonly Dictionary<Type, Handler> handlers;
     36    private readonly Dictionary<Type, object> serializerMapping;   
    3937    private readonly Stack<CompositeObject> parentStack;   
    4038    private readonly Dictionary<int, Type> typeIds;   
     
    4543      id2obj = new Dictionary<int, object>();
    4644      parentStack = new Stack<CompositeObject>();
    47       handlers = new Dictionary<Type, Handler> {
    48                      {typeof (BeginToken), CompositeStartHandler},
    49                      {typeof (EndToken), CompositeEndHandler},
    50                      {typeof (PrimitiveToken), PrimitiveHandler},
    51                      {typeof (ReferenceToken), ReferenceHandler},
    52                      {typeof (NullReferenceToken), NullHandler}
    53                    };     
    5445      typeIds = new Dictionary<int, Type>();
    55       serializerMapping = new Dictionary<Type, object>();
    56       foreach ( var typeMapping in typeCache ) {
     46      serializerMapping = createSerializers(typeCache);
     47    }
     48
     49    private Dictionary<Type, object> createSerializers(IEnumerable<TypeMapping> typeCache) {
     50      var serializerMapping = new Dictionary<Type, object>();
     51      foreach (var typeMapping in typeCache) {
    5752        Type type = Type.GetType(typeMapping.TypeName);
    5853        typeIds.Add(typeMapping.Id, type);
     
    6257        }
    6358      }
     59      return serializerMapping;
    6460    }
    6561
     
    6763      finalFixes = new List<Thunk>();     
    6864      foreach (ISerializationToken token in tokens) {
    69         handlers[token.GetType()].Invoke(token);
     65        Type t = token.GetType();
     66        if ( t == typeof(BeginToken) ) {
     67          CompositeStartHandler((BeginToken)token);
     68        } else if ( t == typeof(EndToken) ) {
     69          CompositeEndHandler((EndToken) token);
     70        } else if ( t == typeof(PrimitiveToken) ) {
     71          PrimitiveHandler((PrimitiveToken) token);
     72        } else if ( t == typeof(ReferenceToken) ) {
     73          ReferenceHandler((ReferenceToken) token);
     74        } else if (t == typeof(NullReferenceToken)) {
     75          NullHandler((NullReferenceToken)token);
     76        } else {
     77          throw new ApplicationException("invalid token type");
     78        }
    7079      }
    7180      foreach (Thunk fix in finalFixes) {
     
    7584    }
    7685
    77     private void CompositeStartHandler(ISerializationToken token) {
    78       BeginToken start = (BeginToken)token;
    79       object instance;     
    80       Type type = typeIds[(int)start.TypeId];
     86    private void CompositeStartHandler(BeginToken token) {     
     87      Type type = typeIds[(int)token.TypeId];
    8188      IDecomposer decomposer = null;
    8289      if ( serializerMapping.ContainsKey(type) )
    8390        decomposer = serializerMapping[type] as IDecomposer;     
    84       if (decomposer != null) {       
    85         instance = decomposer.CreateInstance(type);
    86         if (instance == null)
    87           instance = new ParentReference();
    88         parentStack.Push(new CompositeObject(instance));       
    89       } else {
     91      if (decomposer == null)
    9092        throw new ApplicationException(String.Format(
    9193          "No suitable method for deserialization of type \"{0}\" found.",
    9294          type.VersionInvariantName()));
    93       }
    94       if ( start.Id != null )
    95         id2obj.Add((int)start.Id, instance);
     95      object instance = decomposer.CreateInstance(type);
     96      if (instance == null)
     97        instance = new ParentReference();
     98      parentStack.Push(new CompositeObject(instance));             
     99      if ( token.Id != null )
     100        id2obj.Add((int)token.Id, instance);
    96101    }
    97102
    98     private void CompositeEndHandler(ISerializationToken token) {
    99       EndToken end = (EndToken)token;
    100       Type type = typeIds[(int)end.TypeId];
     103    private void CompositeEndHandler(EndToken token) {     
     104      Type type = typeIds[(int)token.TypeId];
    101105      IDecomposer decomposer = null;
    102106      if (serializerMapping.ContainsKey(type))
    103107        decomposer = serializerMapping[type] as IDecomposer;           
    104       if (decomposer != null) {
    105         CompositeObject customComposite = (CompositeObject)parentStack.Pop();
    106         object deserializedObject =         
    107           decomposer.Populate(customComposite.Obj, customComposite.customValues, type);
    108         if ( end.Id != null )
    109           id2obj[(int)end.Id] = deserializedObject;       
    110         SetValue(end.Name, deserializedObject);         
    111       } else {
     108      if (decomposer == null)
    112109        throw new ApplicationException(String.Format(
    113110          "No suitable method for deserialization of type \"{0}\" found.",
    114111          type.VersionInvariantName()));
    115       }
     112      CompositeObject customComposite = (CompositeObject)parentStack.Pop();
     113      object deserializedObject =         
     114        decomposer.Populate(customComposite.Obj, customComposite.customValues, type);
     115      if ( token.Id != null )
     116        id2obj[(int)token.Id] = deserializedObject;       
     117      SetValue(token.Name, deserializedObject);         
    116118    }
    117119
    118     private void PrimitiveHandler(ISerializationToken token) {
    119       PrimitiveToken primitive = (PrimitiveToken)token;
    120       Type type = typeIds[(int)primitive.TypeId];
    121       object value = ((IFormatter) serializerMapping[type]).Parse(primitive.SerialData);
    122       if ( primitive.Id != null )     
    123         id2obj[(int)primitive.Id] = value;
    124       SetValue(primitive.Name, value);
     120    private void PrimitiveHandler(PrimitiveToken token) {     
     121      Type type = typeIds[(int)token.TypeId];
     122      object value = ((IFormatter) serializerMapping[type]).Parse(token.SerialData);
     123      if ( token.Id != null )     
     124        id2obj[(int)token.Id] = value;
     125      SetValue(token.Name, value);
    125126    }
    126127
    127     private void ReferenceHandler(ISerializationToken token) {
    128       ReferenceToken reference = (ReferenceToken)token;
    129       object referredObject = id2obj[reference.Id];
    130       SetValue(reference.Name, id2obj[reference.Id]);
     128    private void ReferenceHandler(ReferenceToken token) {     
     129      object referredObject = id2obj[token.Id];
     130      SetValue(token.Name, referredObject);     
    131131      if (referredObject is ParentReference) {
    132         Setter set = parentStack.Peek().GetSetter(reference.Name);       
    133         int id = reference.Id;
    134         finalFixes.Add(() => set(id2obj[id]));
     132        Setter set = parentStack.Peek().GetSetterForLastAddedValue(token.Name);               
     133        finalFixes.Add(() => set(id2obj[token.Id]));
    135134      }
    136135    }
    137136
    138     private void NullHandler(ISerializationToken token) {
    139       NullReferenceToken nullToken = (NullReferenceToken)token;
    140       SetValue(nullToken.Name, null);
     137    private void NullHandler(NullReferenceToken token) {     
     138      SetValue(token.Name, null);
    141139    }   
    142140
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/Compact/NumberArray2XmlFormatters.cs

    r1361 r1437  
    2222        sb.Append(a.GetLength(i));
    2323      }
     24      for (int i = 0; i < a.Rank; i++) {
     25        sb.Append(Separator);
     26        sb.Append(a.GetLowerBound(i));
     27      }
    2428      foreach (object o in a) {
    2529        sb.Append(Separator);
     
    4145        lengths[i] = int.Parse((string)values.Current);
    4246      }
    43       Array a = Array.CreateInstance(this.Type.GetElementType(), lengths);
     47      int[] lowerBounds = new int[rank];
     48      for (int i = 0; i < rank; i++) {
     49        values.MoveNext();
     50        lowerBounds[i] = int.Parse((string)values.Current);
     51      }
     52      Array a = Array.CreateInstance(this.Type.GetElementType(), lengths, lowerBounds);
    4453      int[] positions = new int[rank];
    4554      while (values.MoveNext()) {
  • branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs

    r1428 r1437  
    139139
    140140    public static void Serialize(object o, string basename, Configuration configuration) {
    141     Serializer s = new Serializer(o, configuration);
     141      Serializer s = new Serializer(o, configuration);
    142142      XmlGenerator xmlGenerator = new XmlGenerator();
    143143      StreamWriter writer = new StreamWriter(basename + ".xml");
  • branches/New Persistence Exploration/Persistence/Persistence/Interfaces/IDecomposer.cs

    r1435 r1437  
    77
    88  public class Tag {
    9     public List<DeSerializer.Thunk> finalFixes;
     9    public List<Thunk> finalFixes;
    1010    public string Name { get; private set; }
    1111    public object Value;     
Note: See TracChangeset for help on using the changeset viewer.