AltSerialization.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #if TARGET_J2EE
  76. if (w.BaseStream is java.io.ObjectOutput) {
  77. ((java.io.ObjectOutput) w.BaseStream).writeObject (value);
  78. return;
  79. }
  80. #endif
  81. BinaryFormatter bf = new BinaryFormatter ();
  82. bf.Serialize (w.BaseStream, value);
  83. break;
  84. case TypeCode.SByte:
  85. w.Write ((sbyte) value);
  86. break;
  87. case TypeCode.Single:
  88. w.Write ((float) value);
  89. break;
  90. case TypeCode.String:
  91. w.Write ((string) value);
  92. break;
  93. case TypeCode.UInt16:
  94. w.Write ((ushort) value);
  95. break;
  96. case TypeCode.UInt32:
  97. w.Write ((uint) value);
  98. break;
  99. case TypeCode.UInt64:
  100. w.Write ((ulong) value);
  101. break;
  102. }
  103. }
  104. internal static object Deserialize (BinaryReader r)
  105. {
  106. TypeCode typeCode = (TypeCode)r.ReadByte();
  107. switch (typeCode) {
  108. case TypeCode.Boolean:
  109. return r.ReadBoolean ();
  110. case TypeCode.Byte:
  111. return r.ReadByte ();
  112. case TypeCode.Char:
  113. return r.ReadChar ();
  114. case TypeCode.DateTime:
  115. return new DateTime (r.ReadInt64 ());
  116. case TypeCode.DBNull:
  117. return DBNull.Value;
  118. case TypeCode.Decimal:
  119. return r.ReadDecimal ();
  120. case TypeCode.Double:
  121. return r.ReadDouble ();
  122. case TypeCode.Empty:
  123. return null;
  124. case TypeCode.Int16:
  125. return r.ReadInt16 ();
  126. case TypeCode.Int32:
  127. return r.ReadInt32 ();
  128. case TypeCode.Int64:
  129. return r.ReadInt64 ();
  130. case TypeCode.Object:
  131. #if TARGET_J2EE
  132. if (r.BaseStream is java.io.ObjectInput)
  133. return ((java.io.ObjectInput) r.BaseStream).readObject ();
  134. #endif
  135. BinaryFormatter bf = new BinaryFormatter ();
  136. return bf.Deserialize (r.BaseStream);
  137. case TypeCode.SByte:
  138. return r.ReadSByte ();
  139. case TypeCode.Single:
  140. return r.ReadSingle ();
  141. case TypeCode.String:
  142. return r.ReadString ();
  143. case TypeCode.UInt16:
  144. return r.ReadUInt16 ();
  145. case TypeCode.UInt32:
  146. return r.ReadUInt32 ();
  147. case TypeCode.UInt64:
  148. return r.ReadUInt64 ();
  149. default:
  150. throw new ArgumentOutOfRangeException ("TypeCode:" + typeCode);
  151. }
  152. }
  153. }
  154. }