2 // System.Web.Util.AltSerialization
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Jackson Harper (jackson@ximian.com)
8 // (C) 2003 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Runtime.Serialization.Formatters.Binary;
37 namespace System.Web.Util {
39 internal sealed class AltSerialization {
41 private static ArrayList types;
43 internal static readonly int NullIndex = 16;
45 private AltSerialization () { }
48 static AltSerialization ()
50 types = new ArrayList ();
52 types.Add (typeof (string));
53 types.Add (typeof (int));
54 types.Add (typeof (bool));
55 types.Add (typeof (DateTime));
56 types.Add (typeof (Decimal));
57 types.Add (typeof (Byte));
58 types.Add (typeof (Char));
59 types.Add (typeof (Single));
60 types.Add (typeof (Double));
61 types.Add (typeof (short));
62 types.Add (typeof (long));
63 types.Add (typeof (ushort));
64 types.Add (typeof (uint));
65 types.Add (typeof (ulong));
68 internal static void SerializeByType (BinaryWriter w, object value)
70 Type type = value.GetType ();
71 int i = types.IndexOf (type);
73 w.Write (15); // types.Count
74 BinaryFormatter bf = new BinaryFormatter ();
75 bf.Serialize (w.BaseStream, value);
82 w.Write ((string) value);
85 w.Write ((int) value);
88 w.Write ((bool) value);
91 w.Write (((DateTime) value).Ticks);
94 w.Write ((decimal) value);
97 w.Write ((byte) value);
100 w.Write ((char) value);
103 w.Write ((float) value);
106 w.Write ((double) value);
109 w.Write ((short) value);
112 w.Write ((long) value);
115 w.Write ((ushort) value);
118 w.Write ((uint) value);
121 w.Write ((ulong) value);
126 internal static object DeserializeFromIndex (int index, BinaryReader r)
129 BinaryFormatter bf = new BinaryFormatter ();
130 return bf.Deserialize (r.BaseStream);
136 value = r.ReadString ();
139 value = r.ReadInt32 ();
142 value = r.ReadBoolean ();
145 value = new DateTime (r.ReadInt64 ());
148 value = r.ReadDecimal ();
151 value = r.ReadByte ();
154 value = r.ReadChar ();
157 value = r.ReadSingle ();
160 value = r.ReadDouble ();
163 value = r.ReadInt16 ();
166 value = r.ReadInt64 ();
169 value = r.ReadUInt16 ();
172 value = r.ReadUInt32 ();
175 value = r.ReadUInt64 ();