Changeset 1361
- Timestamp:
- 03/19/09 13:30:58 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence
- Files:
-
- 22 added
- 6 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/Core/DeSerializer.cs
r1360 r1361 16 16 17 17 18 class Custom Object: IAccessibleObject {18 class CustomComposite : IAccessibleObject { 19 19 20 20 public object Obj { get; private set; } 21 21 public readonly List<object> customValues; 22 22 23 public Custom Object(object obj) {23 public CustomComposite(object obj) { 24 24 Obj = obj; 25 25 customValues = new List<object>(); … … 37 37 38 38 39 class CompositeObject: IAccessibleObject {39 class StorableComposite : IAccessibleObject { 40 40 41 41 public object Obj { get; private set; } 42 42 public readonly Dictionary<string, DataMemberAccessor> accessorDict; 43 43 44 public CompositeObject(object obj, Dictionary<string, DataMemberAccessor> accessorDict) {44 public StorableComposite(object obj, Dictionary<string, DataMemberAccessor> accessorDict) { 45 45 Obj = obj; 46 46 this.accessorDict = new Dictionary<string, DataMemberAccessor>(); … … 76 76 private readonly Dictionary<int, object> id2obj; 77 77 private readonly Dictionary<Type, Handler> handlers; 78 private readonly Stack<IAccessibleObject> compositeStack;78 private readonly Stack<IAccessibleObject> parentStack; 79 79 private readonly Configuration configuration; 80 80 private readonly Dictionary<int, Type> typeIds; … … 86 86 this.configuration = configuration; 87 87 id2obj = new Dictionary<int, object>(); 88 compositeStack = new Stack<IAccessibleObject>();88 parentStack = new Stack<IAccessibleObject>(); 89 89 handlers = new Dictionary<Type, Handler> { 90 90 {typeof (BeginToken), CompositeStartHandler}, … … 109 109 fix(); 110 110 } 111 return compositeStack.Pop().Obj;111 return parentStack.Pop().Obj; 112 112 } 113 113 … … 118 118 if (configuration.GetDecomposer(type) != null) { 119 119 instance = new ParentReference(); 120 compositeStack.Push(new CustomObject(instance));120 parentStack.Push(new CustomComposite(instance)); 121 121 } else { 122 122 instance = Activator.CreateInstance(type, true); 123 123 Dictionary<string, DataMemberAccessor> accessorDict = 124 124 StorableAttribute.GetAutostorableAccessors(instance); 125 compositeStack.Push(new CompositeObject(instance, accessorDict));125 parentStack.Push(new StorableComposite(instance, accessorDict)); 126 126 } 127 127 if ( start.Id != null ) … … 134 134 IDecomposer decomposer = configuration.GetDecomposer(type); 135 135 if (decomposer != null) { 136 Custom Object customObject = (CustomObject)compositeStack.Pop();136 CustomComposite customComposite = (CustomComposite)parentStack.Pop(); 137 137 object deserializedObject = 138 decomposer.Compose(custom Object.customValues, type);138 decomposer.Compose(customComposite.customValues, type); 139 139 if ( end.Id != null ) 140 140 id2obj[(int)end.Id] = deserializedObject; 141 141 SetValue(end.Name, deserializedObject); 142 142 } else { 143 CompositeObject compositeObject = (CompositeObject)compositeStack.Pop();144 compositeObject.PopulateDefaultValues();145 SetValue(end.Name, compositeObject.Obj);143 StorableComposite storableComposite = (StorableComposite)parentStack.Pop(); 144 storableComposite.PopulateDefaultValues(); 145 SetValue(end.Name, storableComposite.Obj); 146 146 } 147 147 } … … 163 163 SetValue(reference.Name, id2obj[reference.Id]); 164 164 if (referredObject is ParentReference) { 165 Setter set = compositeStack.Peek().GetSetter(reference.Name);165 Setter set = parentStack.Peek().GetSetter(reference.Name); 166 166 int id = reference.Id; 167 167 finalFixes.Add(() => set(id2obj[id])); … … 175 175 176 176 private void SetValue(string name, object value) { 177 if ( compositeStack.Count == 0) {178 compositeStack.Push(new CompositeObject(value, new Dictionary<string, DataMemberAccessor>()));177 if (parentStack.Count == 0) { 178 parentStack.Push(new StorableComposite(value, new Dictionary<string, DataMemberAccessor>())); 179 179 } else { 180 object accessibleObject = compositeStack.Peek();181 if (accessibleObject is CompositeObject) {182 (( CompositeObject)accessibleObject).SetValue(name, value);183 } else if (accessibleObject is Custom Object) {184 ((Custom Object)accessibleObject).AddValue(value);180 object accessibleObject = parentStack.Peek(); 181 if (accessibleObject is StorableComposite) { 182 ((StorableComposite)accessibleObject).SetValue(name, value); 183 } else if (accessibleObject is CustomComposite) { 184 ((CustomComposite)accessibleObject).AddValue(value); 185 185 } 186 186 } -
branches/New Persistence Exploration/Persistence/Persistence/Core/Serializer.cs
r1360 r1361 45 45 yield return iterator.Current; 46 46 } 47 48 47 49 48 private IEnumerator<ISerializationToken> Serialize(DataMemberAccessor accessor) { 50 49 object value = accessor.Get(); 51 50 if (value == null) 52 return NullReferenceEnumerat ion(accessor.Name);51 return NullReferenceEnumerator(accessor.Name); 53 52 if (obj2id.ContainsKey(value)) 54 return Reference TokenEnumeration(accessor.Name, obj2id[value]);53 return ReferenceEnumerator(accessor.Name, obj2id[value]); 55 54 if ( ! typeCache.ContainsKey(value.GetType())) 56 55 typeCache.Add(value.GetType(), typeCache.Count); … … 63 62 IFormatter formatter = configuration.GetFormatter(value.GetType()); 64 63 if (formatter != null) 65 return PrimitiveEnumerat ion(accessor.Name, typeId, formatter.DoFormat(value), id);64 return PrimitiveEnumerator(accessor.Name, typeId, formatter.DoFormat(value), id); 66 65 IDecomposer decomposer = configuration.GetDecomposer(value.GetType()); 67 66 if (decomposer != null) 68 return CompositeEnumerat ion(accessor.Name, decomposer.DeCompose(value), id, typeId);69 return StorableEnumerat ion(accessor.Name, value, id, typeId);67 return CompositeEnumerator(accessor.Name, decomposer.DeCompose(value), id, typeId); 68 return StorableEnumerator(accessor.Name, value, id, typeId); 70 69 } 71 70 72 private IEnumerator<ISerializationToken> NullReferenceEnumerat ion(string name) {71 private IEnumerator<ISerializationToken> NullReferenceEnumerator(string name) { 73 72 yield return new NullReferenceToken(name); 74 73 } 75 74 76 private IEnumerator<ISerializationToken> Reference TokenEnumeration(string name, int id) {75 private IEnumerator<ISerializationToken> ReferenceEnumerator(string name, int id) { 77 76 yield return new ReferenceToken(name, id); 78 77 } 79 78 80 private IEnumerator<ISerializationToken> PrimitiveEnumeration(string name, int typeId, object serializedValue, int? id) { 79 private IEnumerator<ISerializationToken> PrimitiveEnumerator(string name, 80 int typeId, object serializedValue, int? id) { 81 81 yield return new PrimitiveToken(name, typeId, serializedValue, id); 82 82 } 83 83 84 private IEnumerator<ISerializationToken> CompositeEnumeration(string name, IEnumerable values, int? id, int typeId) { 84 private IEnumerator<ISerializationToken> CompositeEnumerator(string name, 85 IEnumerable values, int? id, int typeId) { 85 86 yield return new BeginToken(name, typeId, id); 86 87 foreach (object o in values) { … … 92 93 } 93 94 94 private IEnumerator<ISerializationToken> StorableEnumeration(string name, object value, int? id, int typeId) { 95 private IEnumerator<ISerializationToken> StorableEnumerator(string name, 96 object value, int? id, int typeId) { 95 97 yield return new BeginToken(name, typeId, id); 96 98 int nSubComponents = 0; 97 99 foreach (KeyValuePair<string, DataMemberAccessor> mapping in 98 100 StorableAttribute.GetAutostorableAccessors(value)) { 99 nSubComponents += 1; 101 nSubComponents += 1; 100 102 IEnumerator<ISerializationToken> iterator = Serialize(mapping.Value); 101 103 while (iterator.MoveNext()) { … … 104 106 } 105 107 if (nSubComponents == 0) { 106 throw new ApplicationException(String.Format( 107 "Composite value of type \"{0}\" contains no subcomponents", 108 value.GetType().FullName)); 108 throw new ApplicationException( 109 String.Format( 110 "CustomComposite value of type \"{0}\" contains no subcomponents", 111 value.GetType().FullName)); 109 112 } 110 113 yield return new EndToken(name, typeId, id); -
branches/New Persistence Exploration/Persistence/Persistence/HeuristicLab.Persistence.csproj
r1360 r1361 55 55 </ItemGroup> 56 56 <ItemGroup> 57 <Compile Include="Default\NumberArrayFormatters.cs" /> 57 <Compile Include="Default\Decomposers\EnumerableDecomposer.cs" /> 58 <Compile Include="Default\Decomposers\ArrayDecomposer.cs" /> 59 <Compile Include="Default\Decomposers\KeyValuePairDecomposer.cs" /> 60 <Compile Include="Default\Decomposers\DictionaryDecomposer.cs" /> 61 <Compile Include="Default\Xml\Compact\IntArray2XmlFormatters.cs" /> 62 <Compile Include="Default\Xml\Compact\DoubleArray2XmlFormatters.cs" /> 63 <Compile Include="Default\Xml\Compact\DoubleList2XmlFormatter.cs" /> 64 <Compile Include="Default\Xml\Compact\NumberEnumeration2XmlFormatter.cs" /> 65 <Compile Include="Default\Xml\Compact\NumberArray2XmlFormatters.cs" /> 58 66 <Compile Include="Core\DataMemberAccessor.cs" /> 59 <Compile Include="Default\EnumerableFormatters.cs" /> 67 <Compile Include="Default\Xml\Compact\IntList2XmlFormatter.cs" /> 68 <Compile Include="Default\Xml\Primitive\Sting2XmlFormatter.cs" /> 69 <Compile Include="Default\Xml\Primitive\Int2XmlFormatter.cs" /> 70 <Compile Include="Default\Xml\Primitive\Double2XmlFormatter.cs" /> 71 <Compile Include="Default\Xml\Primitive\Boolean2XmlFormatter.cs" /> 72 <Compile Include="Default\Xml\Primitive\DateTime2XmlFormatter.cs" /> 73 <Compile Include="Default\Xml\XmlFormat.cs" /> 60 74 <Compile Include="HeuristicLabPersistencePlugin.cs" /> 61 75 <Compile Include="Core\DeSerializer.cs" /> … … 65 79 <Compile Include="Properties\AssemblyInfo.cs" /> 66 80 <Compile Include="Core\Serializer.cs" /> 67 <Compile Include="Default\Decomposers.cs" />68 <Compile Include="Default\PrimitiveFormatters.cs" />69 81 <Compile Include="Interfaces\Tokens.cs" /> 70 82 <Compile Include="Util.cs" /> 71 83 <Compile Include="Core\StorableAttribute.cs" /> 72 <Compile Include="Default\Xml Formatter.cs" />73 <Compile Include="Default\Xml Parser.cs" />84 <Compile Include="Default\Xml\XmlGenerator.cs" /> 85 <Compile Include="Default\Xml\XmlParser.cs" /> 74 86 </ItemGroup> 75 87 <PropertyGroup> -
branches/New Persistence Exploration/Persistence/Test/NewSerializationTest.cs
r1360 r1361 4 4 using System.IO; 5 5 using HeuristicLab.Persistence.Core; 6 using HeuristicLab.Persistence.Default.Xml; 6 7 using HeuristicLab.Persistence.Interfaces; 7 8 … … 154 155 r.dict.Add("three", 3); 155 156 Serializer s = new Serializer(r, ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 156 Persistence.XmlFormatter xmlFormatter = new XmlFormatter();157 XmlGenerator xmlGenerator = new XmlGenerator(); 157 158 StreamWriter writer = new StreamWriter("test.xml"); 158 159 foreach (ISerializationToken token in s) { 159 string line = xml Formatter.Format(token);160 string line = xmlGenerator.Format(token); 160 161 writer.Write(line); 161 162 Console.Out.Write(line); … … 163 164 writer.Close(); 164 165 writer = new StreamWriter("test-types.xml"); 165 foreach (string line in xml Formatter.Format(s.TypeCache)) {166 foreach (string line in xmlGenerator.Format(s.TypeCache)) { 166 167 writer.WriteLine(line); 167 168 Console.Out.WriteLine(line); … … 181 182 Serializer s = new Serializer(m, 182 183 ConfigurationService.Instance.GetDefaultConfig(XmlFormat.Instance)); 183 Persistence.XmlFormatter xmlFormatter = new XmlFormatter();184 XmlGenerator xmlGenerator = new XmlGenerator(); 184 185 StreamWriter writer = new StreamWriter("test2.xml"); 185 186 foreach (ISerializationToken token in s) { 186 string line = xml Formatter.Format(token);187 string line = xmlGenerator.Format(token); 187 188 writer.Write(line); 188 189 Console.Out.Write(line);
Note: See TracChangeset
for help on using the changeset viewer.