Newtonsoft.Json.JsonTextReader.ReadAsBytes C# (CSharp) Method

ReadAsBytes() public method

Reads the next JSON token from the stream as a Byte[].
public ReadAsBytes ( ) : byte[]
return byte[]
        public override byte[] ReadAsBytes()
        {
            EnsureBuffer();
            bool isWrapped = false;

            switch (_currentState)
            {
                case State.Start:
                case State.Property:
                case State.Array:
                case State.ArrayStart:
                case State.Constructor:
                case State.ConstructorStart:
                case State.PostValue:
                    while (true)
                    {
                        char currentChar = _chars[_charPos];

                        switch (currentChar)
                        {
                            case '\0':
                                if (ReadNullChar())
                                {
                                    SetToken(JsonToken.None, null, false);
                                    return null;
                                }
                                break;
                            case '"':
                            case '\'':
                                ParseString(currentChar, ReadType.ReadAsBytes);
                                byte[] data = (byte[])Value;
                                if (isWrapped)
                                {
                                    ReaderReadAndAssert();
                                    if (TokenType != JsonToken.EndObject)
                                    {
                                        throw JsonReaderException.Create(this, "Error reading bytes. Unexpected token: {0}.".FormatWith(CultureInfo.InvariantCulture, TokenType));
                                    }
                                    SetToken(JsonToken.Bytes, data, false);
                                }
                                return data;
                            case '{':
                                _charPos++;
                                SetToken(JsonToken.StartObject);
                                ReadIntoWrappedTypeObject();
                                isWrapped = true;
                                break;
                            case '[':
                                _charPos++;
                                SetToken(JsonToken.StartArray);
                                return ReadArrayIntoByteArray();
                            case 'n':
                                HandleNull();
                                return null;
                            case '/':
                                ParseComment(false);
                                break;
                            case ',':
                                ProcessValueComma();
                                break;
                            case ']':
                                _charPos++;
                                if (_currentState == State.Array || _currentState == State.ArrayStart || _currentState == State.PostValue)
                                {
                                    SetToken(JsonToken.EndArray);
                                    return null;
                                }
                                throw CreateUnexpectedCharacterException(currentChar);
                            case StringUtils.CarriageReturn:
                                ProcessCarriageReturn(false);
                                break;
                            case StringUtils.LineFeed:
                                ProcessLineFeed();
                                break;
                            case ' ':
                            case StringUtils.Tab:
                                // eat
                                _charPos++;
                                break;
                            default:
                                _charPos++;

                                if (!char.IsWhiteSpace(currentChar))
                                {
                                    throw CreateUnexpectedCharacterException(currentChar);
                                }

                                // eat
                                break;
                        }
                    }
                case State.Finished:
                    ReadFinished();
                    return null;
                default:
                    throw JsonReaderException.Create(this, "Unexpected state: {0}.".FormatWith(CultureInfo.InvariantCulture, CurrentState));
            }
        }

Usage Example

コード例 #1
0
 public void ReadBytesWithBadCharacter()
 {
   JsonReader reader = new JsonTextReader(new StringReader(@"true"));
   reader.ReadAsBytes();
 }
All Usage Examples Of Newtonsoft.Json.JsonTextReader::ReadAsBytes