Changeset 5290 for trunk/sources/HeuristicLab.Persistence
- Timestamp:
- 01/13/11 13:01:28 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3
- Files:
-
- 2 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ConcreteDictionarySerializer.cs
r5289 r5290 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using HeuristicLab.Persistence.Auxiliary;25 using System.Linq; 26 26 using HeuristicLab.Persistence.Core; 27 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 31 31 32 32 [StorableClass] 33 internal sealed class DictionarySerializer : ICompositeSerializer {33 internal sealed class ConcreteDictionarySerializer : ICompositeSerializer { 34 34 35 35 [StorableConstructor] 36 private DictionarySerializer(bool deserializing) { }37 public DictionarySerializer() { }36 private ConcreteDictionarySerializer(bool deserializing) { } 37 public ConcreteDictionarySerializer() { } 38 38 39 39 public int Priority { … … 43 43 44 44 public bool CanSerialize(Type type) { 45 return ReflectionTools.HasDefaultConstructor(type) && 46 type.GetInterface(typeof(IDictionary).FullName) != null; 45 return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>); 47 46 } 48 47 49 48 public string JustifyRejection(Type type) { 50 if (!ReflectionTools.HasDefaultConstructor(type)) 51 return "no default constructor"; 52 return "interface IDictionary not implemented"; 49 return "Type is not a generic Dictionary<>"; 53 50 } 54 51 55 52 public IEnumerable<Tag> CreateMetaInfo(object o) { 56 return new Tag[] { };53 yield return new Tag("Comparer", o.GetType().GetProperty("Comparer").GetValue(o, null)); 57 54 } 58 55 … … 66 63 67 64 public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) { 68 return Activator.CreateInstance(t, true);65 return Activator.CreateInstance(t, metaInfo.First().Value); 69 66 } 70 67 … … 79 76 dict.Add(key.Value, value.Value); 80 77 } 81 } 82 catch (InvalidOperationException e) { 78 } catch (InvalidOperationException e) { 83 79 throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e); 84 } 85 catch (NotSupportedException e) { 80 } catch (NotSupportedException e) { 86 81 throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e); 87 } 88 catch (ArgumentNullException e) { 82 } catch (ArgumentNullException e) { 89 83 throw new PersistenceException("Dictionary key was null.", e); 90 } 91 catch (ArgumentException e) { 84 } catch (ArgumentException e) { 92 85 throw new PersistenceException("Duplicate dictionary key.", e); 93 86 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/HashSetSerializer.cs
r5289 r5290 23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 using System.Linq; 25 26 using System.Reflection; 26 using HeuristicLab.Persistence.Auxiliary;27 27 using HeuristicLab.Persistence.Core; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 32 32 33 33 [StorableClass] 34 internal sealed class EnumerableSerializer : ICompositeSerializer {34 internal sealed class HashSetSerializer : ICompositeSerializer { 35 35 36 36 [StorableConstructor] 37 private EnumerableSerializer(bool deserializing) { }38 public EnumerableSerializer() { }37 private HashSetSerializer(bool deserializing) { } 38 public HashSetSerializer() { } 39 39 40 40 public int Priority { 41 get { return 100; }41 get { return 250; } 42 42 } 43 43 44 45 44 public bool CanSerialize(Type type) { 46 return 47 ReflectionTools.HasDefaultConstructor(type) && 48 type.GetInterface(typeof(IEnumerable).FullName) != null && 49 type.GetMethod("Add") != null && 50 type.GetMethod("Add").GetParameters().Length == 1; 45 return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet<>); 51 46 } 52 47 53 48 public string JustifyRejection(Type type) { 54 if (!ReflectionTools.HasDefaultConstructor(type)) 55 return "no default constructor"; 56 if (type.GetInterface(typeof(IEnumerable).FullName) == null) 57 return "interface IEnumerable not implemented"; 58 if (type.GetMethod("Add") == null) 59 return "no 'Add()' method"; 60 return "no 'Add()' method with one argument"; 49 return "Type is not a generic HashSet<>"; 61 50 } 62 51 63 52 public IEnumerable<Tag> CreateMetaInfo(object o) { 64 return new Tag[] { };53 yield return new Tag("Comparer", o.GetType().GetProperty("Comparer").GetValue(o, null)); 65 54 } 66 55 … … 72 61 73 62 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 74 return Activator.CreateInstance(type, true);63 return Activator.CreateInstance(type, metaInfo.First().Value); 75 64 } 76 65 … … 80 69 foreach (var tag in tags) 81 70 addMethod.Invoke(instance, new[] { tag.Value }); 82 } 83 catch (Exception e) { 71 } catch (Exception e) { 84 72 throw new PersistenceException("Exception caught while trying to populate enumerable.", e); 85 73 } 86 74 } 75 76 public object HashSet { get; set; } 87 77 } 88 78 } -
trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj
r5163 r5290 141 141 <Compile Include="Default\CompositeSerializers\ArraySerializer.cs" /> 142 142 <Compile Include="Default\CompositeSerializers\CompactNumberArray2StringSerializer.cs" /> 143 <Compile Include="Default\CompositeSerializers\ConcreteDictionarySerializer.cs" /> 144 <Compile Include="Default\CompositeSerializers\HashSetSerializer.cs" /> 143 145 <Compile Include="Default\CompositeSerializers\DictionarySerializer.cs" /> 144 146 <Compile Include="Default\CompositeSerializers\EnumerableSerializer.cs" /> -
trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs
r4068 r5290 1102 1102 } 1103 1103 } 1104 private class IdentityComparer<T> : IEqualityComparer<T> { 1105 1106 public bool Equals(T x, T y) { 1107 return x.Equals(y); 1108 } 1109 1110 public int GetHashCode(T obj) { 1111 return obj.GetHashCode(); 1112 } 1113 } 1114 1115 [TestMethod] 1116 public void TestHashSetSerializer() { 1117 var hashSets = new List<HashSet<int>>() { 1118 new HashSet<int>(new[] { 1, 2, 3 }), 1119 new HashSet<int>(new[] { 4, 5, 6 }, new IdentityComparer<int>()), 1120 }; 1121 XmlGenerator.Serialize(hashSets, tempFile); 1122 var newHashSets = XmlParser.Deserialize<List<HashSet<int>>>(tempFile); 1123 Assert.IsTrue(newHashSets[0].Contains(1)); 1124 Assert.IsTrue(newHashSets[0].Contains(2)); 1125 Assert.IsTrue(newHashSets[0].Contains(3)); 1126 Assert.IsTrue(newHashSets[1].Contains(4)); 1127 Assert.IsTrue(newHashSets[1].Contains(5)); 1128 Assert.IsTrue(newHashSets[1].Contains(6)); 1129 Assert.AreEqual(newHashSets[0].Comparer.GetType(), new HashSet<int>().Comparer.GetType()); 1130 Assert.AreEqual(newHashSets[1].Comparer.GetType(), typeof(IdentityComparer<int>)); 1131 } 1132 1133 [TestMethod] 1134 public void TestConcreteDictionarySerializer() { 1135 var dictionaries = new List<Dictionary<int, int>>() { 1136 new Dictionary<int, int>(), 1137 new Dictionary<int, int>(new IdentityComparer<int>()), 1138 }; 1139 dictionaries[0].Add(1, 1); 1140 dictionaries[0].Add(2, 2); 1141 dictionaries[0].Add(3, 3); 1142 dictionaries[1].Add(4, 4); 1143 dictionaries[1].Add(5, 5); 1144 dictionaries[1].Add(6, 6); 1145 XmlGenerator.Serialize(dictionaries, tempFile); 1146 var newDictionaries = XmlParser.Deserialize<List<Dictionary<int, int>>>(tempFile); 1147 Assert.IsTrue(newDictionaries[0].ContainsKey(1)); 1148 Assert.IsTrue(newDictionaries[0].ContainsKey(2)); 1149 Assert.IsTrue(newDictionaries[0].ContainsKey(3)); 1150 Assert.IsTrue(newDictionaries[1].ContainsKey(4)); 1151 Assert.IsTrue(newDictionaries[1].ContainsKey(5)); 1152 Assert.IsTrue(newDictionaries[1].ContainsKey(6)); 1153 Assert.IsTrue(newDictionaries[0].ContainsValue(1)); 1154 Assert.IsTrue(newDictionaries[0].ContainsValue(2)); 1155 Assert.IsTrue(newDictionaries[0].ContainsValue(3)); 1156 Assert.IsTrue(newDictionaries[1].ContainsValue(4)); 1157 Assert.IsTrue(newDictionaries[1].ContainsValue(5)); 1158 Assert.IsTrue(newDictionaries[1].ContainsValue(6)); 1159 Assert.AreEqual(newDictionaries[0].Comparer.GetType(), new Dictionary<int, int>().Comparer.GetType()); 1160 Assert.AreEqual(newDictionaries[1].Comparer.GetType(), typeof(IdentityComparer<int>)); 1161 } 1104 1162 1105 1163 [ClassInitialize]
Note: See TracChangeset
for help on using the changeset viewer.