2
0

AltSerialization.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // System.Web.Util.AltSerialization
  3. //
  4. // Author(s):
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Jackson Harper ([email protected])
  7. //
  8. // (C) 2003 Novell, Inc (http://www.novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.IO;
  32. using System.Collections;
  33. using System.Runtime.Serialization.Formatters.Binary;
  34. namespace System.Web.Util {
  35. internal sealed class AltSerialization {
  36. private static ArrayList types;
  37. internal static readonly int NullIndex = 16;
  38. private AltSerialization () { }
  39. static AltSerialization ()
  40. {
  41. types = new ArrayList ();
  42. types.Add ("");
  43. types.Add (typeof (string));
  44. types.Add (typeof (int));
  45. types.Add (typeof (bool));
  46. types.Add (typeof (DateTime));
  47. types.Add (typeof (Decimal));
  48. types.Add (typeof (Byte));
  49. types.Add (typeof (Char));
  50. types.Add (typeof (Single));
  51. types.Add (typeof (Double));
  52. types.Add (typeof (short));
  53. types.Add (typeof (long));
  54. types.Add (typeof (ushort));
  55. types.Add (typeof (uint));
  56. types.Add (typeof (ulong));
  57. }
  58. internal static void SerializeByType (BinaryWriter w, object value)
  59. {
  60. Type type = value.GetType ();
  61. int i = types.IndexOf (type);
  62. if (i == -1) {
  63. w.Write (15); // types.Count
  64. BinaryFormatter bf = new BinaryFormatter ();
  65. bf.Serialize (w.BaseStream, value);
  66. return;
  67. }
  68. w.Write (i);
  69. switch (i) {
  70. case 1:
  71. w.Write ((string) value);
  72. break;
  73. case 2:
  74. w.Write ((int) value);
  75. break;
  76. case 3:
  77. w.Write ((bool) value);
  78. break;
  79. case 4:
  80. w.Write (((DateTime) value).Ticks);
  81. break;
  82. case 5:
  83. w.Write ((decimal) value);
  84. break;
  85. case 6:
  86. w.Write ((byte) value);
  87. break;
  88. case 7:
  89. w.Write ((char) value);
  90. break;
  91. case 8:
  92. w.Write ((float) value);
  93. break;
  94. case 9:
  95. w.Write ((double) value);
  96. break;
  97. case 10:
  98. w.Write ((short) value);
  99. break;
  100. case 11:
  101. w.Write ((long) value);
  102. break;
  103. case 12:
  104. w.Write ((ushort) value);
  105. break;
  106. case 13:
  107. w.Write ((uint) value);
  108. break;
  109. case 14:
  110. w.Write ((ulong) value);
  111. break;
  112. }
  113. }
  114. internal static object DeserializeFromIndex (int index, BinaryReader r)
  115. {
  116. if (index == 15){
  117. BinaryFormatter bf = new BinaryFormatter ();
  118. return bf.Deserialize (r.BaseStream);
  119. }
  120. object value = null;
  121. switch (index) {
  122. case 1:
  123. value = r.ReadString ();
  124. break;
  125. case 2:
  126. value = r.ReadInt32 ();
  127. break;
  128. case 3:
  129. value = r.ReadBoolean ();
  130. break;
  131. case 4:
  132. value = new DateTime (r.ReadInt64 ());
  133. break;
  134. case 5:
  135. value = r.ReadDecimal ();
  136. break;
  137. case 6:
  138. value = r.ReadByte ();
  139. break;
  140. case 7:
  141. value = r.ReadChar ();
  142. break;
  143. case 8:
  144. value = r.ReadSingle ();
  145. break;
  146. case 9:
  147. value = r.ReadDouble ();
  148. break;
  149. case 10:
  150. value = r.ReadInt16 ();
  151. break;
  152. case 11:
  153. value = r.ReadInt64 ();
  154. break;
  155. case 12:
  156. value = r.ReadUInt16 ();
  157. break;
  158. case 13:
  159. value = r.ReadUInt32 ();
  160. break;
  161. case 14:
  162. value = r.ReadUInt64 ();
  163. break;
  164. }
  165. return value;
  166. }
  167. }
  168. }