merge -r 58060:58217
[mono.git] / mcs / class / System.Web / System.Web.Util / AltSerialization.cs
1 //
2 // System.Web.Util.AltSerialization
3 //
4 // Author(s):
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Jackson Harper (jackson@ximian.com)
7 //
8 // (C) 2003 Novell, Inc (http://www.novell.com)
9 //
10
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31
32 using System;
33 using System.IO;
34 using System.Collections;
35 using System.Runtime.Serialization.Formatters.Binary;
36
37 namespace System.Web.Util {
38
39         internal sealed class AltSerialization {
40
41                 private static ArrayList types;
42
43                 internal static readonly int NullIndex = 16;
44                 
45                 private AltSerialization () { }
46                 
47                 
48                 static AltSerialization ()
49                 {
50                         types = new ArrayList ();
51                         types.Add ("");
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));
66                 }
67                 
68                 internal static void SerializeByType (BinaryWriter w, object value)
69                 {
70                         Type type = value.GetType ();
71                         int i = types.IndexOf (type);
72                         if (i == -1) {
73                                 w.Write (15); // types.Count
74                                 BinaryFormatter bf = new BinaryFormatter ();
75                                 bf.Serialize (w.BaseStream, value);
76                                 return;
77                         }
78                         
79                         w.Write (i);
80                         switch (i) {
81                         case 1:
82                                 w.Write ((string) value);
83                                 break;
84                         case 2:
85                                 w.Write ((int) value);
86                                 break;
87                         case 3:
88                                 w.Write ((bool) value);
89                                 break;
90                         case 4:
91                                 w.Write (((DateTime) value).Ticks);
92                                 break;
93                         case 5:
94                                 w.Write ((decimal) value);
95                                 break;
96                         case 6:
97                                 w.Write ((byte) value);
98                                 break;
99                         case 7:
100                                 w.Write ((char) value);
101                                 break;
102                         case 8:
103                                 w.Write ((float) value);
104                                 break;
105                         case 9:
106                                 w.Write ((double) value);
107                                 break;
108                         case 10:
109                                 w.Write ((short) value);
110                                 break;
111                         case 11:
112                                 w.Write ((long) value);
113                                 break;
114                         case 12:
115                                 w.Write ((ushort) value);
116                                 break;
117                         case 13:
118                                 w.Write ((uint) value);
119                                 break;
120                         case 14:
121                                 w.Write ((ulong) value);
122                                 break;
123                         }
124                 }
125
126                 internal static object DeserializeFromIndex (int index, BinaryReader r)
127                 {
128                         if (index == 15){
129                                 BinaryFormatter bf = new BinaryFormatter ();
130                                 return bf.Deserialize (r.BaseStream);
131                         }
132                         
133                         object value = null;
134                         switch (index) {
135                         case 1:
136                                 value = r.ReadString ();
137                                 break;
138                         case 2:
139                                 value = r.ReadInt32 ();
140                                 break;
141                         case 3:
142                                 value = r.ReadBoolean ();
143                                 break;
144                         case 4:
145                                 value = new DateTime (r.ReadInt64 ());
146                                 break;
147                         case 5:
148                                 value = r.ReadDecimal ();
149                                 break;
150                         case 6:
151                                 value = r.ReadByte ();
152                                 break;
153                         case 7:
154                                 value = r.ReadChar ();
155                                 break;
156                         case 8:
157                                 value = r.ReadSingle ();
158                                 break;
159                         case 9:
160                                 value = r.ReadDouble ();
161                                 break;
162                         case 10:
163                                 value = r.ReadInt16 ();
164                                 break;
165                         case 11:
166                                 value = r.ReadInt64 ();
167                                 break;
168                         case 12:
169                                 value = r.ReadUInt16 ();
170                                 break;
171                         case 13:
172                                 value = r.ReadUInt32 ();
173                                 break;
174                         case 14:
175                                 value = r.ReadUInt64 ();
176                                 break;
177                         }
178                         
179                         return value;
180                 }
181         }
182 }
183