Free cookie consent management tool by TermsFeed Policy Generator

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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.