2008-11-18 Marek Habersack <mhabersack@novell.com>
[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                 AltSerialization () { }
42
43                 internal static void Serialize (BinaryWriter w, object value)
44                 {
45                         TypeCode typeCode = value != null ? Type.GetTypeCode (value.GetType ()) : TypeCode.Empty;
46                         w.Write ((byte)typeCode);
47
48                         switch (typeCode) {
49                         case TypeCode.Boolean:
50                                 w.Write ((bool) value);
51                                 break;
52                         case TypeCode.Byte:
53                                 w.Write ((byte) value);
54                                 break;
55                         case TypeCode.Char:
56                                 w.Write ((char) value);
57                                 break;
58                         case TypeCode.DateTime:
59                                 w.Write (((DateTime) value).Ticks);
60                                 break;
61                         case TypeCode.DBNull:
62                                 break;
63                         case TypeCode.Decimal:
64                                 w.Write ((decimal) value);
65                                 break;
66                         case TypeCode.Double:
67                                 w.Write ((double) value);
68                                 break;
69                         case TypeCode.Empty:
70                                 break;
71                         case TypeCode.Int16:
72                                 w.Write ((short) value);
73                                 break;
74                         case TypeCode.Int32:
75                                 w.Write ((int) value);
76                                 break;
77                         case TypeCode.Int64:
78                                 w.Write ((long) value);
79                                 break;
80                         case TypeCode.Object:
81 #if TARGET_J2EE
82                                 if (w.BaseStream is java.io.ObjectOutput) {
83                                         ((java.io.ObjectOutput) w.BaseStream).writeObject (value);
84                                         return;
85                                 }
86 #endif
87                                 BinaryFormatter bf = new BinaryFormatter ();
88                                 bf.Serialize (w.BaseStream, value);
89                                 break;
90                         case TypeCode.SByte:
91                                 w.Write ((sbyte) value);
92                                 break;
93                         case TypeCode.Single:
94                                 w.Write ((float) value);
95                                 break;
96                         case TypeCode.String:
97                                 w.Write ((string) value);
98                                 break;
99                         case TypeCode.UInt16:
100                                 w.Write ((ushort) value);
101                                 break;
102                         case TypeCode.UInt32:
103                                 w.Write ((uint) value);
104                                 break;
105                         case TypeCode.UInt64:
106                                 w.Write ((ulong) value);
107                                 break;
108
109                         }
110                 }
111
112                 internal static object Deserialize (BinaryReader r)
113                 {
114                         TypeCode typeCode = (TypeCode)r.ReadByte();
115                         switch (typeCode) {
116                         case TypeCode.Boolean:
117                                 return r.ReadBoolean ();
118                         case TypeCode.Byte:
119                                 return r.ReadByte ();
120                         case TypeCode.Char:
121                                 return r.ReadChar ();
122                         case TypeCode.DateTime:
123                                 return new DateTime (r.ReadInt64 ());
124                         case TypeCode.DBNull:
125                                 return DBNull.Value;
126                         case TypeCode.Decimal:
127                                 return r.ReadDecimal ();
128                         case TypeCode.Double:
129                                 return r.ReadDouble ();
130                         case TypeCode.Empty:
131                                 return null;
132                         case TypeCode.Int16:
133                                 return r.ReadInt16 ();
134                         case TypeCode.Int32:
135                                 return r.ReadInt32 ();
136                         case TypeCode.Int64:
137                                 return r.ReadInt64 ();
138                         case TypeCode.Object:
139 #if TARGET_J2EE
140                                 if (r.BaseStream is java.io.ObjectInput)
141                                         return ((java.io.ObjectInput) r.BaseStream).readObject ();
142 #endif
143                                 BinaryFormatter bf = new BinaryFormatter ();
144                                 return bf.Deserialize (r.BaseStream);
145                         case TypeCode.SByte:
146                                 return r.ReadSByte ();
147                         case TypeCode.Single:
148                                 return r.ReadSingle ();
149                         case TypeCode.String:
150                                 return r.ReadString ();
151                         case TypeCode.UInt16:
152                                 return r.ReadUInt16 ();
153                         case TypeCode.UInt32:
154                                 return r.ReadUInt32 ();
155                         case TypeCode.UInt64:
156                                 return r.ReadUInt64 ();
157                         default:
158                                 throw new ArgumentOutOfRangeException ("TypeCode:" + typeCode);
159                         }
160                 }
161         }
162 }
163