AltSerialization.cs 4.3 KB

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