Changeset 1356
- Timestamp:
- 03/18/09 15:15:48 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1355 r1356 57 57 } 58 58 59 private delegate void Handler(I ParseToken token);59 private delegate void Handler(ISerializationToken token); 60 60 61 61 private readonly Dictionary<int, object> id2obj; … … 68 68 private List<Thunk> finalFixes; 69 69 70 public DeSerializer(IEnumerable<KeyValuePair<string, int>> typeCache) :71 this(typeCache, PersistenceConfiguration.Instance) {}72 73 70 public DeSerializer( 74 71 IEnumerable<KeyValuePair<string, int>> typeCache, … … 78 75 compositeStack = new Stack<IAccessibleObject>(); 79 76 handlers = new Dictionary<Type, Handler> { 80 {typeof ( CompositeStart), CompositeStartHandler},81 {typeof ( CompositeEnd), CompositeEndHandler},82 {typeof (Primitive ), PrimitiveHandler},83 {typeof (Reference ), ReferenceHandler},84 {typeof (Null ), NullHandler}77 {typeof (BeginToken), CompositeStartHandler}, 78 {typeof (EndToken), CompositeEndHandler}, 79 {typeof (PrimitiveToken), PrimitiveHandler}, 80 {typeof (ReferenceToken), ReferenceHandler}, 81 {typeof (NullReferenceToken), NullHandler} 85 82 }; 86 83 typeIds = new Dictionary<int, Type>(); … … 91 88 } 92 89 93 public object DeSerialize(IEnumerable<I ParseToken> tokens) {90 public object DeSerialize(IEnumerable<ISerializationToken> tokens) { 94 91 finalFixes = new List<Thunk>(); 95 foreach (I ParseToken token in tokens) {92 foreach (ISerializationToken token in tokens) { 96 93 handlers[token.GetType()].Invoke(token); 97 94 } … … 102 99 } 103 100 104 private void CompositeStartHandler(I ParseToken token) {105 CompositeStart start = (CompositeStart)token;106 object instance; 107 Type type = typeIds[ int.Parse(start.TypeId)];101 private void CompositeStartHandler(ISerializationToken token) { 102 BeginToken start = (BeginToken)token; 103 object instance; 104 Type type = typeIds[(int)start.TypeId]; 108 105 if (persistenceConfiguration.GetDecomposer(type) != null) { 109 106 instance = new ParentReference(); … … 118 115 id2obj.Add((int)start.Id, instance); 119 116 } 120 private void CompositeEndHandler(I ParseToken token) {121 CompositeEnd end = (CompositeEnd)token;122 Type type = typeIds[ int.Parse(end.TypeId)];117 private void CompositeEndHandler(ISerializationToken token) { 118 EndToken end = (EndToken)token; 119 Type type = typeIds[(int)end.TypeId]; 123 120 IDecomposer decomposer = persistenceConfiguration.GetDecomposer(type); 124 121 if (decomposer != null) { … … 134 131 SetValue(end.Name, compositeObject.Obj); 135 132 } 136 } 137 private void PrimitiveHandler(I ParseToken token) {138 Primitive primitive = (Primitive)token;139 Type type = typeIds[ int.Parse(primitive.TypeId)];133 } 134 private void PrimitiveHandler(ISerializationToken token) { 135 PrimitiveToken primitive = (PrimitiveToken)token; 136 Type type = typeIds[(int)primitive.TypeId]; 140 137 object value = persistenceConfiguration 141 138 .GetFormatter(XmlFormat.Instance, type) 142 .DeSerialize(primitive.Serial izedValue);139 .DeSerialize(primitive.SerialData); 143 140 if ( ! value.GetType().IsValueType ) 144 141 id2obj[(int)primitive.Id] = value; 145 142 SetValue(primitive.Name, value); 146 143 } 147 private void ReferenceHandler(I ParseToken token) {148 Reference reference = (Reference)token;144 private void ReferenceHandler(ISerializationToken token) { 145 ReferenceToken reference = (ReferenceToken)token; 149 146 object referredObject = id2obj[reference.Id]; 150 147 SetValue(reference.Name, id2obj[reference.Id]); … … 155 152 } 156 153 } 157 private void NullHandler(I ParseToken token) {158 Null nil = (Null)token;154 private void NullHandler(ISerializationToken token) { 155 NullReferenceToken nil = (NullReferenceToken)token; 159 156 SetValue(nil.Name, null); 160 157 } -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1355 r1356 22 22 } 23 23 } 24 25 public Serializer(object obj) : this(obj, PersistenceConfiguration.Instance) {}26 24 27 25 public Serializer(object obj, PersistenceConfiguration persistenceConfiguration) : -
branches/New Persistence Exploration/Persistence/Persistence/Tokens.cs
r1355 r1356 2 2 namespace Persistence { 3 3 4 #region Serialization Tokens4 public interface ISerializationToken {} 5 5 6 public interface ISerializationToken {7 }8 6 public class BeginToken : ISerializationToken { 9 7 public readonly string Name; … … 16 14 } 17 15 } 16 18 17 public class EndToken : ISerializationToken { 19 18 public readonly string Name; … … 26 25 } 27 26 } 27 28 28 public class PrimitiveToken : ISerializationToken { 29 29 public readonly string Name; … … 38 38 } 39 39 } 40 40 41 public class ReferenceToken : ISerializationToken { 41 42 public readonly string Name; … … 46 47 } 47 48 } 49 48 50 public class NullReferenceToken : ISerializationToken { 49 51 public readonly string Name; … … 52 54 } 53 55 } 54 #endregion55 56 #region DeSerialization Tokens57 58 public interface IParseToken { }59 60 public class CompositeStart : IParseToken {61 public readonly string Name;62 public readonly string TypeId;63 public readonly int? Id;64 public CompositeStart(string name, string typeId, int? id) {65 Name = name;66 TypeId = typeId;67 Id = id;68 }69 }70 public class CompositeEnd : IParseToken {71 public readonly string Name;72 public readonly string TypeId;73 public readonly int? Id;74 public CompositeEnd(string name, string typeId, int? id) {75 Name = name;76 TypeId = typeId;77 Id = id;78 }79 }80 public class Primitive : IParseToken {81 public readonly string Name;82 public readonly string TypeId;83 public readonly string SerializedValue;84 public readonly int? Id;85 public Primitive(string name, string typeId, string serilaizedValue, int? id) {86 Name = name;87 TypeId = typeId;88 SerializedValue = serilaizedValue;89 Id = id;90 }91 }92 public class Reference : IParseToken {93 public readonly string Name;94 public readonly int Id;95 public Reference(string name, int id) {96 Name = name;97 Id = id;98 }99 }100 public class Null : IParseToken {101 public readonly string Name;102 public Null(string name) {103 Name = name;104 }105 }106 #endregion107 56 108 57 } -
branches/New Persistence Exploration/Persistence/Persistence/XmlParser.cs
r1355 r1356 8 8 9 9 10 public class XmlParser : IEnumerable<I ParseToken> {10 public class XmlParser : IEnumerable<ISerializationToken> { 11 11 12 12 private readonly XmlReader reader; 13 private delegate IEnumerator<I ParseToken> Handler();13 private delegate IEnumerator<ISerializationToken> Handler(); 14 14 private readonly Dictionary<string, Handler> handlers; 15 15 … … 28 28 }; 29 29 } 30 public IEnumerator<I ParseToken> GetEnumerator() {30 public IEnumerator<ISerializationToken> GetEnumerator() { 31 31 while (reader.Read()) { 32 32 if (!reader.IsStartElement()) { 33 33 break; 34 34 } 35 IEnumerator<I ParseToken> iterator;35 IEnumerator<ISerializationToken> iterator; 36 36 try { 37 37 iterator = handlers[reader.Name].Invoke(); … … 47 47 } 48 48 } 49 private IEnumerator<I ParseToken> ParsePrimitive() {49 private IEnumerator<ISerializationToken> ParsePrimitive() { 50 50 int? id = null; 51 51 string idString = reader.GetAttribute("id"); 52 52 if (idString != null) 53 53 id = int.Parse(idString); 54 yield return new Primitive (54 yield return new PrimitiveToken( 55 55 reader.GetAttribute("name"), 56 reader.GetAttribute("typeId"),56 int.Parse(reader.GetAttribute("typeId")), 57 57 reader.ReadString(), 58 58 id); 59 59 } 60 private IEnumerator<IParseToken> ParseComposite() { 61 string name = reader.GetAttribute("name"); 62 string typeId = reader.GetAttribute("typeId"); 60 private IEnumerator<ISerializationToken> ParseComposite() { 61 string name = reader.GetAttribute("name"); 63 62 string idString = reader.GetAttribute("id"); 64 63 int? id = null; 64 int? typeId = null; 65 65 if (idString != null) 66 id = int.Parse(idString); 67 yield return new CompositeStart(name, typeId, id); 68 IEnumerator<IParseToken> iterator = GetEnumerator(); 66 id = int.Parse(idString); 67 typeId = int.Parse(reader.GetAttribute("typeId")); 68 yield return new BeginToken(name, typeId, id); 69 IEnumerator<ISerializationToken> iterator = GetEnumerator(); 69 70 while (iterator.MoveNext()) 70 71 yield return iterator.Current; 71 yield return new CompositeEnd(name, typeId, id);72 yield return new EndToken(name, typeId, id); 72 73 } 73 private IEnumerator<I ParseToken> ParseReference() {74 yield return new Reference (74 private IEnumerator<ISerializationToken> ParseReference() { 75 yield return new ReferenceToken( 75 76 reader.GetAttribute("name"), 76 77 int.Parse(reader.GetAttribute("ref"))); 77 78 } 78 private IEnumerator<I ParseToken> ParseNull() {79 yield return new Null (reader.GetAttribute("name"));79 private IEnumerator<ISerializationToken> ParseNull() { 80 yield return new NullReferenceToken(reader.GetAttribute("name")); 80 81 } 81 82 IEnumerator IEnumerable.GetEnumerator() { -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1355 r1356 14 14 r.dict.Add("two", 2); 15 15 r.dict.Add("three", 3); 16 Serializer s = new Serializer(r );16 Serializer s = new Serializer(r, PersistenceConfiguration.Instance); 17 17 Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); 18 18 StreamWriter writer = new StreamWriter("test.xml"); … … 31 31 32 32 XmlParser parser = new XmlParser(new StreamReader("test.xml")); 33 DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 33 DeSerializer deSerializer = new DeSerializer( 34 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 35 PersistenceConfiguration.Instance); 34 36 object o = deSerializer.DeSerialize(parser); 35 37 Root t = CloningFactory.DefaultClone(r); … … 39 41 public static void Test2() { 40 42 Manager m = new Manager(); 41 Serializer s = new Serializer(m );43 Serializer s = new Serializer(m, PersistenceConfiguration.Instance); 42 44 Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); 43 45 StreamWriter writer = new StreamWriter("test2.xml"); … … 49 51 writer.Close(); 50 52 XmlParser parser = new XmlParser(new StreamReader("test2.xml")); 51 DeSerializer deSerializer = new DeSerializer(XmlParser.ParseTypeCache(new StreamReader("test-types.xml"))); 53 DeSerializer deSerializer = new DeSerializer( 54 XmlParser.ParseTypeCache(new StreamReader("test-types.xml")), 55 PersistenceConfiguration.Instance); 52 56 object o = deSerializer.DeSerialize(parser); 53 57 Manager n = CloningFactory.DefaultClone(m);
Note: See TracChangeset
for help on using the changeset viewer.