示例#1
0
 private void ValidateWritingArrayValues()
 {
     if (!_inObject)
     {
         JsonThrowHelper.ThrowJsonWriterException(_tokenType);    //TODO: Add resource message
     }
 }
示例#2
0
        private void ValidateEnd(byte token)
        {
            if (_bitStack.CurrentDepth <= 0)
            {
                JsonThrowHelper.ThrowJsonWriterException(token);    //TODO: Add resource message
            }
            if (token == JsonConstants.CloseBracket)
            {
                if (_inObject)
                {
                    Debug.Assert(_tokenType != JsonTokenType.None);
                    JsonThrowHelper.ThrowJsonWriterException(token);    //TODO: Add resource message
                }
            }
            else
            {
                Debug.Assert(token == JsonConstants.CloseBrace);

                if (!_inObject)
                {
                    JsonThrowHelper.ThrowJsonWriterException(token);    //TODO: Add resource message
                }
            }

            _inObject = _bitStack.Pop();
        }
示例#3
0
 private void ValidateWritingProperty()
 {
     if (!_inObject)
     {
         Debug.Assert(_tokenType != JsonTokenType.StartObject);
         JsonThrowHelper.ThrowJsonWriterException("Cannot add a property within an array or as the first JSON token.");    //TODO: Add resouce message
     }
 }
示例#4
0
        public void Flush(bool isFinalBlock = true)
        {
            //TODO: Fix exception message and check other potential conditions for invalid end.
            if (isFinalBlock && !_writerOptions.SkipValidation && CurrentDepth != 0)
            {
                JsonThrowHelper.ThrowJsonWriterException("Invalid end of JSON.");
            }

            Flush();
        }
示例#5
0
        private void ValidateStart(ref ReadOnlySpan <byte> propertyName, byte token)
        {
            if (JsonWriterHelper.IndexOfAnyEscape(propertyName) != -1)
            {
                JsonThrowHelper.ThrowJsonWriterException("Property name must be properly escaped."); //TODO: Fix message
            }
            if (!_inObject)
            {
                Debug.Assert(_tokenType != JsonTokenType.StartObject);
                JsonThrowHelper.ThrowJsonWriterException(token);    //TODO: Add resouce message
            }

            UpdateBitStackOnStart(token);
        }
示例#6
0
 private void ValidateWritingValue()
 {
     if (_inObject)
     {
         Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
         JsonThrowHelper.ThrowJsonWriterException(_tokenType);    //TODO: Add resource message
     }
     else
     {
         if (!_isNotPrimitive && _tokenType != JsonTokenType.None)
         {
             JsonThrowHelper.ThrowJsonWriterException(_tokenType);    //TODO: Add resource message
         }
     }
 }
示例#7
0
 private void ValidateStart(byte token)
 {
     if (_inObject)
     {
         Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
         JsonThrowHelper.ThrowJsonWriterException(token, _tokenType);
     }
     else
     {
         Debug.Assert(_tokenType != JsonTokenType.StartObject);
         if (_tokenType != JsonTokenType.None && !_isNotPrimitive)
         {
             JsonThrowHelper.ThrowJsonWriterException(token, _tokenType);
         }
     }
 }
示例#8
0
        private void WriteStart(byte token)
        {
            // TODO: Use throw helper with proper error messages
            if (CurrentDepth >= JsonConstants.MaxWriterDepth)
            {
                JsonThrowHelper.ThrowJsonWriterException("Depth too large.");
            }

            if (_writerOptions.SlowPath)
            {
                WriteStartSlow(token);
            }
            else
            {
                WriteStartMinimized(token);
            }

            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
            _currentDepth++;
            _isNotPrimitive = true;
        }
示例#9
0
        private void ValidateStart(byte token)
        {
            if (_inObject)
            {
                Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
                if (_tokenType != JsonTokenType.PropertyName)
                {
                    JsonThrowHelper.ThrowJsonWriterException(token, _tokenType);    //TODO: Add resource message
                }
            }
            else
            {
                Debug.Assert(_tokenType != JsonTokenType.StartObject);
                if (_tokenType == JsonTokenType.PropertyName || (_tokenType != JsonTokenType.None && !_isNotPrimitive))
                {
                    JsonThrowHelper.ThrowJsonWriterException(token, _tokenType);    //TODO: Add resource message
                }
            }

            UpdateBitStackOnStart(token);
        }
示例#10
0
        private static void EscapeNextChars(ref ReadOnlySpan <char> value, int firstChar, ref Span <char> destination, ref int consumed, ref int written)
        {
            int nextChar = -1;

            if (InRange(firstChar, 0xD800, 0xDFFF))
            {
                consumed++;
                if (value.Length <= consumed || firstChar >= 0xDC00)
                {
                    JsonThrowHelper.ThrowJsonWriterException("Invalid UTF-16 string ending in an invalid surrogate pair.");
                }

                nextChar = value[consumed];
                if (!InRange(nextChar, 0xDC00, 0xDFFF))
                {
                    JsonThrowHelper.ThrowJsonWriterException("Invalid UTF-16 string ending in an invalid surrogate pair.");
                }
            }

            destination[written++] = '\\';
            switch (firstChar)
            {
            case '\n':
                destination[written++] = 'n';
                break;

            case '\r':
                destination[written++] = 'r';
                break;

            case '\t':
                destination[written++] = 't';
                break;

            case '\\':
                destination[written++] = '\\';
                break;

            case '/':
                destination[written++] = '/';
                break;

            case '\b':
                destination[written++] = 'b';
                break;

            case '\f':
                destination[written++] = 'f';
                break;

            default:
                destination[written++] = 'u';
                WriteHex(firstChar, ref destination, ref written);
                if (nextChar != -1)
                {
                    destination[written++] = '\\';
                    destination[written++] = 'u';
                    WriteHex(nextChar, ref destination, ref written);
                }
                break;
            }
        }
示例#11
0
        private static int EscapeNextBytes(ReadOnlySpan <byte> value, ref Span <byte> destination, ref int written)
        {
            SequenceValidity status = Utf8Utility.PeekFirstSequence(value, out int numBytesConsumed, out UnicodeScalar unicodeScalar);

            if (status != SequenceValidity.WellFormed)
            {
                JsonThrowHelper.ThrowJsonWriterException("Invalid UTF-8 string.");
            }

            destination[written++] = (byte)'\\';
            int scalar = unicodeScalar.Value;

            switch (scalar)
            {
            case '\n':
                destination[written++] = (byte)'n';
                break;

            case '\r':
                destination[written++] = (byte)'r';
                break;

            case '\t':
                destination[written++] = (byte)'t';
                break;

            case '\\':
                destination[written++] = (byte)'\\';
                break;

            case '/':
                destination[written++] = (byte)'/';
                break;

            case '\b':
                destination[written++] = (byte)'b';
                break;

            case '\f':
                destination[written++] = (byte)'f';
                break;

            default:
                destination[written++] = (byte)'u';
                if (scalar < 0x10000)
                {
                    WriteHex(scalar, ref destination, ref written);
                }
                else
                {
                    int quotient  = DivMod(scalar - 0x10000, 0x400, out int remainder);
                    int firstChar = quotient + 0xD800;
                    int nextChar  = remainder + 0xDC00;
                    WriteHex(firstChar, ref destination, ref written);
                    destination[written++] = (byte)'\\';
                    destination[written++] = (byte)'u';
                    WriteHex(nextChar, ref destination, ref written);
                }
                break;
            }
            return(numBytesConsumed);
        }