internal JavaScriptDeserializer(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            Stream incomingStream = stream;
            if (!stream.CanSeek)
            {
                //Incoming stream if not seekable then read it in to memory.
                incomingStream = new BufferedStreamReader(stream);
            }

            Encoding streamEncoding = DetectEncoding(incomingStream.ReadByte(), incomingStream.ReadByte());
            //Move the stream back to 0
            incomingStream.Position = 0;
            //If the stream contains a BOM, StreamReader will detect it and override our encoding setting
            string input = new StreamReader(incomingStream, streamEncoding, true/*detectEncodingFromByteOrderMarks*/).ReadToEnd();

            if (string.IsNullOrEmpty(input))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExpectingElement, XmlDictionaryString.Empty, "root")));
            }

            _deserializer = new JavaScriptObjectDeserializer(input);
        }
Exemplo n.º 2
0
        public static JsonValue Load(TextReader textReader)
        {
            if (textReader == null)
                throw new ArgumentNullException("textReader");

            var ret = new JavaScriptObjectDeserializer(textReader.ReadToEnd(), true).BasicDeserialize();

            return ToJsonValue(ret);
        }