Changeset 1349
- Timestamp:
- 03/16/09 17:46:28 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1348 r1349 1 1 using System.Collections.Generic; 2 2 using System; 3 using System.Reflection;4 using System.Text;5 3 6 4 namespace Persistence { … … 64 62 private readonly Dictionary<Type, Handler> handlers; 65 63 private readonly Stack<IAccessibleObject> compositeStack; 66 67 private readonly Dictionary<Type, IFormatter> primitiveSerializers; 68 private readonly List<IDecomposer> customSerializers; 64 private readonly PersistenceConfiguration persistenceConfiguration; 69 65 70 66 delegate void Thunk(); 71 67 private List<Thunk> finalFixes; 72 68 73 public DeSerializer() : this( 74 InterfaceInstantiatior.InstantiateAll<IFormatter>(), 75 InterfaceInstantiatior.InstantiateAll<IDecomposer>()) {} 76 77 public DeSerializer( 78 IEnumerable<IFormatter> primitiveSerializers, 79 IEnumerable<IDecomposer> customSerializers) { 69 public DeSerializer() : this(PersistenceConfiguration.Instance) {} 70 71 public DeSerializer(PersistenceConfiguration persistenceConfiguration) { 72 this.persistenceConfiguration = persistenceConfiguration; 80 73 id2obj = new Dictionary<int, object>(); 81 74 compositeStack = new Stack<IAccessibleObject>(); … … 86 79 {typeof (Reference), ReferenceHandler}, 87 80 {typeof (Null), NullHandler} 88 }; 89 this.primitiveSerializers = new Dictionary<Type, IFormatter>(); 90 foreach (IFormatter ps in primitiveSerializers) { 91 this.primitiveSerializers.Add(ps.Type, ps); 92 } 93 this.customSerializers = new List<IDecomposer>(customSerializers); 81 }; 94 82 } 95 83 … … 108 96 CompositeStart start = (CompositeStart)token; 109 97 object instance; 110 if ( FindCompoundSerializer(start.Type) != null) {98 if (persistenceConfiguration.GetDecomposer(start.Type) != null) { 111 99 instance = new ParentReference(); 112 100 compositeStack.Push(new CustomObject(instance)); … … 121 109 } 122 110 private void CompositeEndHandler(IParseToken token) { 123 CompositeEnd end = (CompositeEnd)token; 124 IDecomposer decomposer = FindCompoundSerializer(end.Type);111 CompositeEnd end = (CompositeEnd)token; 112 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(end.Type); 125 113 if (decomposer != null) { 126 114 CustomObject customObject = (CustomObject)compositeStack.Pop(); … … 135 123 SetValue(end.Name, compositeObject.Obj); 136 124 } 137 } 138 private IDecomposer FindCompoundSerializer(Type type) { 139 foreach (IDecomposer serializer in customSerializers) { 140 if (serializer.CanSerialize(type)) 141 return serializer; 142 } 143 return null; 144 } 125 } 145 126 private void PrimitiveHandler(IParseToken token) { 146 127 Primitive primitive = (Primitive)token; 147 object value = primitiveSerializers[primitive.Type].DeSerialize(primitive.SerializedValue); 128 object value = persistenceConfiguration 129 .GetFormatter(XmlFormat.Instance, primitive.Type) 130 .DeSerialize(primitive.SerializedValue); 148 131 if ( ! value.GetType().IsValueType ) 149 132 id2obj[(int)primitive.Id] = value; -
branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj
r1348 r1349 54 54 <ItemGroup> 55 55 <Compile Include="DeSerializer.cs" /> 56 <Compile Include="PersistenceConfiguration.cs" /> 56 57 <Compile Include="Properties\AssemblyInfo.cs" /> 57 58 <Compile Include="Serializer.cs" /> -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1348 r1349 10 10 private readonly string rootName; 11 11 private readonly Dictionary<object, int> obj2id; 12 private readonly Dictionary<Type, IFormatter> primitiveSerializers;13 private readonly List<IDecomposer> compoundSerializers;14 12 private readonly Dictionary<Type, int> typeCache; 13 private readonly PersistenceConfiguration persistenceConfiguration; 15 14 16 public Serializer(object obj) : 17 this(obj, 18 InterfaceInstantiatior.InstantiateAll<IFormatter>(), 19 InterfaceInstantiatior.InstantiateAll<IDecomposer>()) {} 15 public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {} 20 16 21 public Serializer(object obj, 22 IEnumerable<IFormatter> primitiveSerializers, 23 IEnumerable<IDecomposer> compoundSerializers) : 24 this(obj, primitiveSerializers, compoundSerializers, "ROOT") { } 17 public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) : 18 this(obj, persistenceConfiguration, "ROOT") { } 25 19 26 public Serializer(object obj, 27 IEnumerable<IFormatter> primitiveSerializers, 28 IEnumerable<IDecomposer> compoundSerializers, string rootName) { 20 public Serializer(object obj, PersistenceConfiguration persistenceConfiguration, string rootName) { 29 21 this.obj = obj; 30 22 this.rootName = rootName; 31 this.primitiveSerializers = new Dictionary<Type, IFormatter>(); 32 foreach (IFormatter serializer in primitiveSerializers) { 33 this.primitiveSerializers.Add(serializer.Type, serializer); 34 } 35 this.compoundSerializers = new List<IDecomposer>(compoundSerializers); 23 this.persistenceConfiguration = persistenceConfiguration; 36 24 obj2id = new Dictionary<object, int> {{new object(), 0}}; 37 25 typeCache = new Dictionary<Type, int>(); … … 79 67 } 80 68 81 if (primitiveSerializers.ContainsKey(value.GetType())) {82 69 IFormatter formatter = persistenceConfiguration.GetFormatter(XmlFormat.Instance, value.GetType()); 70 if (formatter != null) { 83 71 yield return new PrimitiveToken( 84 72 accessor, 85 primitiveSerializers[value.GetType()].Serialize(value),73 formatter.Serialize(value), 86 74 id); 87 75 yield break; … … 89 77 90 78 yield return new BeginToken(accessor, id); 91 IDecomposer custom = FindCompoundSerializer(value.GetType());79 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType()); 92 80 93 if ( custom!= null) {94 foreach (object o in custom.Serialize(value)) {81 if (decomposer != null) { 82 foreach (object o in decomposer.Serialize(value)) { 95 83 IEnumerator<ISerializationToken> iterator = Serialize(new DataMemberAccessor(o)); 96 84 while (iterator.MoveNext()) … … 117 105 yield return new EndToken(accessor, id); 118 106 } 119 120 private IDecomposer FindCompoundSerializer(Type type) { 121 foreach (IDecomposer s in compoundSerializers) { 122 if (s.CanSerialize(type)) 123 return s; 124 } 125 return null; 126 } 107 127 108 } 128 109 }
Note: See TracChangeset
for help on using the changeset viewer.