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