Changeset 1339 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/13/09 10:58:33 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1336 r1339 107 107 if (FindCompoundSerializer(start.Type) != null) { 108 108 instance = new ParentReference(); 109 compositeStack.Push(new CustomObject(instance)); 110 id2obj.Add(start.Id, instance); 109 compositeStack.Push(new CustomObject(instance)); 111 110 } else { 112 111 instance = Activator.CreateInstance(start.Type, true); 113 112 Dictionary<string, DataMemberAccessor> accessorDict = 114 113 StorableAttribute.GetAutostorableAccessors(instance); 115 compositeStack.Push(new CompositeObject(instance, accessorDict)); 116 id2obj.Add(start.Id, instance); 114 compositeStack.Push(new CompositeObject(instance, accessorDict)); 117 115 } 116 if ( start.Id != null ) 117 id2obj.Add((int)start.Id, instance); 118 118 } 119 119 private void CompositeEndHandler(IParseToken token) { … … 124 124 object deserializedObject = 125 125 compoundSerializer.DeSerialize(customObject.customValues, end.Type); 126 id2obj[end.Id] = deserializedObject; 126 if ( end.Id != null ) 127 id2obj[(int)end.Id] = deserializedObject; 127 128 SetValue(end.Name, deserializedObject); 128 129 } else { -
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 -
branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs
r1330 r1339 8 8 public class BeginToken : ISerializationToken { 9 9 public readonly DataMemberAccessor Accessor; 10 public readonly int Id;11 public BeginToken(DataMemberAccessor accessor, int id) {10 public readonly int? Id; 11 public BeginToken(DataMemberAccessor accessor, int? id) { 12 12 Accessor = accessor; 13 13 Id = id; … … 16 16 public class EndToken : ISerializationToken { 17 17 public readonly DataMemberAccessor Accessor; 18 public readonly int Id;19 public EndToken(DataMemberAccessor accessor, int id) {18 public readonly int? Id; 19 public EndToken(DataMemberAccessor accessor, int? id) { 20 20 Accessor = accessor; 21 21 Id = id; … … 55 55 public readonly string Name; 56 56 public readonly Type Type; 57 public readonly int Id;58 public CompositeStart(string name, Type type, int id) {57 public readonly int? Id; 58 public CompositeStart(string name, Type type, int? id) { 59 59 Name = name; 60 60 Type = type; … … 65 65 public readonly string Name; 66 66 public readonly Type Type; 67 public readonly int Id;68 public CompositeEnd(string name, Type type, int id) {67 public readonly int? Id; 68 public CompositeEnd(string name, Type type, int? id) { 69 69 Name = name; 70 70 Type = type; -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1338 r1339 1 1 using System.Collections.Generic; 2 2 using System; 3 using System.Xml;4 3 using System.Text; 5 4 namespace Persistence { … … 53 52 private string FormatBegin(ISerializationToken token) { 54 53 BeginToken beginToken = (BeginToken)token; 54 var attributes = new Dictionary<string, string> { 55 {"name", beginToken.Accessor.Name}, 56 {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName } }; 57 if ( beginToken.Id != null ) 58 attributes.Add("id", beginToken.Id.ToString()); 55 59 string result = Prefix + 56 FormatNode("COMPOSITE", 57 new Dictionary<string, string> 58 { 59 {"name", beginToken.Accessor.Name}, 60 {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName}, 61 {"id", beginToken.Id.ToString()} 62 }, NodeType.Start) + "\n"; 60 FormatNode("COMPOSITE", attributes, NodeType.Start) + "\n"; 63 61 depth += 1; 64 62 return result; -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1338 r1339 59 59 string name = reader.GetAttribute("name"); 60 60 Type type = Type.GetType(reader.GetAttribute("type")); 61 int id = int.Parse(reader.GetAttribute("id")); 61 string idString = reader.GetAttribute("id"); 62 int? id = null; 63 if (idString != null) 64 id = int.Parse(idString); 62 65 yield return new CompositeStart(name, type, id); 63 66 IEnumerator<IParseToken> iterator = GetEnumerator();
Note: See TracChangeset
for help on using the changeset viewer.