Changeset 1420
- Timestamp:
- 03/25/09 17:52:02 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence/Core
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1419 r1420 6 6 7 7 struct ParentReference { } 8 delegate void Setter(object value); 8 delegate void Setter(object value); 9 9 10 11 interface IAccessibleObject { 12 object Obj { get; } 13 Setter GetSetter(string name); 14 } 15 16 17 class CustomComposite : IAccessibleObject { 10 class CompositeObject { 18 11 19 12 public object Obj { get; private set; } 20 13 public List<Tag> customValues; 21 14 22 public C ustomComposite(object obj) {15 public CompositeObject(object obj) { 23 16 Obj = obj; 24 17 customValues = new List<Tag>(); … … 29 22 } 30 23 31 public Setter GetSetter(string name) { 32 int index = customValues.Count - 1; 33 Tag t = customValues[index]; 24 public Setter GetSetter(string name) { 25 Tag t = customValues[customValues.Count - 1]; 34 26 return value => t.Value = value; 35 27 } 36 } 37 38 39 class StorableComposite : IAccessibleObject { 40 41 public object Obj { get; private set; } 42 public readonly Dictionary<string, DataMemberAccessor> accessorDict; 43 44 public StorableComposite(object obj, Dictionary<string, DataMemberAccessor> accessorDict) { 45 Obj = obj; 46 this.accessorDict = new Dictionary<string, DataMemberAccessor>(); 47 foreach (KeyValuePair<string, DataMemberAccessor> pair in accessorDict) { 48 this.accessorDict.Add( 49 pair.Value.Name, 50 pair.Value); 51 } 52 } 53 54 public void SetValue(string name, object value) { 55 accessorDict[name].Set(value); 56 accessorDict.Remove(name); 57 } 58 59 public Setter GetSetter(string name) { 60 return value => accessorDict[name].Set(value); 61 } 62 63 public void PopulateDefaultValues() { 64 foreach (var pair in accessorDict) { 65 pair.Value.Set(pair.Value.DefaultValue); 66 } 67 } 68 } 69 28 } 70 29 71 30 public class DeSerializer { … … 77 36 private readonly Dictionary<Type, object> serializerMapping; 78 37 private readonly Dictionary<Type, Handler> handlers; 79 private readonly Stack< IAccessibleObject> parentStack;38 private readonly Stack<CompositeObject> parentStack; 80 39 private readonly Dictionary<int, Type> typeIds; 81 40 private List<Thunk> finalFixes; … … 84 43 IEnumerable<TypeMapping> typeCache) { 85 44 id2obj = new Dictionary<int, object>(); 86 parentStack = new Stack< IAccessibleObject>();45 parentStack = new Stack<CompositeObject>(); 87 46 handlers = new Dictionary<Type, Handler> { 88 47 {typeof (BeginToken), CompositeStartHandler}, … … 105 64 106 65 public object DeSerialize(IEnumerable<ISerializationToken> tokens) { 107 finalFixes = new List<Thunk>(); 66 finalFixes = new List<Thunk>(); 108 67 foreach (ISerializationToken token in tokens) { 109 68 handlers[token.GetType()].Invoke(token); … … 126 85 if (instance == null) 127 86 instance = new ParentReference(); 128 parentStack.Push(new CustomComposite(instance)); 129 } else { 130 instance = Activator.CreateInstance(type, true); 131 Dictionary<string, DataMemberAccessor> accessorDict = 132 StorableAttribute.GetAutostorableAccessors(instance); 133 parentStack.Push(new StorableComposite(instance, accessorDict)); 87 parentStack.Push(new CompositeObject(instance)); 88 } else { 89 throw new ApplicationException(String.Format( 90 "No suitable method for deserialization of type \"{0}\" found.", 91 type.FullName)); 134 92 } 135 93 if ( start.Id != null ) … … 144 102 decomposer = serializerMapping[type] as IDecomposer; 145 103 if (decomposer != null) { 146 C ustomComposite customComposite = (CustomComposite)parentStack.Pop();104 CompositeObject customComposite = (CompositeObject)parentStack.Pop(); 147 105 object deserializedObject = 148 106 decomposer.Populate(customComposite.Obj, customComposite.customValues, type); … … 151 109 SetValue(end.Name, deserializedObject); 152 110 } else { 153 StorableComposite storableComposite = (StorableComposite)parentStack.Pop();154 storableComposite.PopulateDefaultValues();155 SetValue(end.Name, storableComposite.Obj);111 throw new ApplicationException(String.Format( 112 "No suitable method for deserialization of type \"{0}\" found.", 113 type.FullName)); 156 114 } 157 115 } … … 183 141 184 142 private void SetValue(string name, object value) { 185 if (parentStack.Count == 0) { 186 parentStack.Push(new StorableComposite(value, new Dictionary<string, DataMemberAccessor>())); 187 } else { 188 object accessibleObject = parentStack.Peek(); 189 if (accessibleObject is StorableComposite) { 190 ((StorableComposite)accessibleObject).SetValue(name, value); 191 } else if (accessibleObject is CustomComposite) { 192 ((CustomComposite)accessibleObject).AddValue(name, value); 193 } 143 if (parentStack.Count == 0) { 144 parentStack.Push(new CompositeObject(value)); 145 } else { 146 parentStack.Peek().AddValue(name, value); 194 147 } 195 148 } -
branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs
r1419 r1420 92 92 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 93 93 if (decomposer != null) 94 return CompositeEnumerator(accessor.Name, decomposer.DeCompose(value), id, typeId); 95 return StorableEnumerator(accessor.Name, value, id, typeId); 94 return CompositeEnumerator(accessor.Name, decomposer.DeCompose(value), id, typeId); 95 throw new ApplicationException( 96 String.Format( 97 "No suitable method for serializing values of type \"{0}\" found.", 98 value.GetType().FullName)); 96 99 } 97 100 … … 121 124 } 122 125 123 private IEnumerator<ISerializationToken> StorableEnumerator(string name,124 object value, int? id, int typeId) {125 yield return new BeginToken(name, typeId, id);126 int nSubComponents = 0;127 foreach (KeyValuePair<string, DataMemberAccessor> mapping in128 StorableAttribute.GetAutostorableAccessors(value)) {129 nSubComponents += 1;130 IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value);131 while (iterator.MoveNext()) {132 yield return iterator.Current;133 }134 }135 if (nSubComponents == 0 && ! EmptyStorableClassAttribute.IsEmpyStorable(value.GetType())) {136 throw new ApplicationException(137 String.Format(138 "Value of type \"{0}\" has no formatter or decomposer, " +139 "contains no storable subcomponents, " +140 "and is not marked as empty storable class.",141 value.GetType().FullName));142 }143 yield return new EndToken(name, typeId, id);144 }145 126 } 146 127
Note: See TracChangeset
for help on using the changeset viewer.