- Timestamp:
- 03/18/09 14:51:38 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1349 r1355 63 63 private readonly Stack<IAccessibleObject> compositeStack; 64 64 private readonly PersistenceConfiguration persistenceConfiguration; 65 private readonly Dictionary<int, Type> typeIds; 65 66 66 67 delegate void Thunk(); 67 68 private List<Thunk> finalFixes; 68 69 69 public DeSerializer() : this(PersistenceConfiguration.Instance) {} 70 public DeSerializer(IEnumerable<KeyValuePair<string, int>> typeCache) : 71 this(typeCache, PersistenceConfiguration.Instance) {} 70 72 71 public DeSerializer(PersistenceConfiguration persistenceConfiguration) { 73 public DeSerializer( 74 IEnumerable<KeyValuePair<string, int>> typeCache, 75 PersistenceConfiguration persistenceConfiguration) { 72 76 this.persistenceConfiguration = persistenceConfiguration; 73 77 id2obj = new Dictionary<int, object>(); … … 80 84 {typeof (Null), NullHandler} 81 85 }; 86 typeIds = new Dictionary<int, Type>(); 87 foreach ( var pair in typeCache ) { 88 Type type = Type.GetType(pair.Key); 89 typeIds.Add(pair.Value, type); 90 } 82 91 } 83 92 … … 96 105 CompositeStart start = (CompositeStart)token; 97 106 object instance; 98 if (persistenceConfiguration.GetDecomposer(start.Type) != null) { 107 Type type = typeIds[int.Parse(start.TypeId)]; 108 if (persistenceConfiguration.GetDecomposer(type) != null) { 99 109 instance = new ParentReference(); 100 110 compositeStack.Push(new CustomObject(instance)); 101 111 } else { 102 instance = Activator.CreateInstance( start.Type, true);112 instance = Activator.CreateInstance(type, true); 103 113 Dictionary<string, DataMemberAccessor> accessorDict = 104 114 StorableAttribute.GetAutostorableAccessors(instance); … … 109 119 } 110 120 private void CompositeEndHandler(IParseToken token) { 111 CompositeEnd end = (CompositeEnd)token; 112 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(end.Type); 121 CompositeEnd end = (CompositeEnd)token; 122 Type type = typeIds[int.Parse(end.TypeId)]; 123 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type); 113 124 if (decomposer != null) { 114 125 CustomObject customObject = (CustomObject)compositeStack.Pop(); 115 126 object deserializedObject = 116 decomposer.DeSerialize(customObject.customValues, end.Type);127 decomposer.DeSerialize(customObject.customValues, type); 117 128 if ( end.Id != null ) 118 129 id2obj[(int)end.Id] = deserializedObject; … … 126 137 private void PrimitiveHandler(IParseToken token) { 127 138 Primitive primitive = (Primitive)token; 139 Type type = typeIds[int.Parse(primitive.TypeId)]; 128 140 object value = persistenceConfiguration 129 .GetFormatter(XmlFormat.Instance, primitive.Type)141 .GetFormatter(XmlFormat.Instance, type) 130 142 .DeSerialize(primitive.SerializedValue); 131 143 if ( ! value.GetType().IsValueType ) -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1349 r1355 2 2 using System.Collections; 3 3 using System; 4 using System.Linq; 4 5 5 6 namespace Persistence { … … 12 13 private readonly Dictionary<Type, int> typeCache; 13 14 private readonly PersistenceConfiguration persistenceConfiguration; 15 16 public Dictionary<string, int> TypeCache { 17 get { 18 Dictionary<string, int> result = new Dictionary<string, int>(); 19 foreach ( var pair in typeCache ) 20 result.Add(pair.Key.AssemblyQualifiedName, pair.Value); 21 return result; 22 } 23 } 14 24 15 25 public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {} … … 35 45 IEnumerator<ISerializationToken> iterator = Serialize(rootAccessor); 36 46 while (iterator.MoveNext()) 37 yield return iterator.Current; 38 Console.WriteLine("TypeCache:"); 39 foreach (var pair in typeCache) { 40 Console.WriteLine("<TYPE id=\"{0}\" AssemblyQualifiedName=\"{1}\">", 41 pair.Value, 42 pair.Key.AssemblyQualifiedName); 43 } 47 yield return iterator.Current; 44 48 } 45 49 … … 57 61 yield break; 58 62 } 59 63 60 64 if ( ! typeCache.ContainsKey(value.GetType())) 61 65 typeCache.Add(value.GetType(), typeCache.Count); 66 int typeId = typeCache[value.GetType()]; 62 67 63 68 int? id = null; … … 70 75 if (formatter != null) { 71 76 yield return new PrimitiveToken( 72 accessor, 77 accessor.Name, 78 typeId, 73 79 formatter.Serialize(value), 74 80 id); … … 76 82 } 77 83 78 yield return new BeginToken(accessor , id);84 yield return new BeginToken(accessor.Name, typeId, id); 79 85 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(value.GetType()); 80 86 … … 85 91 yield return iterator.Current; 86 92 } 87 yield return new EndToken(accessor , id);93 yield return new EndToken(accessor.Name, typeId, id); 88 94 yield break; 89 95 } … … 103 109 value.GetType().FullName)); 104 110 } 105 yield return new EndToken(accessor , id);111 yield return new EndToken(accessor.Name, typeId, id); 106 112 } 107 113 -
branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs
r1339 r1355 6 6 public interface ISerializationToken { 7 7 } 8 public class BeginToken : ISerializationToken { 9 public readonly DataMemberAccessor Accessor; 8 public class BeginToken : ISerializationToken { 9 public readonly string Name; 10 public readonly int? TypeId; 10 11 public readonly int? Id; 11 public BeginToken(DataMemberAccessor accessor, int? id) { 12 Accessor = accessor; 12 public BeginToken(string name, int? typeId, int? id) { 13 Name = name; 14 TypeId = typeId; 13 15 Id = id; 14 16 } 15 17 } 16 18 public class EndToken : ISerializationToken { 17 public readonly DataMemberAccessor Accessor; 19 public readonly string Name; 20 public readonly int? TypeId; 18 21 public readonly int? Id; 19 public EndToken(DataMemberAccessor accessor, int? id) { 20 Accessor = accessor; 22 public EndToken(string name, int? typeId, int? id) { 23 Name = name; 24 TypeId = typeId; 21 25 Id = id; 22 26 } 23 27 } 24 28 public class PrimitiveToken : ISerializationToken { 25 public readonly DataMemberAccessor Accessor;26 public readonly object Data;29 public readonly string Name; 30 public readonly int? TypeId; 27 31 public readonly int? Id; 28 public PrimitiveToken(DataMemberAccessor accessor, object data, int? id) { 29 Accessor = accessor; 30 Data = data; 32 public readonly object SerialData; 33 public PrimitiveToken(string name, int? typeId, object serialData, int? id) { 34 Name = name; 35 TypeId = typeId; 36 SerialData = serialData; 31 37 Id = id; 32 38 } … … 54 60 public class CompositeStart : IParseToken { 55 61 public readonly string Name; 56 public readonly Type Type;62 public readonly string TypeId; 57 63 public readonly int? Id; 58 public CompositeStart(string name, Type type, int? id) {64 public CompositeStart(string name, string typeId, int? id) { 59 65 Name = name; 60 Type = type;66 TypeId = typeId; 61 67 Id = id; 62 68 } … … 64 70 public class CompositeEnd : IParseToken { 65 71 public readonly string Name; 66 public readonly Type Type;72 public readonly string TypeId; 67 73 public readonly int? Id; 68 public CompositeEnd(string name, Type type, int? id) {74 public CompositeEnd(string name, string typeId, int? id) { 69 75 Name = name; 70 Type = type;76 TypeId = typeId; 71 77 Id = id; 72 78 } … … 74 80 public class Primitive : IParseToken { 75 81 public readonly string Name; 76 public readonly Type Type;82 public readonly string TypeId; 77 83 public readonly string SerializedValue; 78 84 public readonly int? Id; 79 public Primitive(string name, Type type, string serilaizedValue, int? id) {85 public Primitive(string name, string typeId, string serilaizedValue, int? id) { 80 86 Name = name; 81 Type = type;87 TypeId = typeId; 82 88 SerializedValue = serilaizedValue; 83 89 Id = id; -
branches/New Persistence Exploration/Persistence/Persistence/XmlFormatter.cs
r1339 r1355 52 52 private string FormatBegin(ISerializationToken token) { 53 53 BeginToken beginToken = (BeginToken)token; 54 var attributes = new Dictionary<string, string> { 55 {"name", beginToken.Accessor.Name},56 {"type", beginToken.Accessor.Get().GetType().AssemblyQualifiedName } };54 var attributes = new Dictionary<string, string> {{"name", beginToken.Name}}; 55 if ( beginToken.TypeId != null ) 56 attributes.Add("typeId", beginToken.TypeId.ToString()); 57 57 if ( beginToken.Id != null ) 58 58 attributes.Add("id", beginToken.Id.ToString()); … … 72 72 Dictionary<string, string> attributes = 73 73 new Dictionary<string, string> { 74 {"type ", dataToken.Accessor.Get().GetType().AssemblyQualifiedName}};75 if ( !string.IsNullOrEmpty(dataToken. Accessor.Name) )76 attributes.Add("name", dataToken. Accessor.Name);74 {"typeId", dataToken.TypeId.ToString()}}; 75 if ( !string.IsNullOrEmpty(dataToken.Name) ) 76 attributes.Add("name", dataToken.Name); 77 77 if ( dataToken.Id != null ) 78 78 attributes.Add("id", dataToken.Id.ToString()); 79 79 return Prefix + 80 80 FormatNode("PRIMITIVE", attributes, NodeType.Start) + 81 dataToken. Data + "</PRIMITIVE>\n";81 dataToken.SerialData + "</PRIMITIVE>\n"; 82 82 } 83 83 … … 98 98 return Prefix + FormatNode("NULL", attributes, NodeType.Inline) + "\n"; 99 99 } 100 101 public IEnumerable<string> Format(Dictionary<string, int> typeCache) { 102 yield return "<TYPECACHE>"; 103 foreach ( var pair in typeCache ) { 104 yield return String.Format(" <TYPE id=\"{0}\" name=\"{1}\"/>", 105 pair.Value, pair.Key); 106 } 107 yield return "</TYPECACHE>"; 108 } 100 109 } 101 110 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1339 r1355 6 6 7 7 namespace Persistence { 8 9 8 10 public class XmlParser : IEnumerable<IParseToken> { 9 11 … … 52 54 yield return new Primitive( 53 55 reader.GetAttribute("name"), 54 Type.GetType(reader.GetAttribute("type")),56 reader.GetAttribute("typeId"), 55 57 reader.ReadString(), 56 58 id); … … 58 60 private IEnumerator<IParseToken> ParseComposite() { 59 61 string name = reader.GetAttribute("name"); 60 Type type = Type.GetType(reader.GetAttribute("type"));62 string typeId = reader.GetAttribute("typeId"); 61 63 string idString = reader.GetAttribute("id"); 62 64 int? id = null; 63 65 if (idString != null) 64 66 id = int.Parse(idString); 65 yield return new CompositeStart(name, type , id);67 yield return new CompositeStart(name, typeId, id); 66 68 IEnumerator<IParseToken> iterator = GetEnumerator(); 67 69 while (iterator.MoveNext()) 68 70 yield return iterator.Current; 69 yield return new CompositeEnd(name, type , id);71 yield return new CompositeEnd(name, typeId, id); 70 72 } 71 73 private IEnumerator<IParseToken> ParseReference() { … … 80 82 return GetEnumerator(); 81 83 } 84 public static Dictionary<string, int> ParseTypeCache(TextReader reader) { 85 Dictionary<string, int> typeCache = new Dictionary<string, int>(); 86 XmlReader xmlReader = XmlReader.Create(reader); 87 while ( xmlReader.Read() ) { 88 if (xmlReader.Name == "TYPE") { 89 typeCache.Add(xmlReader.GetAttribute("name") 90 , int.Parse(xmlReader.GetAttribute("id"))); 91 } 92 } 93 return typeCache; 94 } 82 95 } 83 96 } -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1354 r1355 23 23 } 24 24 writer.Close(); 25 writer = new StreamWriter("test-types.xml"); 26 foreach (string line in xmlFormatter.Format(s.TypeCache)) { 27 writer.WriteLine(line); 28 Console.Out.WriteLine(line); 29 } 30 writer.Close(); 31 25 32 XmlParser parser = new XmlParser(new StreamReader("test.xml")); 26 DeSerializer deSerializer = new DeSerializer( );33 DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 27 34 object o = deSerializer.DeSerialize(parser); 28 35 Root t = CloningFactory.DefaultClone(r); … … 42 49 writer.Close(); 43 50 XmlParser parser = new XmlParser(new StreamReader("test2.xml")); 44 DeSerializer deSerializer = new DeSerializer( );51 DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 45 52 object o = deSerializer.DeSerialize(parser); 46 53 Manager n = CloningFactory.DefaultClone(m); … … 120 127 121 128 public static void Main() { 122 //Test1();129 Test1(); 123 130 //Test2(); 124 131 //SpeedTest();
Note: See TracChangeset
for help on using the changeset viewer.