Changeset 1317 for branches/New Persistence Exploration/Persistence
- Timestamp:
- 03/09/09 15:16:47 (16 years ago)
- Location:
- branches/New Persistence Exploration/Persistence/Persistence
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/New Persistence Exploration/Persistence/Persistence/DeSerializer.cs
r1314 r1317 98 98 CompositeStart start = (CompositeStart)token; 99 99 object instance = null; 100 if (this.FindC ustomSerializer(start.Type) != null) {100 if (this.FindCompoundSerializer(start.Type) != null) { 101 101 instance = new ParentReference(); 102 102 compositeStack.Push(new CustomObject(instance)); … … 112 112 private void CompositeEndHandler(IParseToken token) { 113 113 CompositeEnd end = (CompositeEnd)token; 114 ICompoundSerializer customSerializer = this.FindC ustomSerializer(end.Type);114 ICompoundSerializer customSerializer = this.FindCompoundSerializer(end.Type); 115 115 if (customSerializer != null) { 116 116 CustomObject customObject = (CustomObject)compositeStack.Pop(); … … 124 124 } 125 125 } 126 private ICompoundSerializer FindC ustomSerializer(Type type) {126 private ICompoundSerializer FindCompoundSerializer(Type type) { 127 127 foreach (ICompoundSerializer serializer in customSerializers) { 128 128 if (serializer.CanSerialize(type)) -
branches/New Persistence Exploration/Persistence/Persistence/NewSerializationTest.cs
r1314 r1317 21 21 [Storable] 22 22 public List<Root> selfReferences; 23 [Storable] 24 public double[,] multiDimArray = new double[,] { { 1, 2, 3 }, { 3, 4, 5 } }; 25 [Storable] 26 public bool boolean = true; 27 [Storable] 28 public DateTime dateTime = new DateTime(); 29 [Storable] 30 public KeyValuePair<string, int> kvp = new KeyValuePair<string, int>("test key", 123); 31 [Storable] 32 public Dictionary<string, int> dict = new Dictionary<string, int>(); 23 33 } 24 34 … … 40 50 r.c = new Custom(); 41 51 r.c.r = r; 42 IPrimitiveSerializer[] serializers = { 52 r.dict.Add("one", 1); 53 r.dict.Add("two", 2); 54 r.dict.Add("three", 3); 55 56 Serializer s = new Serializer(r, 57 new IPrimitiveSerializer[] { 43 58 new String2XMLSerializer(), 44 59 new Int2XMLSerializer(), 45 new Double2XmlSerializer()}; 46 Serializer s = new Serializer(r, serializers); 60 new Double2XmlSerializer(), 61 new Boolean2XmlSerializer(), 62 new DateTime2XmlSerializer(), 63 }, new ICompoundSerializer[] { 64 new EnumerableSerializer(), 65 new ArraySerializer(), 66 new KeyValuePair2XmlSerializer(), 67 new Dictionary2XmlSerializer(), 68 }); 47 69 Persistence.XmlFormatter xmlFormatter = new Persistence.XmlFormatter(); 48 70 StreamWriter writer = new StreamWriter("test.xml"); … … 59 81 new String2XMLSerializer(), 60 82 new Double2XmlSerializer(), 83 new Boolean2XmlSerializer(), 84 new DateTime2XmlSerializer(), 61 85 }, 62 86 new ICompoundSerializer[] { 63 87 new ArraySerializer(), 64 new EnumerableSerializer() }); 88 new EnumerableSerializer(), 89 new KeyValuePair2XmlSerializer(), 90 new Dictionary2XmlSerializer(), 91 }); 65 92 object o = deSerializer.DeSerialize(parser); 66 93 Console.Out.WriteLine(Util.AutoFormat(o, true)); -
branches/New Persistence Exploration/Persistence/Persistence/Serializer.cs
r1314 r1317 11 11 private Dictionary<object, int> obj2id; 12 12 private Dictionary<Type, IPrimitiveSerializer> primitiveSerializers; 13 private List<ICompoundSerializer> c ustomSerializers;13 private List<ICompoundSerializer> compoundSerializers; 14 14 15 public Serializer(object obj, IEnumerable<IPrimitiveSerializer> primitiveSerializers) : 16 this(obj, primitiveSerializers, "ROOT") { } 15 public Serializer(object obj, 16 IEnumerable<IPrimitiveSerializer> primitiveSerializers, 17 IEnumerable<ICompoundSerializer> compoundSerializers) : 18 this(obj, primitiveSerializers, compoundSerializers, "ROOT") { } 17 19 18 public Serializer(object obj, IEnumerable<IPrimitiveSerializer> primitiveSerializers, string rootName) { 20 public Serializer(object obj, 21 IEnumerable<IPrimitiveSerializer> primitiveSerializers, 22 IEnumerable<ICompoundSerializer> compoundSerializers, string rootName) { 19 23 this.obj = obj; 20 24 this.rootName = rootName; … … 23 27 this.primitiveSerializers.Add(serializer.Type, serializer); 24 28 } 25 this.customSerializers = new List<ICompoundSerializer>(); 26 customSerializers.Add(new EnumerableSerializer()); 27 customSerializers.Add(new ArraySerializer()); 29 this.compoundSerializers = new List<ICompoundSerializer>(compoundSerializers); 28 30 this.obj2id = new Dictionary<object, int>(); 29 31 obj2id.Add(new object(), 0); … … 54 56 this.obj2id.Add(value, id); 55 57 yield return new BeginToken(accessor, id); 56 ICompoundSerializer customSerializer = this.FindC ustomSerializer(value.GetType());58 ICompoundSerializer customSerializer = this.FindCompoundSerializer(value.GetType()); 57 59 if (customSerializer != null) { 58 60 foreach (object obj in customSerializer.Serialize(value)) { … … 62 64 } 63 65 } else { // composite serialization 66 int nSubComponents = 0; 64 67 foreach (KeyValuePair<string, DataMemberAccessor> mapping in 65 68 StorableAttribute.GetAutostorableAccessors(value)) { 66 IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value); 67 while (iterator.MoveNext()) 69 nSubComponents += 1; 70 IEnumerator<ISerializationToken> iterator = this.Serialize(mapping.Value); 71 while (iterator.MoveNext()) { 68 72 yield return iterator.Current; 73 } 74 } 75 if (nSubComponents == 0) { 76 throw new ApplicationException(String.Format( 77 "Composite value of type \"{0}\" contains no subcomponents", 78 value.GetType().FullName)); 69 79 } 70 80 } … … 73 83 } 74 84 75 private ICompoundSerializer FindC ustomSerializer(Type type) {76 foreach (ICompoundSerializer s in c ustomSerializers) {85 private ICompoundSerializer FindCompoundSerializer(Type type) { 86 foreach (ICompoundSerializer s in compoundSerializers) { 77 87 if (s.CanSerialize(type)) 78 88 return s;
Note: See TracChangeset
for help on using the changeset viewer.