Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1356


Ignore:
Timestamp:
03/18/09 15:15:48 (16 years ago)
Author:
epitzer
Message:

Unify token classes for parsing and formatting, make format specification mandatory. (#506)

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

Legend:

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

    r1355 r1356  
    5757    }
    5858
    59     private delegate void Handler(IParseToken token);
     59    private delegate void Handler(ISerializationToken token);
    6060
    6161    private readonly Dictionary<int, object> id2obj;
     
    6868    private List<Thunk> finalFixes;
    6969
    70     public DeSerializer(IEnumerable<KeyValuePair<string, int>> typeCache) :
    71       this(typeCache, PersistenceConfiguration.Instance) {}
    72  
    7370    public DeSerializer(
    7471      IEnumerable<KeyValuePair<string, int>> typeCache,
     
    7875      compositeStack = new Stack<IAccessibleObject>();
    7976      handlers = new Dictionary<Type, Handler> {
    80                      {typeof (CompositeStart), CompositeStartHandler},
    81                      {typeof (CompositeEnd), CompositeEndHandler},
    82                      {typeof (Primitive), PrimitiveHandler},
    83                      {typeof (Reference), ReferenceHandler},
    84                      {typeof (Null), NullHandler}
     77                     {typeof (BeginToken), CompositeStartHandler},
     78                     {typeof (EndToken), CompositeEndHandler},
     79                     {typeof (PrimitiveToken), PrimitiveHandler},
     80                     {typeof (ReferenceToken), ReferenceHandler},
     81                     {typeof (NullReferenceToken), NullHandler}
    8582                   };     
    8683      typeIds = new Dictionary<int, Type>();
     
    9188    }
    9289
    93     public object DeSerialize(IEnumerable<IParseToken> tokens) {
     90    public object DeSerialize(IEnumerable<ISerializationToken> tokens) {
    9491      finalFixes = new List<Thunk>();
    95       foreach (IParseToken token in tokens) {
     92      foreach (ISerializationToken token in tokens) {
    9693        handlers[token.GetType()].Invoke(token);
    9794      }
     
    10299    }
    103100
    104     private void CompositeStartHandler(IParseToken token) {
    105       CompositeStart start = (CompositeStart)token;
    106       object instance;
    107       Type type = typeIds[int.Parse(start.TypeId)];
     101    private void CompositeStartHandler(ISerializationToken token) {
     102      BeginToken start = (BeginToken)token;
     103      object instance;     
     104      Type type = typeIds[(int)start.TypeId];
    108105      if (persistenceConfiguration.GetDecomposer(type) != null) {
    109106        instance = new ParentReference();
     
    118115        id2obj.Add((int)start.Id, instance);
    119116    }
    120     private void CompositeEndHandler(IParseToken token) {
    121       CompositeEnd end = (CompositeEnd)token;
    122       Type type = typeIds[int.Parse(end.TypeId)];
     117    private void CompositeEndHandler(ISerializationToken token) {
     118      EndToken end = (EndToken)token;
     119      Type type = typeIds[(int)end.TypeId];
    123120      IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type);
    124121      if (decomposer != null) {
     
    134131        SetValue(end.Name, compositeObject.Obj);
    135132      }
    136     }   
    137     private void PrimitiveHandler(IParseToken token) {
    138       Primitive primitive = (Primitive)token;
    139       Type type = typeIds[int.Parse(primitive.TypeId)];
     133    }
     134    private void PrimitiveHandler(ISerializationToken token) {
     135      PrimitiveToken primitive = (PrimitiveToken)token;
     136      Type type = typeIds[(int)primitive.TypeId];
    140137      object value = persistenceConfiguration
    141138        .GetFormatter(XmlFormat.Instance, type)
    142         .DeSerialize(primitive.SerializedValue);
     139        .DeSerialize(primitive.SerialData);
    143140      if ( ! value.GetType().IsValueType )
    144141        id2obj[(int)primitive.Id] = value;
    145142      SetValue(primitive.Name, value);
    146143    }
    147     private void ReferenceHandler(IParseToken token) {
    148       Reference reference = (Reference)token;
     144    private void ReferenceHandler(ISerializationToken token) {
     145      ReferenceToken reference = (ReferenceToken)token;
    149146      object referredObject = id2obj[reference.Id];
    150147      SetValue(reference.Name, id2obj[reference.Id]);
     
    155152      }
    156153    }
    157     private void NullHandler(IParseToken token) {
    158       Null nil = (Null)token;
     154    private void NullHandler(ISerializationToken token) {
     155      NullReferenceToken nil = (NullReferenceToken)token;
    159156      SetValue(nil.Name, null);
    160157    }
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1355 r1356  
    2222      }
    2323    }
    24 
    25     public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {}
    2624
    2725    public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) :       
  • branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs

    r1355 r1356  
    22namespace Persistence {
    33
    4   #region Serialization Tokens
     4  public interface ISerializationToken {}
    55
    6   public interface ISerializationToken {
    7   }
    86  public class BeginToken : ISerializationToken {   
    97    public readonly string Name;
     
    1614    }
    1715  }
     16
    1817  public class EndToken : ISerializationToken {
    1918    public readonly string Name;
     
    2625    }
    2726  }
     27
    2828  public class PrimitiveToken : ISerializationToken {
    2929    public readonly string Name;
     
    3838    }
    3939  }
     40
    4041  public class ReferenceToken : ISerializationToken {
    4142    public readonly string Name;
     
    4647    }
    4748  }
     49
    4850  public class NullReferenceToken : ISerializationToken {
    4951    public readonly string Name;
     
    5254    }
    5355  }
    54   #endregion
    55 
    56   #region DeSerialization Tokens
    57 
    58   public interface IParseToken { }
    59 
    60   public class CompositeStart : IParseToken {
    61     public readonly string Name;
    62     public readonly string TypeId;
    63     public readonly int? Id;
    64     public CompositeStart(string name, string typeId, int? id) {
    65       Name = name;
    66       TypeId = typeId;
    67       Id = id;
    68     }
    69   }
    70   public class CompositeEnd : IParseToken {
    71     public readonly string Name;
    72     public readonly string TypeId;
    73     public readonly int? Id;
    74     public CompositeEnd(string name, string typeId, int? id) {
    75       Name = name;
    76       TypeId = typeId;
    77       Id = id;
    78     }
    79   }
    80   public class Primitive : IParseToken {
    81     public readonly string Name;
    82     public readonly string TypeId;
    83     public readonly string SerializedValue;
    84     public readonly int? Id;
    85     public Primitive(string name, string typeId, string serilaizedValue, int? id) {
    86       Name = name;
    87       TypeId = typeId;
    88       SerializedValue = serilaizedValue;
    89       Id = id;
    90     }
    91   }
    92   public class Reference : IParseToken {
    93     public readonly string Name;
    94     public readonly int Id;
    95     public Reference(string name, int id) {
    96       Name = name;
    97       Id = id;
    98     }
    99   }
    100   public class Null : IParseToken {
    101     public readonly string Name;
    102     public Null(string name) {
    103       Name = name;
    104     }
    105   }
    106   #endregion
    10756
    10857}
  • branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs

    r1355 r1356  
    88
    99
    10   public class XmlParser : IEnumerable<IParseToken> {
     10  public class XmlParser : IEnumerable<ISerializationToken> {
    1111
    1212    private readonly XmlReader reader;
    13     private delegate IEnumerator<IParseToken> Handler();
     13    private delegate IEnumerator<ISerializationToken> Handler();
    1414    private readonly Dictionary<string, Handler> handlers;
    1515
     
    2828                   };
    2929    }
    30     public IEnumerator<IParseToken> GetEnumerator() {
     30    public IEnumerator<ISerializationToken> GetEnumerator() {
    3131      while (reader.Read()) {
    3232        if (!reader.IsStartElement()) {
    3333          break;
    3434        }
    35         IEnumerator<IParseToken> iterator;
     35        IEnumerator<ISerializationToken> iterator;
    3636        try {
    3737          iterator = handlers[reader.Name].Invoke();
     
    4747      }
    4848    }
    49     private IEnumerator<IParseToken> ParsePrimitive() {
     49    private IEnumerator<ISerializationToken> ParsePrimitive() {
    5050      int? id = null;
    5151      string idString = reader.GetAttribute("id");
    5252      if (idString != null)
    5353        id = int.Parse(idString);
    54       yield return new Primitive(
     54      yield return new PrimitiveToken(
    5555        reader.GetAttribute("name"),
    56         reader.GetAttribute("typeId"),       
     56        int.Parse(reader.GetAttribute("typeId")),
    5757        reader.ReadString(),
    5858        id);
    5959    }
    60     private IEnumerator<IParseToken> ParseComposite() {
    61       string name = reader.GetAttribute("name");     
    62       string typeId = reader.GetAttribute("typeId");
     60    private IEnumerator<ISerializationToken> ParseComposite() {
     61      string name = reader.GetAttribute("name");           
    6362      string idString = reader.GetAttribute("id");
    6463      int? id = null;
     64      int? typeId = null;
    6565      if (idString != null)
    66         id = int.Parse(idString);     
    67       yield return new CompositeStart(name, typeId, id);
    68       IEnumerator<IParseToken> iterator = GetEnumerator();
     66        id = int.Parse(idString);
     67        typeId = int.Parse(reader.GetAttribute("typeId"));
     68      yield return new BeginToken(name, typeId, id);
     69      IEnumerator<ISerializationToken> iterator = GetEnumerator();
    6970      while (iterator.MoveNext())
    7071        yield return iterator.Current;
    71       yield return new CompositeEnd(name, typeId, id);
     72      yield return new EndToken(name, typeId, id);
    7273    }
    73     private IEnumerator<IParseToken> ParseReference() {
    74       yield return new Reference(
     74    private IEnumerator<ISerializationToken> ParseReference() {
     75      yield return new ReferenceToken(
    7576        reader.GetAttribute("name"),
    7677        int.Parse(reader.GetAttribute("ref")));
    7778    }
    78     private IEnumerator<IParseToken> ParseNull() {
    79       yield return new Null(reader.GetAttribute("name"));
     79    private IEnumerator<ISerializationToken> ParseNull() {
     80      yield return new NullReferenceToken(reader.GetAttribute("name"));
    8081    }
    8182    IEnumerator IEnumerable.GetEnumerator() {
  • branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs

    r1355 r1356  
    1414      r.dict.Add("two", 2);
    1515      r.dict.Add("three", 3);
    16       Serializer s = new Serializer(r);       
     16      Serializer s = new Serializer(r, PersistenceConfiguration.Instance);       
    1717      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
    1818      StreamWriter writer = new StreamWriter("test.xml");
     
    3131     
    3232      XmlParser parser = new XmlParser(new StreamReader("test.xml"));
    33       DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));       
     33      DeSerializer deSerializer = new DeSerializer(
     34        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
     35        PersistenceConfiguration.Instance);       
    3436      object o = deSerializer.DeSerialize(parser);
    3537      Root t = CloningFactory.DefaultClone(r);
     
    3941    public static void Test2() {
    4042      Manager m = new Manager();     
    41       Serializer s = new Serializer(m);
     43      Serializer s = new Serializer(m, PersistenceConfiguration.Instance);
    4244      Persistence.XmlFormatter xmlFormatter = new XmlFormatter();
    4345      StreamWriter writer = new StreamWriter("test2.xml");
     
    4951      writer.Close();
    5052      XmlParser parser = new XmlParser(new StreamReader("test2.xml"));
    51       DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml")));             
     53      DeSerializer deSerializer = new DeSerializer(
     54        XmlParser.ParseTypeCache(new StreamReader("test-types.xml")),
     55        PersistenceConfiguration.Instance);             
    5256      object o = deSerializer.DeSerialize(parser);
    5357      Manager n = CloningFactory.DefaultClone(m);
Note: See TracChangeset for help on using the changeset viewer.