Пример #1
0
 private void SkipWhitespace()
 {
     while (Utf8String.IsWhiteSpace(_values[_valuesIndex]))
     {
         _valuesIndex++;
     }
 }
Пример #2
0
        private void SkipEmpty()
        {
            var nextByte = (byte)_str[_index];

            while (Utf8String.IsWhiteSpace(nextByte))
            {
                _index++;
                nextByte = (byte)_str[_index];
            }
        }
Пример #3
0
        public JsonDb.JsonValueType GetJsonDb.JsonValueType()
        {
            var nextByte = (byte)_str[_index];

            while (Utf8String.IsWhiteSpace(nextByte))
            {
                _index++;
                nextByte = (byte)_str[_index];
            }

            if (nextByte == '"')
            {
                return(JsonDb.JsonValueType.String);
            }

            if (nextByte == '{')
            {
                return(JsonDb.JsonValueType.Object);
            }

            if (nextByte == '[')
            {
                return(JsonDb.JsonValueType.Array);
            }

            if (nextByte == 't')
            {
                return(JsonDb.JsonValueType.True);
            }

            if (nextByte == 'f')
            {
                return(JsonDb.JsonValueType.False);
            }

            if (nextByte == 'n')
            {
                return(JsonDb.JsonValueType.Null);
            }

            if (nextByte == '-' || (nextByte >= '0' && nextByte <= '9'))
            {
                return(JsonDb.JsonValueType.Number);
            }

            throw new FormatException("Invalid json, tried to read char '" + nextByte + "'.");
        }
Пример #4
0
        public JsonParser(byte[] buffer, int lengthOfJson)
        {
            _buffer       = buffer;
            _insideObject = 0;
            _insideArray  = 0;
            TokenType     = 0;
            _index        = 0;
            _end          = lengthOfJson;

            var nextByte = _buffer[_index];

            while (Utf8String.IsWhiteSpace(nextByte) || nextByte == 0)
            {
                _index++;
                nextByte = _buffer[_index];
            }

            _dbIndex = _end + 1;

            _jsonStartIsObject = _buffer[_index] == '{';
        }
Пример #5
0
        private void MoveToNextTokenType()
        {
            var nextByte = (byte)_str[_index];

            while (Utf8String.IsWhiteSpace(nextByte))
            {
                _index++;
                nextByte = (byte)_str[_index];
            }

            switch (TokenType)
            {
            case JsonTokenType.ObjectStart:
                if (nextByte != '}')
                {
                    TokenType = JsonTokenType.Property;
                    return;
                }
                break;

            case JsonTokenType.ObjectEnd:
                if (nextByte == ',')
                {
                    _index++;
                    if (_insideObject == _insideArray)
                    {
                        TokenType = !_jsonStartIsObject ? JsonTokenType.Property : JsonTokenType.Value;
                        return;
                    }
                    TokenType = _insideObject > _insideArray ? JsonTokenType.Property : JsonTokenType.Value;
                    return;
                }
                break;

            case JsonTokenType.ArrayStart:
                if (nextByte != ']')
                {
                    TokenType = JsonTokenType.Value;
                    return;
                }
                break;

            case JsonTokenType.ArrayEnd:
                if (nextByte == ',')
                {
                    _index++;
                    if (_insideObject == _insideArray)
                    {
                        TokenType = !_jsonStartIsObject ? JsonTokenType.Property : JsonTokenType.Value;
                        return;
                    }
                    TokenType = _insideObject > _insideArray ? JsonTokenType.Property : JsonTokenType.Value;
                    return;
                }
                break;

            case JsonTokenType.Property:
                if (nextByte == ',')
                {
                    _index++;
                    return;
                }
                break;

            case JsonTokenType.Value:
                if (nextByte == ',')
                {
                    _index++;
                    return;
                }
                break;
            }

            _index++;
            switch (nextByte)
            {
            case (byte)'{':
                _insideObject++;
                TokenType = JsonTokenType.ObjectStart;
                return;

            case (byte)'}':
                _insideObject--;
                TokenType = JsonTokenType.ObjectEnd;
                return;

            case (byte)'[':
                _insideArray++;
                TokenType = JsonTokenType.ArrayStart;
                return;

            case (byte)']':
                _insideArray--;
                TokenType = JsonTokenType.ArrayEnd;
                return;

            default:
                throw new FormatException("Unable to get next token type. Check json format.");
            }
        }