Changeset 1281
- Timestamp:
- 03/06/09 18:14:23 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1280 r1281 4 4 public class DeSerializer { 5 5 6 class ParentReference { 7 } 8 9 delegate void Setter(object value); 10 6 11 interface IAccessibleObject { 7 object Obj { get; } 12 object Obj { get; } 13 Setter GetSetter(string name); 8 14 } 9 15 … … 18 24 public void AddValue(object value) { 19 25 customValues.Add(value); 26 } 27 public Setter GetSetter(string name) { 28 int index = customValues.Count-1; 29 return (value) => customValues[index] = value; 20 30 } 21 31 } … … 40 50 throw new NotImplementedException(); 41 51 } 52 public Setter GetSetter(string name) { 53 return (value) => accessorDict[name].Set(value); 54 } 42 55 } 43 56 … … 51 64 private List<ICustomSerializer> customSerializers; 52 65 66 delegate void Thunk(); 67 private List<Thunk> finalFixes; 68 53 69 public DeSerializer( 54 70 IEnumerable<IPrimitiveSerializer> primitiveSerializers, … … 61 77 handlers.Add(typeof(Primitive), new Handler(PrimitiveHandler)); 62 78 handlers.Add(typeof(Reference), new Handler(ReferenceHandler)); 63 handlers.Add(typeof(Null), new Handler(NullHandler)); 64 // TODO: make this configurable 79 handlers.Add(typeof(Null), new Handler(NullHandler)); 65 80 this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>(); 66 81 foreach (IPrimitiveSerializer ps in primitiveSerializers) { … … 68 83 } 69 84 this.customSerializers = new List<ICustomSerializer>(customSerializers); 85 this.finalFixes = new List<Thunk>(); 86 } 70 87 71 }72 88 public object DeSerialize(IEnumerable<IParseToken> tokens) { 73 89 foreach (IParseToken token in tokens) { 74 90 handlers[token.GetType()].Invoke(token); 91 } 92 foreach (Thunk fix in this.finalFixes) { 93 fix(); 75 94 } 76 95 return compositeStack.Pop().Obj; … … 80 99 object instance = null; 81 100 if (this.FindCustomSerializer(start.Type) != null) { 82 instance = new object();101 instance = new ParentReference(); 83 102 compositeStack.Push(new CustomObject(instance)); 84 id2obj.Add(start.Id, instance); 85 // TODO: add warning proxy 103 id2obj.Add(start.Id, instance); 86 104 } else { 87 105 instance = Activator.CreateInstance(start.Type); … … 97 115 if (customSerializer != null) { 98 116 CustomObject customObject = (CustomObject)compositeStack.Pop(); 99 this.SetValue(end.Name, 100 customSerializer.DeSerialize(customObject.customValues, end.Type)); 117 object deserializedObject = 118 customSerializer.DeSerialize(customObject.customValues, end.Type); 119 this.id2obj[end.Id] = deserializedObject; 120 this.SetValue(end.Name, deserializedObject); 101 121 } else { 102 122 CompositeObject compositeObject = (CompositeObject)compositeStack.Pop(); … … 118 138 private void ReferenceHandler(IParseToken token) { 119 139 Reference reference = (Reference)token; 140 object referredObject = this.id2obj[reference.Id]; 120 141 this.SetValue(reference.Name, this.id2obj[reference.Id]); 142 if (referredObject is ParentReference) { 143 Setter set = compositeStack.Peek().GetSetter(reference.Name); 144 int id = reference.Id; 145 this.finalFixes.Add(() => set(id2obj[id])); 146 } 121 147 } 122 148 private void NullHandler(IParseToken token) { -
branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs
r1278 r1281 18 18 public List<int> intList = new List<int>(new int[] { 321, 312, 321 }); 19 19 [Storable] 20 public Custom c; 20 public Custom c; 21 [Storable] 22 public List<Root> selfReferences; 21 23 22 24 } … … 34 36 public static void Main() { 35 37 Root r = new Root(); 38 r.selfReferences = new List<Root>(); 39 r.selfReferences.Add(r); 40 r.selfReferences.Add(r); 36 41 r.c = new Custom(); 37 42 r.c.r = r; -
branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj
r1280 r1281 58 58 <Compile Include="StorableAttribute.cs" /> 59 59 <Compile Include="NewSerializationTest.cs" /> 60 <Compile Include="PersistenceManager.cs" />61 60 <Compile Include="SerializationTest.cs" /> 62 61 <Compile Include="XmlFormatter.cs" />
Note: See TracChangeset
for help on using the changeset viewer.