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
75 ((System.Web.J2EE.ObjectOutputStream)w.BaseStream).NativeStream.writeObject(value);
77 BinaryFormatter bf = new BinaryFormatter ();
78 bf.Serialize (w.BaseStream, value);
86 w.Write ((string) value);
89 w.Write ((int) value);
92 w.Write ((bool) value);
95 w.Write (((DateTime) value).Ticks);
98 w.Write ((decimal) value);
101 w.Write ((byte) value);
104 w.Write ((char) value);
107 w.Write ((float) value);
110 w.Write ((double) value);
113 w.Write ((short) value);
116 w.Write ((long) value);
119 w.Write ((ushort) value);
122 w.Write ((uint) value);
125 w.Write ((ulong) value);
130 internal static object DeserializeFromIndex (int index, BinaryReader r)
134 return ((System.Web.J2EE.ObjectInputStream)r.BaseStream).NativeStream.readObject();
136 BinaryFormatter bf = new BinaryFormatter ();
137 return bf.Deserialize (r.BaseStream);
144 value = r.ReadString ();
147 value = r.ReadInt32 ();
150 value = r.ReadBoolean ();
153 value = new DateTime (r.ReadInt64 ());
156 value = r.ReadDecimal ();
159 value = r.ReadByte ();
162 value = r.ReadChar ();
165 value = r.ReadSingle ();
168 value = r.ReadDouble ();
171 value = r.ReadInt16 ();
174 value = r.ReadInt64 ();
177 value = r.ReadUInt16 ();
180 value = r.ReadUInt32 ();
183 value = r.ReadUInt64 ();