JavaScriptObjectDeserializer.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Text;
  7. using System.Xml;
  8. namespace System.Runtime.Serialization.Json
  9. {
  10. internal partial class JavaScriptObjectDeserializer
  11. {
  12. #region stream/reader classes
  13. public class BufferedStreamReader : StreamReader
  14. {
  15. public BufferedStreamReader (Stream stream)
  16. : base (stream, JavaScriptObjectDeserializer.DetectEncoding (stream.ReadByte (), stream.ReadByte ()))
  17. {
  18. }
  19. }
  20. class BufferedStream : Stream
  21. {
  22. int first, second;
  23. long pos;
  24. Stream source;
  25. public BufferedStream (Stream source)
  26. {
  27. this.source = source;
  28. first = source.ReadByte ();
  29. second = source.ReadByte ();
  30. }
  31. public override int Read (byte [] buffer, int index, int count)
  32. {
  33. if (buffer == null)
  34. throw new ArgumentNullException ("buffer");
  35. if (index < 0 || index >= buffer.Length)
  36. throw new ArgumentOutOfRangeException ("index");
  37. if (count < 0 || index + count >= buffer.Length)
  38. throw new ArgumentOutOfRangeException ("count");
  39. if (count == 0)
  40. return 0;
  41. if (pos < 2) {
  42. buffer [pos] = pos == 0 ? (byte) first : (byte) second;
  43. pos++;
  44. return Read (buffer, index + 1, count - 1) + 1;
  45. }
  46. return source.Read (buffer, index, count);
  47. }
  48. public override int ReadByte ()
  49. {
  50. switch (pos) {
  51. case 0:
  52. pos++;
  53. return first;
  54. case 1:
  55. pos++;
  56. return second;
  57. default:
  58. return source.ReadByte ();
  59. }
  60. }
  61. public override bool CanRead {
  62. get { return source.CanRead; }
  63. }
  64. public override bool CanSeek {
  65. get { return false; }
  66. }
  67. public override bool CanWrite {
  68. get { return false; }
  69. }
  70. public override long Length {
  71. get { return source.Length; }
  72. }
  73. public override long Position {
  74. get { return source.Position; }
  75. set {
  76. if (value < 2)
  77. pos = value;
  78. source.Position = value;
  79. }
  80. }
  81. public override long Seek (long pos, SeekOrigin origin)
  82. {
  83. throw new NotSupportedException ();
  84. }
  85. public override void SetLength (long pos)
  86. {
  87. throw new NotSupportedException ();
  88. }
  89. public override void Write (byte [] buf, int index, int count)
  90. {
  91. throw new NotSupportedException ();
  92. }
  93. public override void Flush ()
  94. {
  95. // do nothing
  96. }
  97. }
  98. #endregion
  99. public JavaScriptObjectDeserializer (string json, bool raiseNumberParseError)
  100. {
  101. reader = new JavaScriptReader (new StringReader (json), raiseNumberParseError);
  102. }
  103. JavaScriptReader reader;
  104. public object BasicDeserialize ()
  105. {
  106. return reader.Read ();
  107. }
  108. public static Encoding DetectEncoding (int byte1, int byte2)
  109. {
  110. if (byte1 == 0) {
  111. if (byte2 == 0)
  112. throw new XmlException ("UTF-32BE is detected, which is not supported");
  113. else
  114. return Encoding.BigEndianUnicode;
  115. } else {
  116. if (byte2 == 0) // could be UTF-32LE, but there is no way to detect that only within two bytes.
  117. return Encoding.Unicode;
  118. else
  119. return Encoding.UTF8;
  120. }
  121. }
  122. }
  123. }