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 AltSerialization () { }
43 internal static void Serialize (BinaryWriter w
, object value)
45 TypeCode typeCode
= value != null ? Type
.GetTypeCode (value.GetType ()) : TypeCode
.Empty
;
46 w
.Write ((byte)typeCode
);
49 case TypeCode
.Boolean
:
50 w
.Write ((bool) value);
53 w
.Write ((byte) value);
56 w
.Write ((char) value);
58 case TypeCode
.DateTime
:
59 w
.Write (((DateTime
) value).Ticks
);
63 case TypeCode
.Decimal
:
64 w
.Write ((decimal) value);
67 w
.Write ((double) value);
72 w
.Write ((short) value);
75 w
.Write ((int) value);
78 w
.Write ((long) value);
81 BinaryFormatter bf
= new BinaryFormatter ();
82 bf
.Serialize (w
.BaseStream
, value);
85 w
.Write ((sbyte) value);
88 w
.Write ((float) value);
91 w
.Write ((string) value);
94 w
.Write ((ushort) value);
97 w
.Write ((uint) value);
100 w
.Write ((ulong) value);
106 internal static object Deserialize (BinaryReader r
)
108 TypeCode typeCode
= (TypeCode
)r
.ReadByte();
110 case TypeCode
.Boolean
:
111 return r
.ReadBoolean ();
113 return r
.ReadByte ();
115 return r
.ReadChar ();
116 case TypeCode
.DateTime
:
117 return new DateTime (r
.ReadInt64 ());
118 case TypeCode
.DBNull
:
120 case TypeCode
.Decimal
:
121 return r
.ReadDecimal ();
122 case TypeCode
.Double
:
123 return r
.ReadDouble ();
127 return r
.ReadInt16 ();
129 return r
.ReadInt32 ();
131 return r
.ReadInt64 ();
132 case TypeCode
.Object
:
133 BinaryFormatter bf
= new BinaryFormatter ();
134 return bf
.Deserialize (r
.BaseStream
);
136 return r
.ReadSByte ();
137 case TypeCode
.Single
:
138 return r
.ReadSingle ();
139 case TypeCode
.String
:
140 return r
.ReadString ();
141 case TypeCode
.UInt16
:
142 return r
.ReadUInt16 ();
143 case TypeCode
.UInt32
:
144 return r
.ReadUInt32 ();
145 case TypeCode
.UInt64
:
146 return r
.ReadUInt64 ();
148 throw new ArgumentOutOfRangeException ("TypeCode:" + typeCode
);