- Timestamp:
- 03/13/09 10:58:33 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1330 r1339 12 12 private readonly Dictionary<Type, IPrimitiveSerializer> primitiveSerializers; 13 13 private readonly List<ICompoundSerializer> compoundSerializers; 14 private readonly Dictionary<Type, int> typeCache; 14 15 15 16 public Serializer(object obj) : … … 34 35 this.compoundSerializers = new List<ICompoundSerializer>(compoundSerializers); 35 36 obj2id = new Dictionary<object, int> {{new object(), 0}}; 36 } 37 typeCache = new Dictionary<Type, int>(); 38 } 37 39 38 40 IEnumerator IEnumerable.GetEnumerator() { … … 46 48 while (iterator.MoveNext()) 47 49 yield return iterator.Current; 50 Console.WriteLine("TypeCache:f"); 51 foreach ( var pair in typeCache ) 52 Console.WriteLine(pair.Key); 48 53 } 49 54 50 55 private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) { 56 51 57 object value = accessor.Get(); 58 52 59 if (value == null) { 53 60 yield return new NullReferenceToken(accessor.Name); 54 } else if (obj2id.ContainsKey(value)) { 61 yield break; 62 } 63 64 if (obj2id.ContainsKey(value)) { 55 65 yield return new ReferenceToken(accessor.Name, obj2id[value]); 56 } else if (primitiveSerializers.ContainsKey(value.GetType())) { 57 int? id = null; 58 if ( ! value.GetType().IsValueType ) { 59 id = obj2id.Count; 60 obj2id.Add(value, (int)id); 61 } 66 yield break; 67 } 68 69 if ( ! typeCache.ContainsKey(value.GetType())) 70 typeCache.Add(value.GetType(), typeCache.Count); 71 72 int? id = null; 73 if ( ! value.GetType().IsValueType) { 74 id = obj2id.Count; 75 obj2id.Add(value, (int)id); 76 } 77 78 if (primitiveSerializers.ContainsKey(value.GetType())) { 79 62 80 yield return new PrimitiveToken( 63 81 accessor, 64 82 primitiveSerializers[value.GetType()].Serialize(value), 65 id); 66 } else { 67 int id = obj2id.Count; 68 obj2id.Add(value, id); 69 yield return new BeginToken(accessor, id); 70 ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType()); 71 if (customSerializer != null) { 72 foreach (object o in customSerializer.Serialize(value)) { 73 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 74 while (iterator.MoveNext()) 75 yield return iterator.Current; 76 } 77 } else { // composite serialization 78 int nSubComponents = 0; 79 foreach (KeyValuePair<string, DataMemberAccessor> mapping in 80 StorableAttribute.GetAutostorableAccessors(value)) { 81 nSubComponents += 1; 82 IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value); 83 while (iterator.MoveNext()) { 84 yield return iterator.Current; 85 } 86 } 87 if (nSubComponents == 0) { 88 throw new ApplicationException(String.Format( 89 "Composite value of type \"{0}\" contains no subcomponents", 90 value.GetType().FullName)); 91 } 83 id); 84 yield break; 85 } 86 87 yield return new BeginToken(accessor, id); 88 ICompoundSerializer customSerializer = FindCompoundSerializer(value.GetType()); 89 90 if (customSerializer != null) { 91 foreach (object o in customSerializer.Serialize(value)) { 92 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 93 while (iterator.MoveNext()) 94 yield return iterator.Current; 92 95 } 93 96 yield return new EndToken(accessor, id); 97 yield break; 94 98 } 99 100 int nSubComponents = 0; 101 foreach (KeyValuePair<string, DataMemberAccessor> mapping in 102 StorableAttribute.GetAutostorableAccessors(value)) { 103 nSubComponents += 1; 104 IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value); 105 while (iterator.MoveNext()) { 106 yield return iterator.Current; 107 } 108 } 109 if (nSubComponents == 0) { 110 throw new ApplicationException(String.Format( 111 "Composite value of type \"{0}\" contains no subcomponents", 112 value.GetType().FullName)); 113 } 114 yield return new EndToken(accessor, id); 95 115 } 96 116
Note: See TracChangeset
for help on using the changeset viewer.