Esempio n. 1
0
 public static void ValidateValue(ReadOnlySpan <char> value)
 {
     if (value.Length > JsonConstants.MaxCharacterTokenSize)
     {
         ThrowHelper.ThrowArgumentException_ValueTooLarge(value.Length);
     }
 }
Esempio n. 2
0
 public static void ValidateBytes(ReadOnlySpan <byte> bytes)
 {
     if (bytes.Length > JsonConstants.MaxBase46ValueTokenSize)
     {
         ThrowHelper.ThrowArgumentException_ValueTooLarge(bytes.Length);
     }
 }
Esempio n. 3
0
 public static void ValidateValue(ReadOnlySpan <byte> value)
 {
     if (value.Length > JsonConstants.MaxUnescapedTokenSize)
     {
         ThrowHelper.ThrowArgumentException_ValueTooLarge(value.Length);
     }
 }
Esempio n. 4
0
        private void TranscodeAndWriteRawValue(ReadOnlySpan <char> json, bool skipInputValidation)
        {
            if (json.Length > JsonConstants.MaxUtf16RawValueLength)
            {
                ThrowHelper.ThrowArgumentException_ValueTooLarge(json.Length);
            }

            byte[]? tempArray = null;

            // For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
            Span <byte> utf8Json = json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorWhileTranscoding) ?
                                   // Use a pooled alloc.
                                   tempArray = ArrayPool <byte> .Shared.Rent(json.Length *JsonConstants.MaxExpansionFactorWhileTranscoding) :
                                   // Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per current implementation)
                                   // and by using a normal alloc we can avoid the Clear().
                                               new byte[JsonReaderHelper.GetUtf8ByteCount(json)];

            try
            {
                int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8Json);
                utf8Json = utf8Json.Slice(0, actualByteCount);
                WriteRawValueCore(utf8Json, skipInputValidation);
            }
            finally
            {
                if (tempArray != null)
                {
                    utf8Json.Clear();
                    ArrayPool <byte> .Shared.Return(tempArray);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Writes the input as JSON content. It is expected that the input content is a single complete JSON value.
        /// </summary>
        /// <param name="utf8Json">The raw JSON content to write.</param>
        /// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</param>
        /// <exception cref="ArgumentException">Thrown if the length of the input is zero or equal to <see cref="int.MaxValue"/>.</exception>
        /// <exception cref="JsonException">
        /// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
        /// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
        /// or the input JSON exceeds a recursive depth of 64.
        /// </exception>
        /// <remarks>
        /// When writing untrused JSON values, do not set <paramref name="skipInputValidation"/> to <see langword="true"/> as this can result in invalid JSON
        /// being written, and/or the overall payload being written to the writer instance being invalid.
        ///
        /// When using this method, the input content will be written to the writer destination as-is, unless validation fails (when it is enabled).
        ///
        /// The <see cref="JsonWriterOptions.SkipValidation"/> value for the writer instance is honored when using this method.
        ///
        /// The <see cref="JsonWriterOptions.Indented"/> and <see cref="JsonWriterOptions.Encoder"/> values for the writer instance are not applied when using this method.
        /// </remarks>
        public void WriteRawValue(ReadOnlySpan <byte> utf8Json, bool skipInputValidation = false)
        {
            if (!_options.SkipValidation)
            {
                ValidateWritingValue();
            }

            if (utf8Json.Length == int.MaxValue)
            {
                ThrowHelper.ThrowArgumentException_ValueTooLarge(int.MaxValue);
            }

            WriteRawValueCore(utf8Json, skipInputValidation);
        }