Changeset 1437 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/27/09 12:50:34 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1435 r1437 17 17 } 18 18 19 public void AddValue(string name, object value, List< DeSerializer.Thunk> finalFixes) {19 public void AddValue(string name, object value, List<Thunk> finalFixes) { 20 20 Tag t = new Tag(name, value); 21 21 t.finalFixes = finalFixes; … … 23 23 } 24 24 25 public Setter GetSetter (string name) {25 public Setter GetSetterForLastAddedValue(string name) { 26 26 Tag t = customValues[customValues.Count - 1]; 27 27 return value => t.Value = value; 28 28 } 29 } 29 } 30 31 public delegate void Thunk(); 30 32 31 33 public class DeSerializer { 32 33 private delegate void Handler(ISerializationToken token); 34 public delegate void Thunk(); 35 34 36 35 private readonly Dictionary<int, object> id2obj; 37 private readonly Dictionary<Type, object> serializerMapping; 38 private readonly Dictionary<Type, Handler> handlers; 36 private readonly Dictionary<Type, object> serializerMapping; 39 37 private readonly Stack<CompositeObject> parentStack; 40 38 private readonly Dictionary<int, Type> typeIds; … … 45 43 id2obj = new Dictionary<int, object>(); 46 44 parentStack = new Stack<CompositeObject>(); 47 handlers = new Dictionary<Type, Handler> {48 {typeof (BeginToken), CompositeStartHandler},49 {typeof (EndToken), CompositeEndHandler},50 {typeof (PrimitiveToken), PrimitiveHandler},51 {typeof (ReferenceToken), ReferenceHandler},52 {typeof (NullReferenceToken), NullHandler}53 };54 45 typeIds = new Dictionary<int, Type>(); 55 serializerMapping = new Dictionary<Type, object>(); 56 foreach ( var typeMapping in typeCache ) { 46 serializerMapping = createSerializers(typeCache); 47 } 48 49 private Dictionary<Type, object> createSerializers(IEnumerable<TypeMapping> typeCache) { 50 var serializerMapping = new Dictionary<Type, object>(); 51 foreach (var typeMapping in typeCache) { 57 52 Type type = Type.GetType(typeMapping.TypeName); 58 53 typeIds.Add(typeMapping.Id, type); … … 62 57 } 63 58 } 59 return serializerMapping; 64 60 } 65 61 … … 67 63 finalFixes = new List<Thunk>(); 68 64 foreach (ISerializationToken token in tokens) { 69 handlers[token.GetType()].Invoke(token); 65 Type t = token.GetType(); 66 if ( t == typeof(BeginToken) ) { 67 CompositeStartHandler((BeginToken)token); 68 } else if ( t == typeof(EndToken) ) { 69 CompositeEndHandler((EndToken) token); 70 } else if ( t == typeof(PrimitiveToken) ) { 71 PrimitiveHandler((PrimitiveToken) token); 72 } else if ( t == typeof(ReferenceToken) ) { 73 ReferenceHandler((ReferenceToken) token); 74 } else if (t == typeof(NullReferenceToken)) { 75 NullHandler((NullReferenceToken)token); 76 } else { 77 throw new ApplicationException("invalid token type"); 78 } 70 79 } 71 80 foreach (Thunk fix in finalFixes) { … … 75 84 } 76 85 77 private void CompositeStartHandler(ISerializationToken token) { 78 BeginToken start = (BeginToken)token; 79 object instance; 80 Type type = typeIds[(int)start.TypeId]; 86 private void CompositeStartHandler(BeginToken token) { 87 Type type = typeIds[(int)token.TypeId]; 81 88 IDecomposer decomposer = null; 82 89 if ( serializerMapping.ContainsKey(type) ) 83 90 decomposer = serializerMapping[type] as IDecomposer; 84 if (decomposer != null) { 85 instance = decomposer.CreateInstance(type); 86 if (instance == null) 87 instance = new ParentReference(); 88 parentStack.Push(new CompositeObject(instance)); 89 } else { 91 if (decomposer == null) 90 92 throw new ApplicationException(String.Format( 91 93 "No suitable method for deserialization of type \"{0}\" found.", 92 94 type.VersionInvariantName())); 93 } 94 if ( start.Id != null ) 95 id2obj.Add((int)start.Id, instance); 95 object instance = decomposer.CreateInstance(type); 96 if (instance == null) 97 instance = new ParentReference(); 98 parentStack.Push(new CompositeObject(instance)); 99 if ( token.Id != null ) 100 id2obj.Add((int)token.Id, instance); 96 101 } 97 102 98 private void CompositeEndHandler(ISerializationToken token) { 99 EndToken end = (EndToken)token; 100 Type type = typeIds[(int)end.TypeId]; 103 private void CompositeEndHandler(EndToken token) { 104 Type type = typeIds[(int)token.TypeId]; 101 105 IDecomposer decomposer = null; 102 106 if (serializerMapping.ContainsKey(type)) 103 107 decomposer = serializerMapping[type] as IDecomposer; 104 if (decomposer != null) { 105 CompositeObject customComposite = (CompositeObject)parentStack.Pop(); 106 object deserializedObject = 107 decomposer.Populate(customComposite.Obj, customComposite.customValues, type); 108 if ( end.Id != null ) 109 id2obj[(int)end.Id] = deserializedObject; 110 SetValue(end.Name, deserializedObject); 111 } else { 108 if (decomposer == null) 112 109 throw new ApplicationException(String.Format( 113 110 "No suitable method for deserialization of type \"{0}\" found.", 114 111 type.VersionInvariantName())); 115 } 112 CompositeObject customComposite = (CompositeObject)parentStack.Pop(); 113 object deserializedObject = 114 decomposer.Populate(customComposite.Obj, customComposite.customValues, type); 115 if ( token.Id != null ) 116 id2obj[(int)token.Id] = deserializedObject; 117 SetValue(token.Name, deserializedObject); 116 118 } 117 119 118 private void PrimitiveHandler(ISerializationToken token) { 119 PrimitiveToken primitive = (PrimitiveToken)token; 120 Type type = typeIds[(int)primitive.TypeId]; 121 object value = ((IFormatter) serializerMapping[type]).Parse(primitive.SerialData); 122 if ( primitive.Id != null ) 123 id2obj[(int)primitive.Id] = value; 124 SetValue(primitive.Name, value); 120 private void PrimitiveHandler(PrimitiveToken token) { 121 Type type = typeIds[(int)token.TypeId]; 122 object value = ((IFormatter) serializerMapping[type]).Parse(token.SerialData); 123 if ( token.Id != null ) 124 id2obj[(int)token.Id] = value; 125 SetValue(token.Name, value); 125 126 } 126 127 127 private void ReferenceHandler(ISerializationToken token) { 128 ReferenceToken reference = (ReferenceToken)token; 129 object referredObject = id2obj[reference.Id]; 130 SetValue(reference.Name, id2obj[reference.Id]); 128 private void ReferenceHandler(ReferenceToken token) { 129 object referredObject = id2obj[token.Id]; 130 SetValue(token.Name, referredObject); 131 131 if (referredObject is ParentReference) { 132 Setter set = parentStack.Peek().GetSetter(reference.Name); 133 int id = reference.Id; 134 finalFixes.Add(() => set(id2obj[id])); 132 Setter set = parentStack.Peek().GetSetterForLastAddedValue(token.Name); 133 finalFixes.Add(() => set(id2obj[token.Id])); 135 134 } 136 135 } 137 136 138 private void NullHandler(ISerializationToken token) { 139 NullReferenceToken nullToken = (NullReferenceToken)token; 140 SetValue(nullToken.Name, null); 137 private void NullHandler(NullReferenceToken token) { 138 SetValue(token.Name, null); 141 139 } 142 140 -
branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/Compact/NumberArray2XmlFormatters.cs
r1361 r1437 22 22 sb.Append(a.GetLength(i)); 23 23 } 24 for (int i = 0; i < a.Rank; i++) { 25 sb.Append(Separator); 26 sb.Append(a.GetLowerBound(i)); 27 } 24 28 foreach (object o in a) { 25 29 sb.Append(Separator); … … 41 45 lengths[i] = int.Parse((string)values.Current); 42 46 } 43 Array a = Array.CreateInstance(this.Type.GetElementType(), lengths); 47 int[] lowerBounds = new int[rank]; 48 for (int i = 0; i < rank; i++) { 49 values.MoveNext(); 50 lowerBounds[i] = int.Parse((string)values.Current); 51 } 52 Array a = Array.CreateInstance(this.Type.GetElementType(), lengths, lowerBounds); 44 53 int[] positions = new int[rank]; 45 54 while (values.MoveNext()) { -
branches/New Persistence Exploration/Persistence/Persistence/Default/Xml/XmlGenerator.cs
r1428 r1437 139 139 140 140 public static void Serialize(object o, string basename, Configuration configuration) { 141 Serializer s = new Serializer(o, configuration);141 Serializer s = new Serializer(o, configuration); 142 142 XmlGenerator xmlGenerator = new XmlGenerator(); 143 143 StreamWriter writer = new StreamWriter(basename + ".xml"); -
branches/New Persistence Exploration/Persistence/Persistence/Interfaces/IDecomposer.cs
r1435 r1437 7 7 8 8 public class Tag { 9 public List< DeSerializer.Thunk> finalFixes;9 public List<Thunk> finalFixes; 10 10 public string Name { get; private set; } 11 11 public object Value;
Note: See TracChangeset
for help on using the changeset viewer.