AltSerialization.cs 4.0 KB

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