Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1281


Ignore:
Timestamp:
03/06/09 18:14:23 (16 years ago)
Author:
epitzer
Message:

Proper deserialization of parent references. (#506)

Location:
branches/New Persistence Exploration/Persistence/Persistence
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs

    r1280 r1281  
    44  public class DeSerializer {
    55
     6    class ParentReference {     
     7    }
     8
     9    delegate void Setter(object value);
     10
    611    interface IAccessibleObject {
    7       object Obj { get; }
     12      object Obj { get; }     
     13      Setter GetSetter(string name);
    814    }
    915
     
    1824      public void AddValue(object value) {
    1925        customValues.Add(value);
     26      }
     27      public Setter GetSetter(string name) {
     28        int index = customValues.Count-1;
     29        return (value) => customValues[index] = value;
    2030      }
    2131    }
     
    4050        throw new NotImplementedException();
    4151      }
     52      public Setter GetSetter(string name) {
     53        return (value) => accessorDict[name].Set(value);
     54      }
    4255    }
    4356
     
    5164    private List<ICustomSerializer> customSerializers;
    5265
     66    delegate void Thunk();
     67    private List<Thunk> finalFixes;
     68
    5369    public DeSerializer(
    5470        IEnumerable<IPrimitiveSerializer> primitiveSerializers,
     
    6177      handlers.Add(typeof(Primitive), new Handler(PrimitiveHandler));
    6278      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));     
    6580      this.primitiveSerializers = new Dictionary<Type, IPrimitiveSerializer>();
    6681      foreach (IPrimitiveSerializer ps in primitiveSerializers) {
     
    6883      }
    6984      this.customSerializers = new List<ICustomSerializer>(customSerializers);
     85      this.finalFixes = new List<Thunk>();
     86    }
    7087
    71     }
    7288    public object DeSerialize(IEnumerable<IParseToken> tokens) {
    7389      foreach (IParseToken token in tokens) {
    7490        handlers[token.GetType()].Invoke(token);
     91      }
     92      foreach (Thunk fix in this.finalFixes) {
     93        fix();
    7594      }
    7695      return compositeStack.Pop().Obj;
     
    8099      object instance = null;
    81100      if (this.FindCustomSerializer(start.Type) != null) {
    82         instance = new object();
     101        instance = new ParentReference();
    83102        compositeStack.Push(new CustomObject(instance));
    84         id2obj.Add(start.Id, instance);
    85         // TODO: add warning proxy
     103        id2obj.Add(start.Id, instance);       
    86104      } else {
    87105        instance = Activator.CreateInstance(start.Type);
     
    97115      if (customSerializer != null) {
    98116        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);         
    101121      } else {
    102122        CompositeObject compositeObject = (CompositeObject)compositeStack.Pop();
     
    118138    private void ReferenceHandler(IParseToken token) {
    119139      Reference reference = (Reference)token;
     140      object referredObject = this.id2obj[reference.Id];
    120141      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      }
    121147    }
    122148    private void NullHandler(IParseToken token) {
  • branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs

    r1278 r1281  
    1818    public List<int> intList = new List<int>(new int[] { 321, 312, 321 });
    1919    [Storable]
    20     public Custom c;
     20    public Custom c;
     21    [Storable]
     22    public List<Root> selfReferences;
    2123
    2224  }
     
    3436    public static void Main() {
    3537      Root r = new Root();
     38      r.selfReferences = new List<Root>();
     39      r.selfReferences.Add(r);
     40      r.selfReferences.Add(r);
    3641      r.c = new Custom();
    3742      r.c.r = r;
  • branches/New Persistence Exploration/Persistence/Persistence/Persistence.csproj

    r1280 r1281  
    5858    <Compile Include="StorableAttribute.cs" />
    5959    <Compile Include="NewSerializationTest.cs" />
    60     <Compile Include="PersistenceManager.cs" />
    6160    <Compile Include="SerializationTest.cs" />
    6261    <Compile Include="XmlFormatter.cs" />
Note: See TracChangeset for help on using the changeset viewer.