Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1317


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

refactoring of serializers & update of serializer tests. (#506)

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

Legend:

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

    r1314 r1317  
    9898      CompositeStart start = (CompositeStart)token;
    9999      object instance = null;
    100       if (this.FindCustomSerializer(start.Type) != null) {
     100      if (this.FindCompoundSerializer(start.Type) != null) {
    101101        instance = new ParentReference();
    102102        compositeStack.Push(new CustomObject(instance));
     
    112112    private void CompositeEndHandler(IParseToken token) {
    113113      CompositeEnd end = (CompositeEnd)token;
    114       ICompoundSerializer customSerializer = this.FindCustomSerializer(end.Type);
     114      ICompoundSerializer customSerializer = this.FindCompoundSerializer(end.Type);
    115115      if (customSerializer != null) {
    116116        CustomObject customObject = (CustomObject)compositeStack.Pop();
     
    124124      }
    125125    }
    126     private ICompoundSerializer FindCustomSerializer(Type type) {
     126    private ICompoundSerializer FindCompoundSerializer(Type type) {
    127127      foreach (ICompoundSerializer serializer in customSerializers) {
    128128        if (serializer.CanSerialize(type))
  • branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs

    r1314 r1317  
    2121    [Storable]
    2222    public List<Root> selfReferences;
     23    [Storable]
     24    public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } };
     25    [Storable]
     26    public bool boolean = true;
     27    [Storable]
     28    public DateTime dateTime = new DateTime();
     29    [Storable]
     30    public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123);
     31    [Storable]
     32    public Dictionary<string, int> dict = new Dictionary<string, int>();
    2333  }
    2434
     
    4050      r.c = new Custom();
    4151      r.c.r = r;
    42       IPrimitiveSerializer[] serializers = {
     52      r.dict.Add("one", 1);
     53      r.dict.Add("two", 2);
     54      r.dict.Add("three", 3);
     55
     56      Serializer s = new Serializer(r,
     57        new IPrimitiveSerializer[] {
    4358          new String2XMLSerializer(),
    4459          new Int2XMLSerializer(),
    45           new Double2XmlSerializer()};
    46       Serializer s = new Serializer(r, serializers);
     60          new Double2XmlSerializer(),
     61          new Boolean2XmlSerializer(),
     62          new DateTime2XmlSerializer(),
     63        }, new ICompoundSerializer[] {
     64          new EnumerableSerializer(),
     65          new ArraySerializer(),
     66          new KeyValuePair2XmlSerializer(),
     67          new Dictionary2XmlSerializer(),
     68        });     
    4769      Persistence.XmlFormatter xmlFormatter = new Persistence.XmlFormatter();
    4870      StreamWriter writer = new StreamWriter("test.xml");
     
    5981          new String2XMLSerializer(),
    6082          new Double2XmlSerializer(),
     83          new Boolean2XmlSerializer(),
     84          new DateTime2XmlSerializer(),
    6185        },
    6286        new ICompoundSerializer[] {
    6387          new ArraySerializer(),
    64           new EnumerableSerializer() });
     88          new EnumerableSerializer(),
     89          new KeyValuePair2XmlSerializer(),
     90          new Dictionary2XmlSerializer(),
     91        });     
    6592      object o = deSerializer.DeSerialize(parser);
    6693      Console.Out.WriteLine(Util.AutoFormat(o, true));
  • branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs

    r1314 r1317  
    1111    private Dictionary<object, int> obj2id;
    1212    private Dictionary<Type, IPrimitiveSerializer> primitiveSerializers;
    13     private List<ICompoundSerializer> customSerializers;
     13    private List<ICompoundSerializer> compoundSerializers;
    1414
    15     public Serializer(object obj, IEnumerable<IPrimitiveSerializer> primitiveSerializers) :
    16       this(obj, primitiveSerializers, "ROOT") { }
     15    public Serializer(object obj,
     16        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
     17        IEnumerable<ICompoundSerializer> compoundSerializers) :
     18      this(obj, primitiveSerializers, compoundSerializers, "ROOT") { }
    1719
    18     public Serializer(object obj, IEnumerable<IPrimitiveSerializer> primitiveSerializers, string rootName) {
     20    public Serializer(object obj,
     21        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
     22        IEnumerable<ICompoundSerializer> compoundSerializers, string rootName) {
    1923      this.obj = obj;
    2024      this.rootName = rootName;
     
    2327        this.primitiveSerializers.Add(serializer.Type, serializer);
    2428      }
    25       this.customSerializers = new List<ICompoundSerializer>();
    26       customSerializers.Add(new EnumerableSerializer());
    27       customSerializers.Add(new ArraySerializer());
     29      this.compoundSerializers = new List<ICompoundSerializer>(compoundSerializers);
    2830      this.obj2id = new Dictionary<object, int>();
    2931      obj2id.Add(new object(), 0);
     
    5456        this.obj2id.Add(value, id);
    5557        yield return new BeginToken(accessor, id);
    56         ICompoundSerializer customSerializer = this.FindCustomSerializer(value.GetType());
     58        ICompoundSerializer customSerializer = this.FindCompoundSerializer(value.GetType());
    5759        if (customSerializer != null) {
    5860          foreach (object obj in customSerializer.Serialize(value)) {
     
    6264          }
    6365        } else { // composite serialization
     66          int nSubComponents = 0;
    6467          foreach (KeyValuePair<string, DataMemberAccessor> mapping in
    6568            StorableAttribute.GetAutostorableAccessors(value)) {
    66             IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value);
    67             while (iterator.MoveNext())
     69            nSubComponents += 1;
     70            IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value);                       
     71            while (iterator.MoveNext()) {                           
    6872              yield return iterator.Current;
     73            }                       
     74          }
     75          if (nSubComponents == 0) {
     76            throw new ApplicationException(String.Format(
     77              "Composite value of type \"{0}\" contains no subcomponents",
     78              value.GetType().FullName));
    6979          }
    7080        }
     
    7383    }
    7484
    75     private ICompoundSerializer FindCustomSerializer(Type type) {
    76       foreach (ICompoundSerializer s in customSerializers) {
     85    private ICompoundSerializer FindCompoundSerializer(Type type) {
     86      foreach (ICompoundSerializer s in compoundSerializers) {
    7787        if (s.CanSerialize(type))
    7888          return s;
Note: See TracChangeset for help on using the changeset viewer.