| | | 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 | | using System.Diagnostics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | |
| | | 8 | | namespace System.Text.Json |
| | | 9 | | { |
| | | 10 | | public sealed partial class Utf8JsonWriter |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Writes the pre-encoded property name (as a JSON string) as the first part of a name/value pair of a JSON obj |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 16 | | /// <exception cref="InvalidOperationException"> |
| | | 17 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 18 | | /// </exception> |
| | | 19 | | public void WritePropertyName(JsonEncodedText propertyName) |
| | 0 | 20 | | => WritePropertyNameHelper(propertyName.EncodedUtf8Bytes); |
| | | 21 | | |
| | | 22 | | internal void WritePropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection) |
| | 0 | 23 | | { |
| | 0 | 24 | | if (_options.Indented) |
| | 0 | 25 | | { |
| | 0 | 26 | | ReadOnlySpan<byte> escapedPropertyName = |
| | 0 | 27 | | escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3); |
| | | 28 | | |
| | 0 | 29 | | WritePropertyNameHelper(escapedPropertyName); |
| | 0 | 30 | | } |
| | | 31 | | else |
| | 0 | 32 | | { |
| | 0 | 33 | | Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxUnescapedTokenSize - 3); |
| | | 34 | | |
| | 0 | 35 | | WriteStringPropertyNameSection(escapedPropertyNameSection); |
| | | 36 | | |
| | 0 | 37 | | _currentDepth &= JsonConstants.RemoveFlagsBitMask; |
| | 0 | 38 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 39 | | _commentAfterNoneOrPropertyName = false; |
| | 0 | 40 | | } |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | private void WritePropertyNameHelper(ReadOnlySpan<byte> utf8PropertyName) |
| | 0 | 44 | | { |
| | 0 | 45 | | Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | | 46 | | |
| | 0 | 47 | | WriteStringByOptionsPropertyName(utf8PropertyName); |
| | | 48 | | |
| | 0 | 49 | | _currentDepth &= JsonConstants.RemoveFlagsBitMask; |
| | 0 | 50 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 51 | | _commentAfterNoneOrPropertyName = false; |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Writes the property name (as a JSON string) as the first part of a name/value pair of a JSON object. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 58 | | /// <exception cref="ArgumentException"> |
| | | 59 | | /// Thrown when the specified property name is too large. |
| | | 60 | | /// </exception> |
| | | 61 | | /// <exception cref="ArgumentNullException"> |
| | | 62 | | /// The <paramref name="propertyName"/> parameter is <see langword="null"/>. |
| | | 63 | | /// </exception> |
| | | 64 | | /// <exception cref="InvalidOperationException"> |
| | | 65 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 66 | | /// </exception> |
| | | 67 | | /// <remarks> |
| | | 68 | | /// The property name is escaped before writing. |
| | | 69 | | /// </remarks> |
| | | 70 | | public void WritePropertyName(string propertyName) |
| | 0 | 71 | | { |
| | 0 | 72 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | 0 | 73 | | WritePropertyName(propertyName.AsSpan()); |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Writes the property name (as a JSON string) as the first part of a name/value pair of a JSON object. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 80 | | /// <exception cref="ArgumentException"> |
| | | 81 | | /// Thrown when the specified property name is too large. |
| | | 82 | | /// </exception> |
| | | 83 | | /// <exception cref="InvalidOperationException"> |
| | | 84 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 85 | | /// </exception> |
| | | 86 | | /// <remarks> |
| | | 87 | | /// The property name is escaped before writing. |
| | | 88 | | /// </remarks> |
| | | 89 | | public void WritePropertyName(ReadOnlySpan<char> propertyName) |
| | 0 | 90 | | { |
| | 0 | 91 | | JsonWriterHelper.ValidateProperty(propertyName); |
| | | 92 | | |
| | 0 | 93 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); |
| | | 94 | | |
| | 0 | 95 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 96 | | |
| | 0 | 97 | | if (propertyIdx != -1) |
| | 0 | 98 | | { |
| | 0 | 99 | | WriteStringEscapeProperty(propertyName, propertyIdx); |
| | 0 | 100 | | } |
| | | 101 | | else |
| | 0 | 102 | | { |
| | 0 | 103 | | WriteStringByOptionsPropertyName(propertyName); |
| | 0 | 104 | | } |
| | 0 | 105 | | _currentDepth &= JsonConstants.RemoveFlagsBitMask; |
| | 0 | 106 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 107 | | _commentAfterNoneOrPropertyName = false; |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | private void WriteStringEscapeProperty(scoped ReadOnlySpan<char> propertyName, int firstEscapeIndexProp) |
| | 0 | 111 | | { |
| | 0 | 112 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); |
| | | 113 | | |
| | 0 | 114 | | char[]? propertyArray = null; |
| | | 115 | | scoped Span<char> escapedPropertyName; |
| | | 116 | | |
| | 0 | 117 | | if (firstEscapeIndexProp != -1) |
| | 0 | 118 | | { |
| | 0 | 119 | | int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); |
| | | 120 | | |
| | 0 | 121 | | if (length > JsonConstants.StackallocCharThreshold) |
| | 0 | 122 | | { |
| | 0 | 123 | | propertyArray = ArrayPool<char>.Shared.Rent(length); |
| | 0 | 124 | | escapedPropertyName = propertyArray; |
| | 0 | 125 | | } |
| | | 126 | | else |
| | 0 | 127 | | { |
| | 0 | 128 | | escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold]; |
| | 0 | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, |
| | 0 | 132 | | propertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 133 | | } |
| | | 134 | | |
| | 0 | 135 | | WriteStringByOptionsPropertyName(propertyName); |
| | | 136 | | |
| | 0 | 137 | | if (propertyArray != null) |
| | 0 | 138 | | { |
| | 0 | 139 | | ArrayPool<char>.Shared.Return(propertyArray); |
| | 0 | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | |
| | | 143 | | private void WriteStringByOptionsPropertyName(ReadOnlySpan<char> propertyName) |
| | 0 | 144 | | { |
| | 0 | 145 | | ValidateWritingProperty(); |
| | 0 | 146 | | if (_options.Indented) |
| | 0 | 147 | | { |
| | 0 | 148 | | WriteStringIndentedPropertyName(propertyName); |
| | 0 | 149 | | } |
| | | 150 | | else |
| | 0 | 151 | | { |
| | 0 | 152 | | WriteStringMinimizedPropertyName(propertyName); |
| | 0 | 153 | | } |
| | 0 | 154 | | } |
| | | 155 | | |
| | | 156 | | private void WriteStringMinimizedPropertyName(ReadOnlySpan<char> escapedPropertyName) |
| | 0 | 157 | | { |
| | 0 | 158 | | Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 159 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue - 4) / JsonConstants.MaxExpansionFactorWhileTranscod |
| | | 160 | | |
| | | 161 | | // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + 3 |
| | | 162 | | // Optionally, 1 list separator, and up to 3x growth when transcoding |
| | 0 | 163 | | int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 4; |
| | | 164 | | |
| | 0 | 165 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 166 | | { |
| | 0 | 167 | | Grow(maxRequired); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | Span<byte> output = _memory.Span; |
| | | 171 | | |
| | 0 | 172 | | if (_currentDepth < 0) |
| | 0 | 173 | | { |
| | 0 | 174 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 175 | | } |
| | 0 | 176 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 177 | | |
| | 0 | 178 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 179 | | |
| | 0 | 180 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 181 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | private void WriteStringIndentedPropertyName(ReadOnlySpan<char> escapedPropertyName) |
| | 0 | 185 | | { |
| | 0 | 186 | | int indent = Indentation; |
| | 0 | 187 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 188 | | |
| | 0 | 189 | | Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 190 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue - 5 - indent - _newLineLength) / JsonConstants.MaxEx |
| | | 191 | | |
| | | 192 | | // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + 4 |
| | | 193 | | // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding |
| | 0 | 194 | | int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + |
| | | 195 | | |
| | 0 | 196 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 197 | | { |
| | 0 | 198 | | Grow(maxRequired); |
| | 0 | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | Span<byte> output = _memory.Span; |
| | | 202 | | |
| | 0 | 203 | | if (_currentDepth < 0) |
| | 0 | 204 | | { |
| | 0 | 205 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 209 | | { |
| | 0 | 210 | | WriteNewLine(output); |
| | 0 | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 214 | | BytesPending += indent; |
| | | 215 | | |
| | 0 | 216 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 217 | | |
| | 0 | 218 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 219 | | |
| | 0 | 220 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 221 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 222 | | output[BytesPending++] = JsonConstants.Space; |
| | 0 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Writes the UTF-8 property name (as a JSON string) as the first part of a name/value pair of a JSON object. |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param> |
| | | 229 | | /// <exception cref="ArgumentException"> |
| | | 230 | | /// Thrown when the specified property name is too large. |
| | | 231 | | /// </exception> |
| | | 232 | | /// <exception cref="InvalidOperationException"> |
| | | 233 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 234 | | /// </exception> |
| | | 235 | | /// <remarks> |
| | | 236 | | /// The property name is escaped before writing. |
| | | 237 | | /// </remarks> |
| | | 238 | | public void WritePropertyName(ReadOnlySpan<byte> utf8PropertyName) |
| | 0 | 239 | | { |
| | 0 | 240 | | JsonWriterHelper.ValidateProperty(utf8PropertyName); |
| | | 241 | | |
| | 0 | 242 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); |
| | | 243 | | |
| | 0 | 244 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 245 | | |
| | 0 | 246 | | if (propertyIdx != -1) |
| | 0 | 247 | | { |
| | 0 | 248 | | WriteStringEscapeProperty(utf8PropertyName, propertyIdx); |
| | 0 | 249 | | } |
| | | 250 | | else |
| | 0 | 251 | | { |
| | 0 | 252 | | WriteStringByOptionsPropertyName(utf8PropertyName); |
| | 0 | 253 | | } |
| | 0 | 254 | | _currentDepth &= JsonConstants.RemoveFlagsBitMask; |
| | 0 | 255 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 256 | | _commentAfterNoneOrPropertyName = false; |
| | 0 | 257 | | } |
| | | 258 | | |
| | | 259 | | private void WritePropertyNameUnescaped(ReadOnlySpan<byte> utf8PropertyName) |
| | 0 | 260 | | { |
| | 0 | 261 | | JsonWriterHelper.ValidateProperty(utf8PropertyName); |
| | 0 | 262 | | WriteStringByOptionsPropertyName(utf8PropertyName); |
| | | 263 | | |
| | 0 | 264 | | _currentDepth &= JsonConstants.RemoveFlagsBitMask; |
| | 0 | 265 | | _tokenType = JsonTokenType.PropertyName; |
| | 0 | 266 | | _commentAfterNoneOrPropertyName = false; |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | private void WriteStringEscapeProperty(scoped ReadOnlySpan<byte> utf8PropertyName, int firstEscapeIndexProp) |
| | 0 | 270 | | { |
| | 0 | 271 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); |
| | | 272 | | |
| | 0 | 273 | | byte[]? propertyArray = null; |
| | | 274 | | scoped Span<byte> escapedPropertyName; |
| | | 275 | | |
| | 0 | 276 | | if (firstEscapeIndexProp != -1) |
| | 0 | 277 | | { |
| | 0 | 278 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); |
| | | 279 | | |
| | 0 | 280 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 281 | | { |
| | 0 | 282 | | propertyArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 283 | | escapedPropertyName = propertyArray; |
| | 0 | 284 | | } |
| | | 285 | | else |
| | 0 | 286 | | { |
| | 0 | 287 | | escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco |
| | 0 | 291 | | utf8PropertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 292 | | } |
| | | 293 | | |
| | 0 | 294 | | WriteStringByOptionsPropertyName(utf8PropertyName); |
| | | 295 | | |
| | 0 | 296 | | if (propertyArray != null) |
| | 0 | 297 | | { |
| | 0 | 298 | | ArrayPool<byte>.Shared.Return(propertyArray); |
| | 0 | 299 | | } |
| | 0 | 300 | | } |
| | | 301 | | |
| | | 302 | | private void WriteStringByOptionsPropertyName(ReadOnlySpan<byte> utf8PropertyName) |
| | 0 | 303 | | { |
| | 0 | 304 | | ValidateWritingProperty(); |
| | 0 | 305 | | if (_options.Indented) |
| | 0 | 306 | | { |
| | 0 | 307 | | WriteStringIndentedPropertyName(utf8PropertyName); |
| | 0 | 308 | | } |
| | | 309 | | else |
| | 0 | 310 | | { |
| | 0 | 311 | | WriteStringMinimizedPropertyName(utf8PropertyName); |
| | 0 | 312 | | } |
| | 0 | 313 | | } |
| | | 314 | | |
| | | 315 | | // AggressiveInlining used since this is only called from one location. |
| | | 316 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 317 | | private void WriteStringMinimizedPropertyName(ReadOnlySpan<byte> escapedPropertyName) |
| | 0 | 318 | | { |
| | 0 | 319 | | Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 320 | | Debug.Assert(escapedPropertyName.Length < int.MaxValue - 4); |
| | | 321 | | |
| | 0 | 322 | | int minRequired = escapedPropertyName.Length + 3; // 2 quotes for property name, and 1 colon |
| | 0 | 323 | | int maxRequired = minRequired + 1; // Optionally, 1 list separator |
| | | 324 | | |
| | 0 | 325 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 326 | | { |
| | 0 | 327 | | Grow(maxRequired); |
| | 0 | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | Span<byte> output = _memory.Span; |
| | | 331 | | |
| | 0 | 332 | | if (_currentDepth < 0) |
| | 0 | 333 | | { |
| | 0 | 334 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 335 | | } |
| | 0 | 336 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 337 | | |
| | 0 | 338 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 339 | | BytesPending += escapedPropertyName.Length; |
| | | 340 | | |
| | 0 | 341 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 342 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 343 | | } |
| | | 344 | | |
| | | 345 | | // AggressiveInlining used since this is only called from one location. |
| | | 346 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 347 | | private void WriteStringPropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection) |
| | 0 | 348 | | { |
| | 0 | 349 | | Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxEscapedTokenSize - 3); |
| | 0 | 350 | | Debug.Assert(escapedPropertyNameSection.Length < int.MaxValue - 4); |
| | | 351 | | |
| | 0 | 352 | | int maxRequired = escapedPropertyNameSection.Length + 1; // Optionally, 1 list separator |
| | | 353 | | |
| | 0 | 354 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 355 | | { |
| | 0 | 356 | | Grow(maxRequired); |
| | 0 | 357 | | } |
| | | 358 | | |
| | 0 | 359 | | Span<byte> output = _memory.Span; |
| | | 360 | | |
| | 0 | 361 | | if (_currentDepth < 0) |
| | 0 | 362 | | { |
| | 0 | 363 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | escapedPropertyNameSection.CopyTo(output.Slice(BytesPending)); |
| | 0 | 367 | | BytesPending += escapedPropertyNameSection.Length; |
| | 0 | 368 | | } |
| | | 369 | | |
| | | 370 | | // AggressiveInlining used since this is only called from one location. |
| | | 371 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 372 | | private void WriteStringIndentedPropertyName(ReadOnlySpan<byte> escapedPropertyName) |
| | 0 | 373 | | { |
| | 0 | 374 | | int indent = Indentation; |
| | 0 | 375 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 376 | | |
| | 0 | 377 | | Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 378 | | Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - 5 - _newLineLength); |
| | | 379 | | |
| | 0 | 380 | | int minRequired = indent + escapedPropertyName.Length + 4; // 2 quotes for property name, 1 colon, and 1 spa |
| | 0 | 381 | | int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li |
| | | 382 | | |
| | 0 | 383 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 384 | | { |
| | 0 | 385 | | Grow(maxRequired); |
| | 0 | 386 | | } |
| | | 387 | | |
| | 0 | 388 | | Span<byte> output = _memory.Span; |
| | | 389 | | |
| | 0 | 390 | | if (_currentDepth < 0) |
| | 0 | 391 | | { |
| | 0 | 392 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 393 | | } |
| | | 394 | | |
| | 0 | 395 | | Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName); |
| | | 396 | | |
| | 0 | 397 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 398 | | { |
| | 0 | 399 | | WriteNewLine(output); |
| | 0 | 400 | | } |
| | | 401 | | |
| | 0 | 402 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 403 | | BytesPending += indent; |
| | | 404 | | |
| | 0 | 405 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 406 | | |
| | 0 | 407 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 408 | | BytesPending += escapedPropertyName.Length; |
| | | 409 | | |
| | 0 | 410 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 411 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 412 | | output[BytesPending++] = JsonConstants.Space; |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Writes the pre-encoded property name and pre-encoded value (as a JSON string) as part of a name/value pair o |
| | | 417 | | /// </summary> |
| | | 418 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 419 | | /// <param name="value">The JSON-encoded value to write.</param> |
| | | 420 | | /// <exception cref="InvalidOperationException"> |
| | | 421 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 422 | | /// </exception> |
| | | 423 | | public void WriteString(JsonEncodedText propertyName, JsonEncodedText value) |
| | 0 | 424 | | => WriteStringHelper(propertyName.EncodedUtf8Bytes, value.EncodedUtf8Bytes); |
| | | 425 | | |
| | | 426 | | private void WriteStringHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 427 | | { |
| | 0 | 428 | | Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize && utf8Value.Length <= JsonConst |
| | | 429 | | |
| | 0 | 430 | | WriteStringByOptions(utf8PropertyName, utf8Value); |
| | | 431 | | |
| | 0 | 432 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 433 | | _tokenType = JsonTokenType.String; |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | /// <summary> |
| | | 437 | | /// Writes the property name and pre-encoded value (as a JSON string) as part of a name/value pair of a JSON obj |
| | | 438 | | /// </summary> |
| | | 439 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 440 | | /// <param name="value">The JSON-encoded value to write.</param> |
| | | 441 | | /// <exception cref="ArgumentException"> |
| | | 442 | | /// Thrown when the specified property name is too large. |
| | | 443 | | /// </exception> |
| | | 444 | | /// <exception cref="ArgumentNullException"> |
| | | 445 | | /// The <paramref name="propertyName"/> parameter is <see langword="null"/>. |
| | | 446 | | /// </exception> |
| | | 447 | | /// <exception cref="InvalidOperationException"> |
| | | 448 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 449 | | /// </exception> |
| | | 450 | | /// <remarks> |
| | | 451 | | /// The property name is escaped before writing. |
| | | 452 | | /// </remarks> |
| | | 453 | | public void WriteString(string propertyName, JsonEncodedText value) |
| | 0 | 454 | | { |
| | 0 | 455 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | 0 | 456 | | WriteString(propertyName.AsSpan(), value); |
| | 0 | 457 | | } |
| | | 458 | | |
| | | 459 | | /// <summary> |
| | | 460 | | /// Writes the property name and string text value (as a JSON string) as part of a name/value pair of a JSON obj |
| | | 461 | | /// </summary> |
| | | 462 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 463 | | /// <param name="value">The value to write.</param> |
| | | 464 | | /// <exception cref="ArgumentException"> |
| | | 465 | | /// Thrown when the specified property name or value is too large. |
| | | 466 | | /// </exception> |
| | | 467 | | /// <exception cref="ArgumentNullException"> |
| | | 468 | | /// The <paramref name="propertyName"/> parameter is <see langword="null"/>. |
| | | 469 | | /// </exception> |
| | | 470 | | /// <exception cref="InvalidOperationException"> |
| | | 471 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 472 | | /// </exception> |
| | | 473 | | /// <remarks> |
| | | 474 | | /// <para> |
| | | 475 | | /// The property name and value is escaped before writing. |
| | | 476 | | /// </para> |
| | | 477 | | /// <para> |
| | | 478 | | /// If <paramref name="value"/> is <see langword="null"/> the JSON null value is written, |
| | | 479 | | /// as if <see cref="WriteNull(System.ReadOnlySpan{byte})"/> were called. |
| | | 480 | | /// </para> |
| | | 481 | | /// </remarks> |
| | | 482 | | public void WriteString(string propertyName, string? value) |
| | 0 | 483 | | { |
| | 0 | 484 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | | 485 | | |
| | 0 | 486 | | if (value == null) |
| | 0 | 487 | | { |
| | 0 | 488 | | WriteNull(propertyName.AsSpan()); |
| | 0 | 489 | | } |
| | | 490 | | else |
| | 0 | 491 | | { |
| | 0 | 492 | | WriteString(propertyName.AsSpan(), value.AsSpan()); |
| | 0 | 493 | | } |
| | 0 | 494 | | } |
| | | 495 | | |
| | | 496 | | /// <summary> |
| | | 497 | | /// Writes the property name and text value (as a JSON string) as part of a name/value pair of a JSON object. |
| | | 498 | | /// </summary> |
| | | 499 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 500 | | /// <param name="value">The value to write.</param> |
| | | 501 | | /// <exception cref="ArgumentException"> |
| | | 502 | | /// Thrown when the specified property name or value is too large. |
| | | 503 | | /// </exception> |
| | | 504 | | /// <exception cref="InvalidOperationException"> |
| | | 505 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 506 | | /// </exception> |
| | | 507 | | /// <remarks> |
| | | 508 | | /// The property name and value is escaped before writing. |
| | | 509 | | /// </remarks> |
| | | 510 | | public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) |
| | 0 | 511 | | { |
| | 0 | 512 | | JsonWriterHelper.ValidatePropertyAndValue(propertyName, value); |
| | | 513 | | |
| | 0 | 514 | | WriteStringEscape(propertyName, value); |
| | | 515 | | |
| | 0 | 516 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 517 | | _tokenType = JsonTokenType.String; |
| | 0 | 518 | | } |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// Writes the UTF-8 property name and UTF-8 text value (as a JSON string) as part of a name/value pair of a JSO |
| | | 522 | | /// </summary> |
| | | 523 | | /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param> |
| | | 524 | | /// <param name="utf8Value">The UTF-8 encoded value to write.</param> |
| | | 525 | | /// <exception cref="ArgumentException"> |
| | | 526 | | /// Thrown when the specified property name or value is too large. |
| | | 527 | | /// </exception> |
| | | 528 | | /// <exception cref="InvalidOperationException"> |
| | | 529 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 530 | | /// </exception> |
| | | 531 | | /// <remarks> |
| | | 532 | | /// The property name and value is escaped before writing. |
| | | 533 | | /// </remarks> |
| | | 534 | | public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 535 | | { |
| | 0 | 536 | | JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, utf8Value); |
| | | 537 | | |
| | 0 | 538 | | WriteStringEscape(utf8PropertyName, utf8Value); |
| | | 539 | | |
| | 0 | 540 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 541 | | _tokenType = JsonTokenType.String; |
| | 0 | 542 | | } |
| | | 543 | | |
| | | 544 | | /// <summary> |
| | | 545 | | /// Writes the pre-encoded property name and string text value (as a JSON string) as part of a name/value pair o |
| | | 546 | | /// </summary> |
| | | 547 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 548 | | /// <param name="value">The value to write.</param> |
| | | 549 | | /// <exception cref="ArgumentException"> |
| | | 550 | | /// Thrown when the specified value is too large. |
| | | 551 | | /// </exception> |
| | | 552 | | /// <exception cref="InvalidOperationException"> |
| | | 553 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 554 | | /// </exception> |
| | | 555 | | /// <remarks> |
| | | 556 | | /// <para> |
| | | 557 | | /// The value is escaped before writing. |
| | | 558 | | /// </para> |
| | | 559 | | /// <para> |
| | | 560 | | /// If <paramref name="value"/> is <see langword="null"/> the JSON null value is written, |
| | | 561 | | /// as if <see cref="WriteNull(System.Text.Json.JsonEncodedText)"/> was called. |
| | | 562 | | /// </para> |
| | | 563 | | /// </remarks> |
| | | 564 | | public void WriteString(JsonEncodedText propertyName, string? value) |
| | 0 | 565 | | { |
| | 0 | 566 | | if (value == null) |
| | 0 | 567 | | { |
| | 0 | 568 | | WriteNull(propertyName); |
| | 0 | 569 | | } |
| | | 570 | | else |
| | 0 | 571 | | { |
| | 0 | 572 | | WriteString(propertyName, value.AsSpan()); |
| | 0 | 573 | | } |
| | 0 | 574 | | } |
| | | 575 | | |
| | | 576 | | /// <summary> |
| | | 577 | | /// Writes the pre-encoded property name and text value (as a JSON string) as part of a name/value pair of a JSO |
| | | 578 | | /// </summary> |
| | | 579 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 580 | | /// <param name="value">The value to write.</param> |
| | | 581 | | /// <exception cref="ArgumentException"> |
| | | 582 | | /// Thrown when the specified value is too large. |
| | | 583 | | /// </exception> |
| | | 584 | | /// <exception cref="InvalidOperationException"> |
| | | 585 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 586 | | /// </exception> |
| | | 587 | | /// <remarks> |
| | | 588 | | /// The value is escaped before writing. |
| | | 589 | | /// </remarks> |
| | | 590 | | public void WriteString(JsonEncodedText propertyName, ReadOnlySpan<char> value) |
| | 0 | 591 | | => WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, value); |
| | | 592 | | |
| | | 593 | | private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) |
| | 0 | 594 | | { |
| | 0 | 595 | | Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | | 596 | | |
| | 0 | 597 | | JsonWriterHelper.ValidateValue(value); |
| | | 598 | | |
| | 0 | 599 | | int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); |
| | | 600 | | |
| | 0 | 601 | | Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2); |
| | | 602 | | |
| | 0 | 603 | | if (valueIdx != -1) |
| | 0 | 604 | | { |
| | 0 | 605 | | WriteStringEscapeValueOnly(utf8PropertyName, value, valueIdx); |
| | 0 | 606 | | } |
| | | 607 | | else |
| | 0 | 608 | | { |
| | 0 | 609 | | WriteStringByOptions(utf8PropertyName, value); |
| | 0 | 610 | | } |
| | | 611 | | |
| | 0 | 612 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 613 | | _tokenType = JsonTokenType.String; |
| | 0 | 614 | | } |
| | | 615 | | |
| | | 616 | | /// <summary> |
| | | 617 | | /// Writes the property name and text value (as a JSON string) as part of a name/value pair of a JSON object. |
| | | 618 | | /// </summary> |
| | | 619 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 620 | | /// <param name="value">The value to write.</param> |
| | | 621 | | /// <exception cref="ArgumentException"> |
| | | 622 | | /// Thrown when the specified property name or value is too large. |
| | | 623 | | /// </exception> |
| | | 624 | | /// <exception cref="ArgumentNullException"> |
| | | 625 | | /// The <paramref name="propertyName"/> parameter is <see langword="null"/>. |
| | | 626 | | /// </exception> |
| | | 627 | | /// <exception cref="InvalidOperationException"> |
| | | 628 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 629 | | /// </exception> |
| | | 630 | | /// <remarks> |
| | | 631 | | /// The property name and value is escaped before writing. |
| | | 632 | | /// </remarks> |
| | | 633 | | public void WriteString(string propertyName, ReadOnlySpan<char> value) |
| | 0 | 634 | | { |
| | 0 | 635 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | 0 | 636 | | WriteString(propertyName.AsSpan(), value); |
| | 0 | 637 | | } |
| | | 638 | | |
| | | 639 | | /// <summary> |
| | | 640 | | /// Writes the UTF-8 property name and text value (as a JSON string) as part of a name/value pair of a JSON obje |
| | | 641 | | /// </summary> |
| | | 642 | | /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param> |
| | | 643 | | /// <param name="value">The value to write.</param> |
| | | 644 | | /// <exception cref="ArgumentException"> |
| | | 645 | | /// Thrown when the specified property name or value is too large. |
| | | 646 | | /// </exception> |
| | | 647 | | /// <exception cref="InvalidOperationException"> |
| | | 648 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 649 | | /// </exception> |
| | | 650 | | /// <remarks> |
| | | 651 | | /// The property name and value is escaped before writing. |
| | | 652 | | /// </remarks> |
| | | 653 | | public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) |
| | 0 | 654 | | { |
| | 0 | 655 | | JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, value); |
| | | 656 | | |
| | 0 | 657 | | WriteStringEscape(utf8PropertyName, value); |
| | | 658 | | |
| | 0 | 659 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 660 | | _tokenType = JsonTokenType.String; |
| | 0 | 661 | | } |
| | | 662 | | |
| | | 663 | | /// <summary> |
| | | 664 | | /// Writes the pre-encoded property name and UTF-8 text value (as a JSON string) as part of a name/value pair of |
| | | 665 | | /// </summary> |
| | | 666 | | /// <param name="propertyName">The JSON-encoded name of the property to write.</param> |
| | | 667 | | /// <param name="utf8Value">The UTF-8 encoded value to write.</param> |
| | | 668 | | /// <exception cref="ArgumentException"> |
| | | 669 | | /// Thrown when the specified value is too large. |
| | | 670 | | /// </exception> |
| | | 671 | | /// <exception cref="InvalidOperationException"> |
| | | 672 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 673 | | /// </exception> |
| | | 674 | | /// <remarks> |
| | | 675 | | /// The value is escaped before writing. |
| | | 676 | | /// </remarks> |
| | | 677 | | public void WriteString(JsonEncodedText propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 678 | | => WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, utf8Value); |
| | | 679 | | |
| | | 680 | | private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 681 | | { |
| | 0 | 682 | | Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | | 683 | | |
| | 0 | 684 | | JsonWriterHelper.ValidateValue(utf8Value); |
| | | 685 | | |
| | 0 | 686 | | int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); |
| | | 687 | | |
| | 0 | 688 | | Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2); |
| | | 689 | | |
| | 0 | 690 | | if (valueIdx != -1) |
| | 0 | 691 | | { |
| | 0 | 692 | | WriteStringEscapeValueOnly(utf8PropertyName, utf8Value, valueIdx); |
| | 0 | 693 | | } |
| | | 694 | | else |
| | 0 | 695 | | { |
| | 0 | 696 | | WriteStringByOptions(utf8PropertyName, utf8Value); |
| | 0 | 697 | | } |
| | | 698 | | |
| | 0 | 699 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 700 | | _tokenType = JsonTokenType.String; |
| | 0 | 701 | | } |
| | | 702 | | |
| | | 703 | | /// <summary> |
| | | 704 | | /// Writes the property name and UTF-8 text value (as a JSON string) as part of a name/value pair of a JSON obje |
| | | 705 | | /// </summary> |
| | | 706 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 707 | | /// <param name="utf8Value">The UTF-8 encoded value to write.</param> |
| | | 708 | | /// <exception cref="ArgumentException"> |
| | | 709 | | /// Thrown when the specified property name or value is too large. |
| | | 710 | | /// </exception> |
| | | 711 | | /// <exception cref="ArgumentNullException"> |
| | | 712 | | /// The <paramref name="propertyName"/> parameter is <see langword="null"/>. |
| | | 713 | | /// </exception> |
| | | 714 | | /// <exception cref="InvalidOperationException"> |
| | | 715 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 716 | | /// </exception> |
| | | 717 | | /// <remarks> |
| | | 718 | | /// The property name and value is escaped before writing. |
| | | 719 | | /// </remarks> |
| | | 720 | | public void WriteString(string propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 721 | | { |
| | 0 | 722 | | ArgumentNullException.ThrowIfNull(propertyName); |
| | 0 | 723 | | WriteString(propertyName.AsSpan(), utf8Value); |
| | 0 | 724 | | } |
| | | 725 | | |
| | | 726 | | /// <summary> |
| | | 727 | | /// Writes the property name and UTF-8 text value (as a JSON string) as part of a name/value pair of a JSON obje |
| | | 728 | | /// </summary> |
| | | 729 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 730 | | /// <param name="utf8Value">The UTF-8 encoded value to write.</param> |
| | | 731 | | /// <exception cref="ArgumentException"> |
| | | 732 | | /// Thrown when the specified property name or value is too large. |
| | | 733 | | /// </exception> |
| | | 734 | | /// <exception cref="InvalidOperationException"> |
| | | 735 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 736 | | /// </exception> |
| | | 737 | | /// <remarks> |
| | | 738 | | /// The property name and value is escaped before writing. |
| | | 739 | | /// </remarks> |
| | | 740 | | public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 741 | | { |
| | 0 | 742 | | JsonWriterHelper.ValidatePropertyAndValue(propertyName, utf8Value); |
| | | 743 | | |
| | 0 | 744 | | WriteStringEscape(propertyName, utf8Value); |
| | | 745 | | |
| | 0 | 746 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 747 | | _tokenType = JsonTokenType.String; |
| | 0 | 748 | | } |
| | | 749 | | |
| | | 750 | | /// <summary> |
| | | 751 | | /// Writes the property name and pre-encoded value (as a JSON string) as part of a name/value pair of a JSON obj |
| | | 752 | | /// </summary> |
| | | 753 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 754 | | /// <param name="value">The JSON-encoded value to write.</param> |
| | | 755 | | /// <exception cref="ArgumentException"> |
| | | 756 | | /// Thrown when the specified property name is too large. |
| | | 757 | | /// </exception> |
| | | 758 | | /// <exception cref="InvalidOperationException"> |
| | | 759 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 760 | | /// </exception> |
| | | 761 | | /// <remarks> |
| | | 762 | | /// The property name is escaped before writing. |
| | | 763 | | /// </remarks> |
| | | 764 | | public void WriteString(ReadOnlySpan<char> propertyName, JsonEncodedText value) |
| | 0 | 765 | | => WriteStringHelperEscapeProperty(propertyName, value.EncodedUtf8Bytes); |
| | | 766 | | |
| | | 767 | | private void WriteStringHelperEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 768 | | { |
| | 0 | 769 | | Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | | 770 | | |
| | 0 | 771 | | JsonWriterHelper.ValidateProperty(propertyName); |
| | | 772 | | |
| | 0 | 773 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); |
| | | 774 | | |
| | 0 | 775 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 776 | | |
| | 0 | 777 | | if (propertyIdx != -1) |
| | 0 | 778 | | { |
| | 0 | 779 | | WriteStringEscapePropertyOnly(propertyName, utf8Value, propertyIdx); |
| | 0 | 780 | | } |
| | | 781 | | else |
| | 0 | 782 | | { |
| | 0 | 783 | | WriteStringByOptions(propertyName, utf8Value); |
| | 0 | 784 | | } |
| | | 785 | | |
| | 0 | 786 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 787 | | _tokenType = JsonTokenType.String; |
| | 0 | 788 | | } |
| | | 789 | | |
| | | 790 | | /// <summary> |
| | | 791 | | /// Writes the property name and string text value (as a JSON string) as part of a name/value pair of a JSON obj |
| | | 792 | | /// </summary> |
| | | 793 | | /// <param name="propertyName">The name of the property to write.</param> |
| | | 794 | | /// <param name="value">The value to write.</param> |
| | | 795 | | /// <exception cref="ArgumentException"> |
| | | 796 | | /// Thrown when the specified property name or value is too large. |
| | | 797 | | /// </exception> |
| | | 798 | | /// <exception cref="InvalidOperationException"> |
| | | 799 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 800 | | /// </exception> |
| | | 801 | | /// <remarks> |
| | | 802 | | /// <para> |
| | | 803 | | /// The property name and value are escaped before writing. |
| | | 804 | | /// </para> |
| | | 805 | | /// <para> |
| | | 806 | | /// If <paramref name="value"/> is <see langword="null"/> the JSON null value is written, |
| | | 807 | | /// as if <see cref="WriteNull(System.ReadOnlySpan{char})"/> was called. |
| | | 808 | | /// </para> |
| | | 809 | | /// </remarks> |
| | | 810 | | public void WriteString(ReadOnlySpan<char> propertyName, string? value) |
| | 0 | 811 | | { |
| | 0 | 812 | | if (value == null) |
| | 0 | 813 | | { |
| | 0 | 814 | | WriteNull(propertyName); |
| | 0 | 815 | | } |
| | | 816 | | else |
| | 0 | 817 | | { |
| | 0 | 818 | | WriteString(propertyName, value.AsSpan()); |
| | 0 | 819 | | } |
| | 0 | 820 | | } |
| | | 821 | | |
| | | 822 | | /// <summary> |
| | | 823 | | /// Writes the UTF-8 property name and pre-encoded value (as a JSON string) as part of a name/value pair of a JS |
| | | 824 | | /// </summary> |
| | | 825 | | /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param> |
| | | 826 | | /// <param name="value">The JSON-encoded value to write.</param> |
| | | 827 | | /// <exception cref="ArgumentException"> |
| | | 828 | | /// Thrown when the specified property name is too large. |
| | | 829 | | /// </exception> |
| | | 830 | | /// <exception cref="InvalidOperationException"> |
| | | 831 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 832 | | /// </exception> |
| | | 833 | | /// <remarks> |
| | | 834 | | /// The property name is escaped before writing. |
| | | 835 | | /// </remarks> |
| | | 836 | | public void WriteString(ReadOnlySpan<byte> utf8PropertyName, JsonEncodedText value) |
| | 0 | 837 | | => WriteStringHelperEscapeProperty(utf8PropertyName, value.EncodedUtf8Bytes); |
| | | 838 | | |
| | | 839 | | private void WriteStringHelperEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 840 | | { |
| | 0 | 841 | | Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | | 842 | | |
| | 0 | 843 | | JsonWriterHelper.ValidateProperty(utf8PropertyName); |
| | | 844 | | |
| | 0 | 845 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); |
| | | 846 | | |
| | 0 | 847 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 848 | | |
| | 0 | 849 | | if (propertyIdx != -1) |
| | 0 | 850 | | { |
| | 0 | 851 | | WriteStringEscapePropertyOnly(utf8PropertyName, utf8Value, propertyIdx); |
| | 0 | 852 | | } |
| | | 853 | | else |
| | 0 | 854 | | { |
| | 0 | 855 | | WriteStringByOptions(utf8PropertyName, utf8Value); |
| | 0 | 856 | | } |
| | | 857 | | |
| | 0 | 858 | | SetFlagToAddListSeparatorBeforeNextItem(); |
| | 0 | 859 | | _tokenType = JsonTokenType.String; |
| | 0 | 860 | | } |
| | | 861 | | |
| | | 862 | | /// <summary> |
| | | 863 | | /// Writes the UTF-8 property name and string text value (as a JSON string) as part of a name/value pair of a JS |
| | | 864 | | /// </summary> |
| | | 865 | | /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param> |
| | | 866 | | /// <param name="value">The value to write.</param> |
| | | 867 | | /// <exception cref="ArgumentException"> |
| | | 868 | | /// Thrown when the specified property name or value is too large. |
| | | 869 | | /// </exception> |
| | | 870 | | /// <exception cref="InvalidOperationException"> |
| | | 871 | | /// Thrown if this would result in invalid JSON being written (while validation is enabled). |
| | | 872 | | /// </exception> |
| | | 873 | | /// <remarks> |
| | | 874 | | /// <para> |
| | | 875 | | /// The property name and value are escaped before writing. |
| | | 876 | | /// </para> |
| | | 877 | | /// <para> |
| | | 878 | | /// If <paramref name="value"/> is <see langword="null"/> the JSON null value is written, |
| | | 879 | | /// as if <see cref="WriteNull(System.ReadOnlySpan{byte})"/> was called. |
| | | 880 | | /// </para> |
| | | 881 | | /// </remarks> |
| | | 882 | | public void WriteString(ReadOnlySpan<byte> utf8PropertyName, string? value) |
| | 0 | 883 | | { |
| | 0 | 884 | | if (value == null) |
| | 0 | 885 | | { |
| | 0 | 886 | | WriteNull(utf8PropertyName); |
| | 0 | 887 | | } |
| | | 888 | | else |
| | 0 | 889 | | { |
| | 0 | 890 | | WriteString(utf8PropertyName, value.AsSpan()); |
| | 0 | 891 | | } |
| | 0 | 892 | | } |
| | | 893 | | |
| | | 894 | | private void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> utf8Value, in |
| | 0 | 895 | | { |
| | 0 | 896 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); |
| | 0 | 897 | | Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < utf8Value.Length); |
| | | 898 | | |
| | 0 | 899 | | byte[]? valueArray = null; |
| | | 900 | | |
| | 0 | 901 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndex); |
| | | 902 | | |
| | 0 | 903 | | Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 904 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 905 | | (valueArray = ArrayPool<byte>.Shared.Rent(length)); |
| | | 906 | | |
| | 0 | 907 | | JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndex, _options.Encoder, out int written); |
| | | 908 | | |
| | 0 | 909 | | WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written)); |
| | | 910 | | |
| | 0 | 911 | | if (valueArray != null) |
| | 0 | 912 | | { |
| | 0 | 913 | | ArrayPool<byte>.Shared.Return(valueArray); |
| | 0 | 914 | | } |
| | 0 | 915 | | } |
| | | 916 | | |
| | | 917 | | private void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> value, int fi |
| | 0 | 918 | | { |
| | 0 | 919 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); |
| | 0 | 920 | | Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < value.Length); |
| | | 921 | | |
| | 0 | 922 | | char[]? valueArray = null; |
| | | 923 | | |
| | 0 | 924 | | int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndex); |
| | | 925 | | |
| | 0 | 926 | | Span<char> escapedValue = length <= JsonConstants.StackallocCharThreshold ? |
| | 0 | 927 | | stackalloc char[JsonConstants.StackallocCharThreshold] : |
| | 0 | 928 | | (valueArray = ArrayPool<char>.Shared.Rent(length)); |
| | | 929 | | |
| | 0 | 930 | | JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, _options.Encoder, out int written); |
| | | 931 | | |
| | 0 | 932 | | WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written)); |
| | | 933 | | |
| | 0 | 934 | | if (valueArray != null) |
| | 0 | 935 | | { |
| | 0 | 936 | | ArrayPool<char>.Shared.Return(valueArray); |
| | 0 | 937 | | } |
| | 0 | 938 | | } |
| | | 939 | | |
| | | 940 | | private void WriteStringEscapePropertyOnly(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> escapedValue, int |
| | 0 | 941 | | { |
| | 0 | 942 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); |
| | 0 | 943 | | Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < propertyName.Length); |
| | | 944 | | |
| | 0 | 945 | | char[]? propertyArray = null; |
| | | 946 | | |
| | 0 | 947 | | int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndex); |
| | | 948 | | |
| | 0 | 949 | | Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? |
| | 0 | 950 | | stackalloc char[JsonConstants.StackallocCharThreshold] : |
| | 0 | 951 | | (propertyArray = ArrayPool<char>.Shared.Rent(length)); |
| | | 952 | | |
| | 0 | 953 | | JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int |
| | | 954 | | |
| | 0 | 955 | | WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue); |
| | | 956 | | |
| | 0 | 957 | | if (propertyArray != null) |
| | 0 | 958 | | { |
| | 0 | 959 | | ArrayPool<char>.Shared.Return(propertyArray); |
| | 0 | 960 | | } |
| | 0 | 961 | | } |
| | | 962 | | |
| | | 963 | | private void WriteStringEscapePropertyOnly(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> escapedValue, |
| | 0 | 964 | | { |
| | 0 | 965 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); |
| | 0 | 966 | | Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < utf8PropertyName.Length); |
| | | 967 | | |
| | 0 | 968 | | byte[]? propertyArray = null; |
| | | 969 | | |
| | 0 | 970 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndex); |
| | | 971 | | |
| | 0 | 972 | | Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? |
| | 0 | 973 | | stackalloc byte[JsonConstants.StackallocByteThreshold] : |
| | 0 | 974 | | (propertyArray = ArrayPool<byte>.Shared.Rent(length)); |
| | | 975 | | |
| | 0 | 976 | | JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out |
| | | 977 | | |
| | 0 | 978 | | WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue); |
| | | 979 | | |
| | 0 | 980 | | if (propertyArray != null) |
| | 0 | 981 | | { |
| | 0 | 982 | | ArrayPool<byte>.Shared.Return(propertyArray); |
| | 0 | 983 | | } |
| | 0 | 984 | | } |
| | | 985 | | |
| | | 986 | | private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) |
| | 0 | 987 | | { |
| | 0 | 988 | | int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); |
| | 0 | 989 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); |
| | | 990 | | |
| | 0 | 991 | | Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2); |
| | 0 | 992 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 993 | | |
| | | 994 | | // Equivalent to: valueIdx != -1 || propertyIdx != -1 |
| | 0 | 995 | | if (valueIdx + propertyIdx != -2) |
| | 0 | 996 | | { |
| | 0 | 997 | | WriteStringEscapePropertyOrValue(propertyName, value, propertyIdx, valueIdx); |
| | 0 | 998 | | } |
| | | 999 | | else |
| | 0 | 1000 | | { |
| | 0 | 1001 | | WriteStringByOptions(propertyName, value); |
| | 0 | 1002 | | } |
| | 0 | 1003 | | } |
| | | 1004 | | |
| | | 1005 | | private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 1006 | | { |
| | 0 | 1007 | | int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); |
| | 0 | 1008 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); |
| | | 1009 | | |
| | 0 | 1010 | | Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2); |
| | 0 | 1011 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 1012 | | |
| | | 1013 | | // Equivalent to: valueIdx != -1 || propertyIdx != -1 |
| | 0 | 1014 | | if (valueIdx + propertyIdx != -2) |
| | 0 | 1015 | | { |
| | 0 | 1016 | | WriteStringEscapePropertyOrValue(utf8PropertyName, utf8Value, propertyIdx, valueIdx); |
| | 0 | 1017 | | } |
| | | 1018 | | else |
| | 0 | 1019 | | { |
| | 0 | 1020 | | WriteStringByOptions(utf8PropertyName, utf8Value); |
| | 0 | 1021 | | } |
| | 0 | 1022 | | } |
| | | 1023 | | |
| | | 1024 | | private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 1025 | | { |
| | 0 | 1026 | | int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); |
| | 0 | 1027 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); |
| | | 1028 | | |
| | 0 | 1029 | | Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2); |
| | 0 | 1030 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 1031 | | |
| | | 1032 | | // Equivalent to: valueIdx != -1 || propertyIdx != -1 |
| | 0 | 1033 | | if (valueIdx + propertyIdx != -2) |
| | 0 | 1034 | | { |
| | 0 | 1035 | | WriteStringEscapePropertyOrValue(propertyName, utf8Value, propertyIdx, valueIdx); |
| | 0 | 1036 | | } |
| | | 1037 | | else |
| | 0 | 1038 | | { |
| | 0 | 1039 | | WriteStringByOptions(propertyName, utf8Value); |
| | 0 | 1040 | | } |
| | 0 | 1041 | | } |
| | | 1042 | | |
| | | 1043 | | private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) |
| | 0 | 1044 | | { |
| | 0 | 1045 | | int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); |
| | 0 | 1046 | | int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); |
| | | 1047 | | |
| | 0 | 1048 | | Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2); |
| | 0 | 1049 | | Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2); |
| | | 1050 | | |
| | | 1051 | | // Equivalent to: valueIdx != -1 || propertyIdx != -1 |
| | 0 | 1052 | | if (valueIdx + propertyIdx != -2) |
| | 0 | 1053 | | { |
| | 0 | 1054 | | WriteStringEscapePropertyOrValue(utf8PropertyName, value, propertyIdx, valueIdx); |
| | 0 | 1055 | | } |
| | | 1056 | | else |
| | 0 | 1057 | | { |
| | 0 | 1058 | | WriteStringByOptions(utf8PropertyName, value); |
| | 0 | 1059 | | } |
| | 0 | 1060 | | } |
| | | 1061 | | |
| | | 1062 | | private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<char> propertyName, scoped ReadOnlySpan<char> |
| | 0 | 1063 | | { |
| | 0 | 1064 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); |
| | 0 | 1065 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); |
| | | 1066 | | |
| | 0 | 1067 | | char[]? valueArray = null; |
| | 0 | 1068 | | char[]? propertyArray = null; |
| | | 1069 | | scoped Span<char> escapedValue; |
| | | 1070 | | |
| | 0 | 1071 | | if (firstEscapeIndexVal != -1) |
| | 0 | 1072 | | { |
| | 0 | 1073 | | int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); |
| | | 1074 | | |
| | 0 | 1075 | | if (length > JsonConstants.StackallocCharThreshold) |
| | 0 | 1076 | | { |
| | 0 | 1077 | | valueArray = ArrayPool<char>.Shared.Rent(length); |
| | 0 | 1078 | | escapedValue = valueArray; |
| | 0 | 1079 | | } |
| | | 1080 | | else |
| | 0 | 1081 | | { |
| | 0 | 1082 | | escapedValue = stackalloc char[JsonConstants.StackallocCharThreshold]; |
| | 0 | 1083 | | } |
| | | 1084 | | |
| | 0 | 1085 | | JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int writte |
| | 0 | 1086 | | value = escapedValue.Slice(0, written); |
| | 0 | 1087 | | } |
| | | 1088 | | |
| | | 1089 | | scoped Span<char> escapedPropertyName; |
| | | 1090 | | |
| | 0 | 1091 | | if (firstEscapeIndexProp != -1) |
| | 0 | 1092 | | { |
| | 0 | 1093 | | int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); |
| | | 1094 | | |
| | 0 | 1095 | | if (length > JsonConstants.StackallocCharThreshold) |
| | 0 | 1096 | | { |
| | 0 | 1097 | | propertyArray = ArrayPool<char>.Shared.Rent(length); |
| | 0 | 1098 | | escapedPropertyName = propertyArray; |
| | 0 | 1099 | | } |
| | | 1100 | | else |
| | 0 | 1101 | | { |
| | 0 | 1102 | | escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold]; |
| | 0 | 1103 | | } |
| | | 1104 | | |
| | 0 | 1105 | | JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, |
| | 0 | 1106 | | propertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 1107 | | } |
| | | 1108 | | |
| | 0 | 1109 | | WriteStringByOptions(propertyName, value); |
| | | 1110 | | |
| | 0 | 1111 | | if (valueArray != null) |
| | 0 | 1112 | | { |
| | 0 | 1113 | | ArrayPool<char>.Shared.Return(valueArray); |
| | 0 | 1114 | | } |
| | | 1115 | | |
| | 0 | 1116 | | if (propertyArray != null) |
| | 0 | 1117 | | { |
| | 0 | 1118 | | ArrayPool<char>.Shared.Return(propertyArray); |
| | 0 | 1119 | | } |
| | 0 | 1120 | | } |
| | | 1121 | | |
| | | 1122 | | private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<byte> utf8PropertyName, scoped ReadOnlySpan<by |
| | 0 | 1123 | | { |
| | 0 | 1124 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); |
| | 0 | 1125 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); |
| | | 1126 | | |
| | 0 | 1127 | | byte[]? valueArray = null; |
| | 0 | 1128 | | byte[]? propertyArray = null; |
| | | 1129 | | scoped Span<byte> escapedValue; |
| | | 1130 | | |
| | 0 | 1131 | | if (firstEscapeIndexVal != -1) |
| | 0 | 1132 | | { |
| | 0 | 1133 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); |
| | | 1134 | | |
| | 0 | 1135 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 1136 | | { |
| | 0 | 1137 | | valueArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 1138 | | escapedValue = valueArray; |
| | 0 | 1139 | | } |
| | | 1140 | | else |
| | 0 | 1141 | | { |
| | 0 | 1142 | | escapedValue = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 1143 | | } |
| | | 1144 | | |
| | 0 | 1145 | | JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int wr |
| | 0 | 1146 | | utf8Value = escapedValue.Slice(0, written); |
| | 0 | 1147 | | } |
| | | 1148 | | |
| | | 1149 | | scoped Span<byte> escapedPropertyName; |
| | | 1150 | | |
| | 0 | 1151 | | if (firstEscapeIndexProp != -1) |
| | 0 | 1152 | | { |
| | 0 | 1153 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); |
| | | 1154 | | |
| | 0 | 1155 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 1156 | | { |
| | 0 | 1157 | | propertyArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 1158 | | escapedPropertyName = propertyArray; |
| | 0 | 1159 | | } |
| | | 1160 | | else |
| | 0 | 1161 | | { |
| | 0 | 1162 | | escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 1163 | | } |
| | | 1164 | | |
| | 0 | 1165 | | JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco |
| | 0 | 1166 | | utf8PropertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 1167 | | } |
| | | 1168 | | |
| | 0 | 1169 | | WriteStringByOptions(utf8PropertyName, utf8Value); |
| | | 1170 | | |
| | 0 | 1171 | | if (valueArray != null) |
| | 0 | 1172 | | { |
| | 0 | 1173 | | ArrayPool<byte>.Shared.Return(valueArray); |
| | 0 | 1174 | | } |
| | | 1175 | | |
| | 0 | 1176 | | if (propertyArray != null) |
| | 0 | 1177 | | { |
| | 0 | 1178 | | ArrayPool<byte>.Shared.Return(propertyArray); |
| | 0 | 1179 | | } |
| | 0 | 1180 | | } |
| | | 1181 | | |
| | | 1182 | | private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<char> propertyName, scoped ReadOnlySpan<byte> |
| | 0 | 1183 | | { |
| | 0 | 1184 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length); |
| | 0 | 1185 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length); |
| | | 1186 | | |
| | 0 | 1187 | | byte[]? valueArray = null; |
| | 0 | 1188 | | char[]? propertyArray = null; |
| | | 1189 | | scoped Span<byte> escapedValue; |
| | | 1190 | | |
| | 0 | 1191 | | if (firstEscapeIndexVal != -1) |
| | 0 | 1192 | | { |
| | 0 | 1193 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); |
| | | 1194 | | |
| | 0 | 1195 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 1196 | | { |
| | 0 | 1197 | | valueArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 1198 | | escapedValue = valueArray; |
| | 0 | 1199 | | } |
| | | 1200 | | else |
| | 0 | 1201 | | { |
| | 0 | 1202 | | escapedValue = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 1203 | | } |
| | | 1204 | | |
| | 0 | 1205 | | JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int wr |
| | 0 | 1206 | | utf8Value = escapedValue.Slice(0, written); |
| | 0 | 1207 | | } |
| | | 1208 | | |
| | | 1209 | | scoped Span<char> escapedPropertyName; |
| | | 1210 | | |
| | 0 | 1211 | | if (firstEscapeIndexProp != -1) |
| | 0 | 1212 | | { |
| | 0 | 1213 | | int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); |
| | | 1214 | | |
| | 0 | 1215 | | if (length > JsonConstants.StackallocCharThreshold) |
| | 0 | 1216 | | { |
| | 0 | 1217 | | propertyArray = ArrayPool<char>.Shared.Rent(length); |
| | 0 | 1218 | | escapedPropertyName = propertyArray; |
| | 0 | 1219 | | } |
| | | 1220 | | else |
| | 0 | 1221 | | { |
| | 0 | 1222 | | escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold]; |
| | 0 | 1223 | | } |
| | | 1224 | | |
| | 0 | 1225 | | JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, |
| | 0 | 1226 | | propertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 1227 | | } |
| | | 1228 | | |
| | 0 | 1229 | | WriteStringByOptions(propertyName, utf8Value); |
| | | 1230 | | |
| | 0 | 1231 | | if (valueArray != null) |
| | 0 | 1232 | | { |
| | 0 | 1233 | | ArrayPool<byte>.Shared.Return(valueArray); |
| | 0 | 1234 | | } |
| | | 1235 | | |
| | 0 | 1236 | | if (propertyArray != null) |
| | 0 | 1237 | | { |
| | 0 | 1238 | | ArrayPool<char>.Shared.Return(propertyArray); |
| | 0 | 1239 | | } |
| | 0 | 1240 | | } |
| | | 1241 | | |
| | | 1242 | | private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<byte> utf8PropertyName, scoped ReadOnlySpan<ch |
| | 0 | 1243 | | { |
| | 0 | 1244 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length); |
| | 0 | 1245 | | Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length); |
| | | 1246 | | |
| | 0 | 1247 | | char[]? valueArray = null; |
| | 0 | 1248 | | byte[]? propertyArray = null; |
| | | 1249 | | scoped Span<char> escapedValue; |
| | | 1250 | | |
| | 0 | 1251 | | if (firstEscapeIndexVal != -1) |
| | 0 | 1252 | | { |
| | 0 | 1253 | | int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); |
| | | 1254 | | |
| | 0 | 1255 | | if (length > JsonConstants.StackallocCharThreshold) |
| | 0 | 1256 | | { |
| | 0 | 1257 | | valueArray = ArrayPool<char>.Shared.Rent(length); |
| | 0 | 1258 | | escapedValue = valueArray; |
| | 0 | 1259 | | } |
| | | 1260 | | else |
| | 0 | 1261 | | { |
| | 0 | 1262 | | escapedValue = stackalloc char[JsonConstants.StackallocCharThreshold]; |
| | 0 | 1263 | | } |
| | | 1264 | | |
| | 0 | 1265 | | JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int writte |
| | 0 | 1266 | | value = escapedValue.Slice(0, written); |
| | 0 | 1267 | | } |
| | | 1268 | | |
| | | 1269 | | scoped Span<byte> escapedPropertyName; |
| | | 1270 | | |
| | 0 | 1271 | | if (firstEscapeIndexProp != -1) |
| | 0 | 1272 | | { |
| | 0 | 1273 | | int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); |
| | | 1274 | | |
| | 0 | 1275 | | if (length > JsonConstants.StackallocByteThreshold) |
| | 0 | 1276 | | { |
| | 0 | 1277 | | propertyArray = ArrayPool<byte>.Shared.Rent(length); |
| | 0 | 1278 | | escapedPropertyName = propertyArray; |
| | 0 | 1279 | | } |
| | | 1280 | | else |
| | 0 | 1281 | | { |
| | 0 | 1282 | | escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold]; |
| | 0 | 1283 | | } |
| | | 1284 | | |
| | 0 | 1285 | | JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco |
| | 0 | 1286 | | utf8PropertyName = escapedPropertyName.Slice(0, written); |
| | 0 | 1287 | | } |
| | | 1288 | | |
| | 0 | 1289 | | WriteStringByOptions(utf8PropertyName, value); |
| | | 1290 | | |
| | 0 | 1291 | | if (valueArray != null) |
| | 0 | 1292 | | { |
| | 0 | 1293 | | ArrayPool<char>.Shared.Return(valueArray); |
| | 0 | 1294 | | } |
| | | 1295 | | |
| | 0 | 1296 | | if (propertyArray != null) |
| | 0 | 1297 | | { |
| | 0 | 1298 | | ArrayPool<byte>.Shared.Return(propertyArray); |
| | 0 | 1299 | | } |
| | 0 | 1300 | | } |
| | | 1301 | | |
| | | 1302 | | private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) |
| | 0 | 1303 | | { |
| | 0 | 1304 | | ValidateWritingProperty(); |
| | 0 | 1305 | | if (_options.Indented) |
| | 0 | 1306 | | { |
| | 0 | 1307 | | WriteStringIndented(propertyName, value); |
| | 0 | 1308 | | } |
| | | 1309 | | else |
| | 0 | 1310 | | { |
| | 0 | 1311 | | WriteStringMinimized(propertyName, value); |
| | 0 | 1312 | | } |
| | 0 | 1313 | | } |
| | | 1314 | | |
| | | 1315 | | private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 1316 | | { |
| | 0 | 1317 | | ValidateWritingProperty(); |
| | 0 | 1318 | | if (_options.Indented) |
| | 0 | 1319 | | { |
| | 0 | 1320 | | WriteStringIndented(utf8PropertyName, utf8Value); |
| | 0 | 1321 | | } |
| | | 1322 | | else |
| | 0 | 1323 | | { |
| | 0 | 1324 | | WriteStringMinimized(utf8PropertyName, utf8Value); |
| | 0 | 1325 | | } |
| | 0 | 1326 | | } |
| | | 1327 | | |
| | | 1328 | | private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) |
| | 0 | 1329 | | { |
| | 0 | 1330 | | ValidateWritingProperty(); |
| | 0 | 1331 | | if (_options.Indented) |
| | 0 | 1332 | | { |
| | 0 | 1333 | | WriteStringIndented(propertyName, utf8Value); |
| | 0 | 1334 | | } |
| | | 1335 | | else |
| | 0 | 1336 | | { |
| | 0 | 1337 | | WriteStringMinimized(propertyName, utf8Value); |
| | 0 | 1338 | | } |
| | 0 | 1339 | | } |
| | | 1340 | | |
| | | 1341 | | private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) |
| | 0 | 1342 | | { |
| | 0 | 1343 | | ValidateWritingProperty(); |
| | 0 | 1344 | | if (_options.Indented) |
| | 0 | 1345 | | { |
| | 0 | 1346 | | WriteStringIndented(utf8PropertyName, value); |
| | 0 | 1347 | | } |
| | | 1348 | | else |
| | 0 | 1349 | | { |
| | 0 | 1350 | | WriteStringMinimized(utf8PropertyName, value); |
| | 0 | 1351 | | } |
| | 0 | 1352 | | } |
| | | 1353 | | |
| | | 1354 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1355 | | private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue) |
| | 0 | 1356 | | { |
| | 0 | 1357 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxUnescapedTokenSize); |
| | 0 | 1358 | | Debug.Assert(escapedPropertyName.Length < ((int.MaxValue - 6) / JsonConstants.MaxExpansionFactorWhileTransco |
| | | 1359 | | |
| | | 1360 | | // All ASCII, 2 quotes for property name, 2 quotes for value, and 1 colon => escapedPropertyName.Length + es |
| | | 1361 | | // Optionally, 1 list separator, and up to 3x growth when transcoding |
| | 0 | 1362 | | int maxRequired = ((escapedPropertyName.Length + escapedValue.Length) * JsonConstants.MaxExpansionFactorWhil |
| | | 1363 | | |
| | 0 | 1364 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1365 | | { |
| | 0 | 1366 | | Grow(maxRequired); |
| | 0 | 1367 | | } |
| | | 1368 | | |
| | 0 | 1369 | | Span<byte> output = _memory.Span; |
| | | 1370 | | |
| | 0 | 1371 | | if (_currentDepth < 0) |
| | 0 | 1372 | | { |
| | 0 | 1373 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1374 | | } |
| | 0 | 1375 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1376 | | |
| | 0 | 1377 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 1378 | | |
| | 0 | 1379 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1380 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | | 1381 | | |
| | 0 | 1382 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1383 | | |
| | 0 | 1384 | | TranscodeAndWrite(escapedValue, output); |
| | | 1385 | | |
| | 0 | 1386 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1387 | | } |
| | | 1388 | | |
| | | 1389 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1390 | | private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue) |
| | 0 | 1391 | | { |
| | 0 | 1392 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1393 | | Debug.Assert(escapedPropertyName.Length < int.MaxValue - escapedValue.Length - 6); |
| | | 1394 | | |
| | 0 | 1395 | | int minRequired = escapedPropertyName.Length + escapedValue.Length + 5; // 2 quotes for property name, 2 quo |
| | 0 | 1396 | | int maxRequired = minRequired + 1; // Optionally, 1 list separator |
| | | 1397 | | |
| | 0 | 1398 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1399 | | { |
| | 0 | 1400 | | Grow(maxRequired); |
| | 0 | 1401 | | } |
| | | 1402 | | |
| | 0 | 1403 | | Span<byte> output = _memory.Span; |
| | | 1404 | | |
| | 0 | 1405 | | if (_currentDepth < 0) |
| | 0 | 1406 | | { |
| | 0 | 1407 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1408 | | } |
| | 0 | 1409 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1410 | | |
| | 0 | 1411 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1412 | | BytesPending += escapedPropertyName.Length; |
| | | 1413 | | |
| | 0 | 1414 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1415 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | | 1416 | | |
| | 0 | 1417 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1418 | | |
| | 0 | 1419 | | escapedValue.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1420 | | BytesPending += escapedValue.Length; |
| | | 1421 | | |
| | 0 | 1422 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1423 | | } |
| | | 1424 | | |
| | | 1425 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1426 | | private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue) |
| | 0 | 1427 | | { |
| | 0 | 1428 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1429 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) |
| | | 1430 | | |
| | | 1431 | | // All ASCII, 2 quotes for property name, 2 quotes for value, and 1 colon => escapedPropertyName.Length + es |
| | | 1432 | | // Optionally, 1 list separator, and up to 3x growth when transcoding |
| | 0 | 1433 | | int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escapedV |
| | | 1434 | | |
| | 0 | 1435 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1436 | | { |
| | 0 | 1437 | | Grow(maxRequired); |
| | 0 | 1438 | | } |
| | | 1439 | | |
| | 0 | 1440 | | Span<byte> output = _memory.Span; |
| | | 1441 | | |
| | 0 | 1442 | | if (_currentDepth < 0) |
| | 0 | 1443 | | { |
| | 0 | 1444 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1445 | | } |
| | 0 | 1446 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1447 | | |
| | 0 | 1448 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 1449 | | |
| | 0 | 1450 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1451 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | | 1452 | | |
| | 0 | 1453 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1454 | | |
| | 0 | 1455 | | escapedValue.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1456 | | BytesPending += escapedValue.Length; |
| | | 1457 | | |
| | 0 | 1458 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1459 | | } |
| | | 1460 | | |
| | | 1461 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1462 | | private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue) |
| | 0 | 1463 | | { |
| | 0 | 1464 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1465 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) |
| | | 1466 | | |
| | | 1467 | | // All ASCII, 2 quotes for property name, 2 quotes for value, and 1 colon => escapedPropertyName.Length + es |
| | | 1468 | | // Optionally, 1 list separator, and up to 3x growth when transcoding |
| | 0 | 1469 | | int maxRequired = (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escapedProperty |
| | | 1470 | | |
| | 0 | 1471 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1472 | | { |
| | 0 | 1473 | | Grow(maxRequired); |
| | 0 | 1474 | | } |
| | | 1475 | | |
| | 0 | 1476 | | Span<byte> output = _memory.Span; |
| | | 1477 | | |
| | 0 | 1478 | | if (_currentDepth < 0) |
| | 0 | 1479 | | { |
| | 0 | 1480 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1481 | | } |
| | 0 | 1482 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1483 | | |
| | 0 | 1484 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1485 | | BytesPending += escapedPropertyName.Length; |
| | | 1486 | | |
| | 0 | 1487 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1488 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | | 1489 | | |
| | 0 | 1490 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1491 | | |
| | 0 | 1492 | | TranscodeAndWrite(escapedValue, output); |
| | | 1493 | | |
| | 0 | 1494 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1495 | | } |
| | | 1496 | | |
| | | 1497 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1498 | | private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue) |
| | 0 | 1499 | | { |
| | 0 | 1500 | | int indent = Indentation; |
| | 0 | 1501 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 1502 | | |
| | 0 | 1503 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1504 | | Debug.Assert(escapedPropertyName.Length < ((int.MaxValue - 7 - indent - _newLineLength) / JsonConstants.MaxE |
| | | 1505 | | |
| | | 1506 | | // All ASCII, 2 quotes for property name, 2 quotes for value, 1 colon, and 1 space => escapedPropertyName.Le |
| | | 1507 | | // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding |
| | 0 | 1508 | | int maxRequired = indent + ((escapedPropertyName.Length + escapedValue.Length) * JsonConstants.MaxExpansionF |
| | | 1509 | | |
| | 0 | 1510 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1511 | | { |
| | 0 | 1512 | | Grow(maxRequired); |
| | 0 | 1513 | | } |
| | | 1514 | | |
| | 0 | 1515 | | Span<byte> output = _memory.Span; |
| | | 1516 | | |
| | 0 | 1517 | | if (_currentDepth < 0) |
| | 0 | 1518 | | { |
| | 0 | 1519 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1520 | | } |
| | | 1521 | | |
| | 0 | 1522 | | Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName); |
| | | 1523 | | |
| | 0 | 1524 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 1525 | | { |
| | 0 | 1526 | | WriteNewLine(output); |
| | 0 | 1527 | | } |
| | | 1528 | | |
| | 0 | 1529 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 1530 | | BytesPending += indent; |
| | | 1531 | | |
| | 0 | 1532 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1533 | | |
| | 0 | 1534 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 1535 | | |
| | 0 | 1536 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1537 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 1538 | | output[BytesPending++] = JsonConstants.Space; |
| | | 1539 | | |
| | 0 | 1540 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1541 | | |
| | 0 | 1542 | | TranscodeAndWrite(escapedValue, output); |
| | | 1543 | | |
| | 0 | 1544 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1545 | | } |
| | | 1546 | | |
| | | 1547 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1548 | | private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue) |
| | 0 | 1549 | | { |
| | 0 | 1550 | | int indent = Indentation; |
| | 0 | 1551 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 1552 | | |
| | 0 | 1553 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1554 | | Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - escapedValue.Length - 7 - _newLineLength); |
| | | 1555 | | |
| | 0 | 1556 | | int minRequired = indent + escapedPropertyName.Length + escapedValue.Length + 6; // 2 quotes for property na |
| | 0 | 1557 | | int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li |
| | | 1558 | | |
| | 0 | 1559 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1560 | | { |
| | 0 | 1561 | | Grow(maxRequired); |
| | 0 | 1562 | | } |
| | | 1563 | | |
| | 0 | 1564 | | Span<byte> output = _memory.Span; |
| | | 1565 | | |
| | 0 | 1566 | | if (_currentDepth < 0) |
| | 0 | 1567 | | { |
| | 0 | 1568 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1569 | | } |
| | | 1570 | | |
| | 0 | 1571 | | Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName); |
| | | 1572 | | |
| | 0 | 1573 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 1574 | | { |
| | 0 | 1575 | | WriteNewLine(output); |
| | 0 | 1576 | | } |
| | | 1577 | | |
| | 0 | 1578 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 1579 | | BytesPending += indent; |
| | | 1580 | | |
| | 0 | 1581 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1582 | | |
| | 0 | 1583 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1584 | | BytesPending += escapedPropertyName.Length; |
| | | 1585 | | |
| | 0 | 1586 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1587 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 1588 | | output[BytesPending++] = JsonConstants.Space; |
| | | 1589 | | |
| | 0 | 1590 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1591 | | |
| | 0 | 1592 | | escapedValue.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1593 | | BytesPending += escapedValue.Length; |
| | | 1594 | | |
| | 0 | 1595 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1596 | | } |
| | | 1597 | | |
| | | 1598 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1599 | | private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue) |
| | 0 | 1600 | | { |
| | 0 | 1601 | | int indent = Indentation; |
| | 0 | 1602 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 1603 | | |
| | 0 | 1604 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1605 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) |
| | | 1606 | | |
| | | 1607 | | // All ASCII, 2 quotes for property name, 2 quotes for value, 1 colon, and 1 space => escapedPropertyName.Le |
| | | 1608 | | // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding |
| | 0 | 1609 | | int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + |
| | | 1610 | | |
| | 0 | 1611 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1612 | | { |
| | 0 | 1613 | | Grow(maxRequired); |
| | 0 | 1614 | | } |
| | | 1615 | | |
| | 0 | 1616 | | Span<byte> output = _memory.Span; |
| | | 1617 | | |
| | 0 | 1618 | | if (_currentDepth < 0) |
| | 0 | 1619 | | { |
| | 0 | 1620 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1621 | | } |
| | | 1622 | | |
| | 0 | 1623 | | Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName); |
| | | 1624 | | |
| | 0 | 1625 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 1626 | | { |
| | 0 | 1627 | | WriteNewLine(output); |
| | 0 | 1628 | | } |
| | | 1629 | | |
| | 0 | 1630 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 1631 | | BytesPending += indent; |
| | | 1632 | | |
| | 0 | 1633 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1634 | | |
| | 0 | 1635 | | TranscodeAndWrite(escapedPropertyName, output); |
| | | 1636 | | |
| | 0 | 1637 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1638 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 1639 | | output[BytesPending++] = JsonConstants.Space; |
| | | 1640 | | |
| | 0 | 1641 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1642 | | |
| | 0 | 1643 | | escapedValue.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1644 | | BytesPending += escapedValue.Length; |
| | | 1645 | | |
| | 0 | 1646 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1647 | | } |
| | | 1648 | | |
| | | 1649 | | // TODO: https://github.com/dotnet/runtime/issues/29293 |
| | | 1650 | | private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue) |
| | 0 | 1651 | | { |
| | 0 | 1652 | | int indent = Indentation; |
| | 0 | 1653 | | Debug.Assert(indent <= _indentLength * _options.MaxDepth); |
| | | 1654 | | |
| | 0 | 1655 | | Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize); |
| | 0 | 1656 | | Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) |
| | | 1657 | | |
| | | 1658 | | // All ASCII, 2 quotes for property name, 2 quotes for value, 1 colon, and 1 space => escapedPropertyName.Le |
| | | 1659 | | // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding |
| | 0 | 1660 | | int maxRequired = indent + (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escape |
| | | 1661 | | |
| | 0 | 1662 | | if (_memory.Length - BytesPending < maxRequired) |
| | 0 | 1663 | | { |
| | 0 | 1664 | | Grow(maxRequired); |
| | 0 | 1665 | | } |
| | | 1666 | | |
| | 0 | 1667 | | Span<byte> output = _memory.Span; |
| | | 1668 | | |
| | 0 | 1669 | | if (_currentDepth < 0) |
| | 0 | 1670 | | { |
| | 0 | 1671 | | output[BytesPending++] = JsonConstants.ListSeparator; |
| | 0 | 1672 | | } |
| | | 1673 | | |
| | 0 | 1674 | | Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName); |
| | | 1675 | | |
| | 0 | 1676 | | if (_tokenType != JsonTokenType.None) |
| | 0 | 1677 | | { |
| | 0 | 1678 | | WriteNewLine(output); |
| | 0 | 1679 | | } |
| | | 1680 | | |
| | 0 | 1681 | | WriteIndentation(output.Slice(BytesPending), indent); |
| | 0 | 1682 | | BytesPending += indent; |
| | | 1683 | | |
| | 0 | 1684 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1685 | | |
| | 0 | 1686 | | escapedPropertyName.CopyTo(output.Slice(BytesPending)); |
| | 0 | 1687 | | BytesPending += escapedPropertyName.Length; |
| | | 1688 | | |
| | 0 | 1689 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1690 | | output[BytesPending++] = JsonConstants.KeyValueSeparator; |
| | 0 | 1691 | | output[BytesPending++] = JsonConstants.Space; |
| | | 1692 | | |
| | 0 | 1693 | | output[BytesPending++] = JsonConstants.Quote; |
| | | 1694 | | |
| | 0 | 1695 | | TranscodeAndWrite(escapedValue, output); |
| | | 1696 | | |
| | 0 | 1697 | | output[BytesPending++] = JsonConstants.Quote; |
| | 0 | 1698 | | } |
| | | 1699 | | } |
| | | 1700 | | } |