|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Buffers; |
| 5 | + |
| 6 | +namespace System.Text.Json |
| 7 | +{ |
| 8 | + public sealed partial class Utf8JsonWriter |
| 9 | + { |
| 10 | + /// <summary> |
| 11 | + /// Writes the input as JSON content. |
| 12 | + /// </summary> |
| 13 | + /// <param name="json">The raw JSON content to write.</param> |
| 14 | + /// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param> |
| 15 | + public void WriteRawValue(string json, bool skipInputValidation = false) |
| 16 | + { |
| 17 | + if (json == null) |
| 18 | + { |
| 19 | + throw new ArgumentNullException(nameof(json)); |
| 20 | + } |
| 21 | + |
| 22 | + WriteRawValue(json.AsSpan(), skipInputValidation); |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Writes the input as JSON content. |
| 27 | + /// </summary> |
| 28 | + /// <param name="json">The raw JSON content to write.</param> |
| 29 | + /// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param> |
| 30 | + public void WriteRawValue(ReadOnlySpan<char> json, bool skipInputValidation = false) |
| 31 | + { |
| 32 | + byte[]? tempArray = null; |
| 33 | + |
| 34 | + // For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold. |
| 35 | + Span<byte> utf8Json = json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorWhileTranscoding) ? |
| 36 | + // Use a pooled alloc. |
| 37 | + tempArray = ArrayPool<byte>.Shared.Rent(json.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) : |
| 38 | + // Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per current implementation) |
| 39 | + // and by using a normal alloc we can avoid the Clear(). |
| 40 | + new byte[JsonReaderHelper.GetUtf8ByteCount(json)]; |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8Json); |
| 45 | + utf8Json = utf8Json.Slice(0, actualByteCount); |
| 46 | + WriteRawValue(utf8Json, skipInputValidation); |
| 47 | + } |
| 48 | + finally |
| 49 | + { |
| 50 | + if (tempArray != null) |
| 51 | + { |
| 52 | + utf8Json.Clear(); |
| 53 | + ArrayPool<byte>.Shared.Return(tempArray); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Writes the input as JSON content. |
| 60 | + /// </summary> |
| 61 | + /// <param name="utf8Json">The raw JSON content to write.</param> |
| 62 | + /// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param> |
| 63 | + public void WriteRawValue(ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false) |
| 64 | + { |
| 65 | + if (utf8Json.Length == 0) |
| 66 | + { |
| 67 | + ThrowHelper.ThrowArgumentException(SR.ExpectedJsonTokens); |
| 68 | + } |
| 69 | + |
| 70 | + if (!skipInputValidation) |
| 71 | + { |
| 72 | + Utf8JsonReader reader = new Utf8JsonReader(utf8Json); |
| 73 | + |
| 74 | + try |
| 75 | + { |
| 76 | + while (reader.Read()); |
| 77 | + } |
| 78 | + catch (JsonReaderException ex) |
| 79 | + { |
| 80 | + ThrowHelper.ThrowArgumentException(ex.Message); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + int maxRequired = utf8Json.Length + 1; // Optionally, 1 list separator |
| 85 | + |
| 86 | + if (_memory.Length - BytesPending < maxRequired) |
| 87 | + { |
| 88 | + Grow(maxRequired); |
| 89 | + } |
| 90 | + |
| 91 | + Span<byte> output = _memory.Span; |
| 92 | + |
| 93 | + if (_currentDepth < 0) |
| 94 | + { |
| 95 | + output[BytesPending++] = JsonConstants.ListSeparator; |
| 96 | + } |
| 97 | + |
| 98 | + utf8Json.CopyTo(output.Slice(BytesPending)); |
| 99 | + BytesPending += utf8Json.Length; |
| 100 | + |
| 101 | + SetFlagToAddListSeparatorBeforeNextItem(); |
| 102 | + |
| 103 | + // Treat all raw JSON value writes as string. |
| 104 | + _tokenType = JsonTokenType.String; |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments