< Summary

Information
Class: System.Text.Json.Utf8JsonWriter
Assembly: System.Text.Json
File(s): File 1: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.cs
File 2: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Bytes.cs
File 3: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTime.cs
File 4: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTimeOffset.cs
File 5: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Decimal.cs
File 6: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Double.cs
File 7: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Float.cs
File 8: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Guid.cs
File 9: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Helpers.cs
File 10: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Literal.cs
File 11: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.SignedNumber.cs
File 12: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.String.cs
File 13: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.UnsignedNumber.cs
File 14: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Bytes.cs
File 15: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Comment.cs
File 16: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTime.cs
File 17: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTimeOffset.cs
File 18: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Decimal.cs
File 19: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Double.cs
File 20: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Float.cs
File 21: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.FormattedNumber.cs
File 22: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Guid.cs
File 23: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Helpers.cs
File 24: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Literal.cs
File 25: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Raw.cs
File 26: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs
File 27: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs
File 28: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.StringSegment.cs
File 29: C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs
Line coverage
2%
Covered lines: 110
Uncovered lines: 5285
Coverable lines: 5395
Total lines: 10299
Line coverage: 2%
Branch coverage
1%
Covered branches: 23
Total branches: 1440
Branch coverage: 1.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 1: .ctor()100%11100%
File 1: .ctor(...)100%110%
File 1: .ctor(...)0%220%
File 1: SetOptions(...)83.33%66100%
File 1: Reset()0%220%
File 1: Reset(...)0%660%
File 1: Reset(...)0%220%
File 1: ResetAllStateForCacheReuse()100%11100%
File 1: Reset(...)50%44100%
File 1: CreateEmptyInstanceForCaching()100%11100%
File 1: ResetHelper()100%11100%
File 1: CheckNotDisposed()75%4475%
File 1: Flush()50%6653.84%
File 1: Dispose()0%440%
File 1: DisposeAsync()0%440%
File 1: FlushAsync()0%660%
File 1: WriteStartArray()100%110%
File 1: WriteStartObject()100%110%
File 1: WriteStart(...)0%440%
File 1: WriteStartMinimized(...)0%440%
File 1: WriteStartSlow(...)0%660%
File 1: ValidateStart()0%220%
File 1: OnValidateStartFailed()0%880%
File 1: WriteStartIndented(...)0%10100%
File 1: WriteStartArray(...)100%110%
File 1: WriteStartObject(...)100%110%
File 1: WriteStartHelper(...)100%110%
File 1: WriteStartArray(...)100%110%
File 1: WriteStartObject(...)100%110%
File 1: WriteStartEscape(...)0%440%
File 1: WriteStartByOptions(...)0%220%
File 1: WriteStartEscapeProperty(...)0%660%
File 1: WriteStartArray(...)100%110%
File 1: WriteStartObject(...)100%110%
File 1: WriteStartArray(...)100%110%
File 1: WriteStartObject(...)100%110%
File 1: WriteStartEscape(...)0%440%
File 1: WriteStartByOptions(...)0%220%
File 1: WriteStartEscapeProperty(...)0%660%
File 1: WriteEndArray()100%110%
File 1: WriteEndObject()100%110%
File 1: WriteEnd(...)0%440%
File 1: WriteEndMinimized(...)0%220%
File 1: WriteEndSlow(...)0%660%
File 1: ValidateEnd(...)0%12120%
File 1: WriteEndIndented(...)0%10100%
File 1: WriteNewLine(...)0%440%
File 1: WriteIndentation(...)100%110%
File 1: UpdateBitStackOnStart(...)0%220%
File 1: Grow(...)16.66%6625.92%
File 1: FirstCallToGetMemory(...)50%4461.11%
File 1: SetFlagToAddListSeparatorBeforeNextItem()100%11100%
File 1: ThrowInvalidOperationException(...)100%110%
File 1: ThrowInvalidOperationException_MismatchedObjectArray(...)100%110%
File 1: ThrowInvalidOperationException_DepthTooLarge()100%110%
File 2: WriteBase64String(...)100%110%
File 2: WriteBase64String(...)100%110%
File 2: WriteBase64String(...)100%110%
File 2: WriteBase64String(...)100%110%
File 2: WriteBase64Escape(...)0%440%
File 2: WriteBase64Escape(...)0%440%
File 2: WriteBase64EscapeProperty(...)0%660%
File 2: WriteBase64EscapeProperty(...)0%660%
File 2: WriteBase64ByOptions(...)0%220%
File 2: WriteBase64ByOptions(...)0%220%
File 2: WriteBase64Minimized(...)0%440%
File 2: WriteBase64Minimized(...)0%440%
File 2: WriteBase64Indented(...)0%880%
File 2: WriteBase64Indented(...)0%880%
File 3: WriteString(...)100%110%
File 3: WriteString(...)100%110%
File 3: WriteString(...)100%110%
File 3: WriteString(...)100%110%
File 3: WriteStringEscape(...)0%440%
File 3: WriteStringEscape(...)0%440%
File 3: WriteStringEscapeProperty(...)0%660%
File 3: WriteStringEscapeProperty(...)0%660%
File 3: WriteStringByOptions(...)0%220%
File 3: WriteStringByOptions(...)0%220%
File 3: WriteStringMinimized(...)0%440%
File 3: WriteStringMinimized(...)0%440%
File 3: WriteStringIndented(...)0%880%
File 3: WriteStringIndented(...)0%880%
File 3: WritePropertyName(...)100%110%
File 4: WriteString(...)100%110%
File 4: WriteString(...)100%110%
File 4: WriteString(...)100%110%
File 4: WriteString(...)100%110%
File 4: WriteStringEscape(...)0%440%
File 4: WriteStringEscape(...)0%440%
File 4: WriteStringEscapeProperty(...)0%660%
File 4: WriteStringEscapeProperty(...)0%660%
File 4: WriteStringByOptions(...)0%220%
File 4: WriteStringByOptions(...)0%220%
File 4: WriteStringMinimized(...)0%440%
File 4: WriteStringMinimized(...)0%440%
File 4: WriteStringIndented(...)0%880%
File 4: WriteStringIndented(...)0%880%
File 4: WritePropertyName(...)100%110%
File 5: WriteNumber(...)100%110%
File 5: WriteNumber(...)100%110%
File 5: WriteNumber(...)100%110%
File 5: WriteNumber(...)100%110%
File 5: WriteNumberEscape(...)0%440%
File 5: WriteNumberEscape(...)0%440%
File 5: WriteNumberEscapeProperty(...)0%660%
File 5: WriteNumberEscapeProperty(...)0%660%
File 5: WriteNumberByOptions(...)0%220%
File 5: WriteNumberByOptions(...)0%220%
File 5: WriteNumberMinimized(...)0%440%
File 5: WriteNumberMinimized(...)0%440%
File 5: WriteNumberIndented(...)0%880%
File 5: WriteNumberIndented(...)0%880%
File 5: WritePropertyName(...)100%110%
File 6: WriteNumber(...)100%110%
File 6: WriteNumber(...)100%110%
File 6: WriteNumber(...)100%110%
File 6: WriteNumber(...)100%110%
File 6: WriteNumberEscape(...)0%440%
File 6: WriteNumberEscape(...)0%440%
File 6: WriteNumberEscapeProperty(...)0%660%
File 6: WriteNumberEscapeProperty(...)0%660%
File 6: WriteNumberByOptions(...)0%220%
File 6: WriteNumberByOptions(...)0%220%
File 6: WriteNumberMinimized(...)0%440%
File 6: WriteNumberMinimized(...)0%440%
File 6: WriteNumberIndented(...)0%880%
File 6: WriteNumberIndented(...)0%880%
File 6: WritePropertyName(...)100%110%
File 7: WriteNumber(...)100%110%
File 7: WriteNumber(...)100%110%
File 7: WriteNumber(...)100%110%
File 7: WriteNumber(...)100%110%
File 7: WriteNumberEscape(...)0%440%
File 7: WriteNumberEscape(...)0%440%
File 7: WriteNumberEscapeProperty(...)0%660%
File 7: WriteNumberEscapeProperty(...)0%660%
File 7: WriteNumberByOptions(...)0%220%
File 7: WriteNumberByOptions(...)0%220%
File 7: WriteNumberMinimized(...)0%440%
File 7: WriteNumberMinimized(...)0%440%
File 7: WriteNumberIndented(...)0%880%
File 7: WriteNumberIndented(...)0%880%
File 7: WritePropertyName(...)100%110%
File 8: WriteString(...)100%110%
File 8: WriteString(...)100%110%
File 8: WriteString(...)100%110%
File 8: WriteString(...)100%110%
File 8: WriteStringEscape(...)0%440%
File 8: WriteStringEscape(...)0%440%
File 8: WriteStringEscapeProperty(...)0%660%
File 8: WriteStringEscapeProperty(...)0%660%
File 8: WriteStringByOptions(...)0%220%
File 8: WriteStringByOptions(...)0%220%
File 8: WriteStringMinimized(...)0%440%
File 8: WriteStringMinimized(...)0%440%
File 8: WriteStringIndented(...)0%880%
File 8: WriteStringIndented(...)0%880%
File 8: WritePropertyName(...)100%110%
File 9: ValidatePropertyNameAndDepth(...)0%440%
File 9: ValidatePropertyNameAndDepth(...)0%440%
File 9: ValidateDepth()0%220%
File 9: ValidateWritingProperty()0%660%
File 9: ValidateWritingProperty(...)0%660%
File 9: OnValidateWritingPropertyFailed()0%440%
File 9: WritePropertyNameMinimized(...)0%440%
File 9: WritePropertyNameIndented(...)0%880%
File 9: WritePropertyNameMinimized(...)0%440%
File 9: WritePropertyNameIndented(...)0%880%
File 9: TranscodeAndWrite(...)100%110%
File 10: WriteNull(...)100%110%
File 10: WriteNullSection(...)0%220%
File 10: WriteLiteralHelper(...)100%110%
File 10: WriteNull(...)100%110%
File 10: WriteNull(...)100%110%
File 10: WriteNull(...)100%110%
File 10: WriteBoolean(...)0%220%
File 10: WriteBoolean(...)100%110%
File 10: WriteBoolean(...)0%440%
File 10: WriteBoolean(...)0%440%
File 10: WriteLiteralEscape(...)0%440%
File 10: WriteLiteralEscape(...)0%440%
File 10: WriteLiteralEscapeProperty(...)0%660%
File 10: WriteLiteralEscapeProperty(...)0%660%
File 10: WriteLiteralByOptions(...)0%220%
File 10: WriteLiteralByOptions(...)0%220%
File 10: WriteLiteralMinimized(...)0%440%
File 10: WriteLiteralMinimized(...)0%440%
File 10: WriteLiteralSection(...)0%440%
File 10: WriteLiteralIndented(...)0%880%
File 10: WriteLiteralIndented(...)0%880%
File 10: WritePropertyName(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumber(...)100%110%
File 11: WriteNumberEscape(...)0%440%
File 11: WriteNumberEscape(...)0%440%
File 11: WriteNumberEscapeProperty(...)0%660%
File 11: WriteNumberEscapeProperty(...)0%660%
File 11: WriteNumberByOptions(...)0%220%
File 11: WriteNumberByOptions(...)0%220%
File 11: WriteNumberMinimized(...)0%440%
File 11: WriteNumberMinimized(...)0%440%
File 11: WriteNumberIndented(...)0%880%
File 11: WriteNumberIndented(...)0%880%
File 11: WritePropertyName(...)100%110%
File 11: WritePropertyName(...)100%110%
File 12: WritePropertyName(...)100%110%
File 12: WritePropertyNameSection(...)0%220%
File 12: WritePropertyNameHelper(...)100%110%
File 12: WritePropertyName(...)100%110%
File 12: WritePropertyName(...)0%660%
File 12: WriteStringEscapeProperty(...)0%660%
File 12: WriteStringByOptionsPropertyName(...)0%220%
File 12: WriteStringMinimizedPropertyName(...)0%440%
File 12: WriteStringIndentedPropertyName(...)0%660%
File 12: WritePropertyName(...)0%660%
File 12: WritePropertyNameUnescaped(...)100%110%
File 12: WriteStringEscapeProperty(...)0%660%
File 12: WriteStringByOptionsPropertyName(...)0%220%
File 12: WriteStringMinimizedPropertyName(...)0%440%
File 12: WriteStringPropertyNameSection(...)0%440%
File 12: WriteStringIndentedPropertyName(...)0%880%
File 12: WriteString(...)100%110%
File 12: WriteStringHelper(...)0%220%
File 12: WriteString(...)100%110%
File 12: WriteString(...)0%220%
File 12: WriteString(...)100%110%
File 12: WriteString(...)100%110%
File 12: WriteString(...)0%220%
File 12: WriteString(...)100%110%
File 12: WriteStringHelperEscapeValue(...)0%660%
File 12: WriteString(...)100%110%
File 12: WriteString(...)100%110%
File 12: WriteString(...)100%110%
File 12: WriteStringHelperEscapeValue(...)0%660%
File 12: WriteString(...)100%110%
File 12: WriteString(...)100%110%
File 12: WriteString(...)100%110%
File 12: WriteStringHelperEscapeProperty(...)0%660%
File 12: WriteString(...)0%220%
File 12: WriteString(...)100%110%
File 12: WriteStringHelperEscapeProperty(...)0%660%
File 12: WriteString(...)0%220%
File 12: WriteStringEscapeValueOnly(...)0%660%
File 12: WriteStringEscapeValueOnly(...)0%660%
File 12: WriteStringEscapePropertyOnly(...)0%660%
File 12: WriteStringEscapePropertyOnly(...)0%660%
File 12: WriteStringEscape(...)0%10100%
File 12: WriteStringEscape(...)0%10100%
File 12: WriteStringEscape(...)0%10100%
File 12: WriteStringEscape(...)0%10100%
File 12: WriteStringEscapePropertyOrValue(...)0%12120%
File 12: WriteStringEscapePropertyOrValue(...)0%12120%
File 12: WriteStringEscapePropertyOrValue(...)0%12120%
File 12: WriteStringEscapePropertyOrValue(...)0%12120%
File 12: WriteStringByOptions(...)0%220%
File 12: WriteStringByOptions(...)0%220%
File 12: WriteStringByOptions(...)0%220%
File 12: WriteStringByOptions(...)0%220%
File 12: WriteStringMinimized(...)0%440%
File 12: WriteStringMinimized(...)0%440%
File 12: WriteStringMinimized(...)0%440%
File 12: WriteStringMinimized(...)0%440%
File 12: WriteStringIndented(...)0%880%
File 12: WriteStringIndented(...)0%880%
File 12: WriteStringIndented(...)0%880%
File 12: WriteStringIndented(...)0%880%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumber(...)100%110%
File 13: WriteNumberEscape(...)0%440%
File 13: WriteNumberEscape(...)0%440%
File 13: WriteNumberEscapeProperty(...)0%660%
File 13: WriteNumberEscapeProperty(...)0%660%
File 13: WriteNumberByOptions(...)0%220%
File 13: WriteNumberByOptions(...)0%220%
File 13: WriteNumberMinimized(...)0%440%
File 13: WriteNumberMinimized(...)0%440%
File 13: WriteNumberIndented(...)0%880%
File 13: WriteNumberIndented(...)0%880%
File 13: WritePropertyName(...)100%110%
File 13: WritePropertyName(...)100%110%
File 14: WriteBase64StringValue(...)100%110%
File 14: WriteBase64ByOptions(...)0%440%
File 14: WriteBase64Minimized(...)0%660%
File 14: WriteBase64Indented(...)0%10100%
File 15: .cctor()100%110%
File 15: WriteCommentValue(...)100%110%
File 15: WriteCommentValue(...)0%880%
File 15: WriteCommentByOptions(...)0%440%
File 15: WriteCommentMinimized(...)0%440%
File 15: WriteCommentIndented(...)0%880%
File 15: WriteCommentValue(...)0%10100%
File 15: WriteCommentByOptions(...)0%220%
File 15: WriteCommentMinimized(...)0%220%
File 15: WriteCommentIndented(...)0%660%
File 16: WriteStringValue(...)0%440%
File 16: WriteStringValueMinimized(...)0%440%
File 16: WriteStringValueIndented(...)0%880%
File 17: WriteStringValue(...)0%440%
File 17: WriteStringValueMinimized(...)0%440%
File 17: WriteStringValueIndented(...)0%880%
File 18: WriteNumberValue(...)0%440%
File 18: WriteNumberValueMinimized(...)0%440%
File 18: WriteNumberValueIndented(...)0%880%
File 18: WriteNumberValueAsString(...)100%110%
File 19: WriteNumberValue(...)0%440%
File 19: WriteNumberValueMinimized(...)0%440%
File 19: WriteNumberValueIndented(...)0%880%
File 19: TryFormatDouble(...)100%110%
File 19: WriteNumberValueAsString(...)100%110%
File 19: WriteFloatingPointConstant(...)0%660%
File 20: WriteNumberValue(...)0%440%
File 20: WriteNumberValueMinimized(...)0%440%
File 20: WriteNumberValueIndented(...)0%880%
File 20: TryFormatSingle(...)100%110%
File 20: WriteNumberValueAsString(...)100%110%
File 20: WriteFloatingPointConstant(...)0%660%
File 21: WriteNumberValue(...)75%4482.35%
File 21: WriteNumberValueMinimized(...)75%4478.57%
File 21: WriteNumberValueIndented(...)0%880%
File 22: WriteStringValue(...)0%440%
File 22: WriteStringValueMinimized(...)0%440%
File 22: WriteStringValueIndented(...)0%880%
File 23: ClearPartialStringData()100%110%
File 23: ValidateWritingValue()50%2260%
File 23: OnValidateWritingValueFailed()0%880%
File 23: ValidateWritingSegment(...)0%10100%
File 23: OnValidateWritingSegmentFailed(...)0%880%
File 23: ValidateNotWithinUnfinalizedString()0%220%
File 23: Base64EncodeAndWrite(...)100%110%
File 24: WriteNullValue()100%110%
File 24: WriteBooleanValue(...)0%220%
File 24: WriteLiteralByOptions(...)0%440%
File 24: WriteLiteralMinimized(...)0%440%
File 24: WriteLiteralIndented(...)0%880%
File 25: WriteRawValue(...)0%220%
File 25: WriteRawValue(...)0%220%
File 25: WriteRawValue(...)0%440%
File 25: WriteRawValue(...)0%14140%
File 25: TranscodeAndWriteRawValue(...)0%880%
File 25: WriteRawValueCore(...)0%10100%
File 26: WriteNumberValue(...)100%110%
File 26: WriteNumberValue(...)0%440%
File 26: WriteNumberValueMinimized(...)0%440%
File 26: WriteNumberValueIndented(...)0%880%
File 26: WriteNumberValueAsString(...)100%110%
File 27: WriteStringValue(...)100%110%
File 27: WriteStringValue(...)0%220%
File 27: WriteStringValue(...)100%110%
File 27: WriteStringEscape(...)0%440%
File 27: WriteStringByOptions(...)0%440%
File 27: WriteStringMinimized(...)0%440%
File 27: WriteStringIndented(...)0%880%
File 27: WriteStringEscapeValue(...)0%660%
File 27: WriteStringValue(...)100%110%
File 27: WriteStringEscape(...)0%440%
File 27: WriteStringByOptions(...)0%440%
File 27: WriteStringMinimized(...)0%440%
File 27: WriteStringIndented(...)0%880%
File 27: WriteStringEscapeValue(...)0%660%
File 27: WriteNumberValueAsStringUnescaped(...)100%110%
File 28: WriteStringValueSegment(...)0%12120%
File 28: WriteStringSegmentWithLeftover(...)0%550%
File 28: WriteStringSegmentEscape(...)0%440%
File 28: WriteStringSegmentEscapeValue(...)0%10100%
File 28: WriteStringSegmentData(...)0%220%
File 28: WriteStringValueSegment(...)0%12120%
File 28: WriteStringSegmentWithLeftover(...)0%550%
File 28: WriteStringSegmentEscape(...)0%440%
File 28: WriteStringSegmentEscapeValue(...)0%10100%
File 28: WriteStringSegmentData(...)0%220%
File 28: WriteBase64StringSegment(...)0%14140%
File 28: WriteBase64StringSegmentWithLeftover(...)0%440%
File 28: WriteBase64StringSegmentData(...)0%880%
File 28: WriteStringSegmentPrologue()0%220%
File 28: WriteStringSegmentIndentedPrologue()0%880%
File 28: WriteStringSegmentMinimizedPrologue()0%440%
File 28: WriteStringSegmentEpilogue()0%220%
File 28: ConcatInto(...)0%880%
File 29: WriteNumberValue(...)100%110%
File 29: WriteNumberValue(...)0%440%
File 29: WriteNumberValueMinimized(...)0%440%
File 29: WriteNumberValueIndented(...)0%880%
File 29: WriteNumberValueAsString(...)100%110%

File(s)

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.IO;
 7using System.Runtime.CompilerServices;
 8using System.Runtime.InteropServices;
 9using System.Threading;
 10using System.Threading.Tasks;
 11using System.Diagnostics.CodeAnalysis;
 12
 13namespace System.Text.Json
 14{
 15    /// <summary>
 16    /// Provides a high-performance API for forward-only, non-cached writing of UTF-8 encoded JSON text.
 17    /// </summary>
 18    /// <remarks>
 19    ///   <para>
 20    ///     It writes the text sequentially with no caching and adheres to the JSON RFC
 21    ///     by default (https://tools.ietf.org/html/rfc8259), with the exception of writing comments.
 22    ///   </para>
 23    ///   <para>
 24    ///     When the user attempts to write invalid JSON and validation is enabled, it throws
 25    ///     an <see cref="InvalidOperationException"/> with a context specific error message.
 26    ///   </para>
 27    ///   <para>
 28    ///     To be able to format the output with indentation and whitespace OR to skip validation, create an instance of
 29    ///     <see cref="JsonWriterOptions"/> and pass that in to the writer.
 30    ///   </para>
 31    /// </remarks>
 32    [DebuggerDisplay("{DebuggerDisplay,nq}")]
 33    public sealed partial class Utf8JsonWriter : IDisposable, IAsyncDisposable
 34    {
 35        private const int DefaultGrowthSize = 4096;
 36        private const int InitialGrowthSize = 256;
 37
 38        private IBufferWriter<byte>? _output;
 39        private Stream? _stream;
 40        private ArrayBufferWriter<byte>? _arrayBufferWriter;
 41
 42        private Memory<byte> _memory;
 43
 44        private EnclosingContainerType _enclosingContainer;
 45        private bool _commentAfterNoneOrPropertyName;
 46        private JsonTokenType _tokenType;
 47        private BitStack _bitStack;
 48
 49        /// <summary>
 50        /// This 3-byte array stores the partial string data leftover when writing a string value
 51        /// segment that is split across multiple segment write calls.
 52        /// </summary>
 53#if !NET
 54        private byte[]? _partialStringData;
 55        private Span<byte> PartialStringDataRaw => _partialStringData ??= new byte[3];
 56#else
 57        private Inline3ByteArray _partialStringData;
 058        private Span<byte> PartialStringDataRaw => _partialStringData;
 59
 60        [InlineArray(3)]
 61        private struct Inline3ByteArray
 62        {
 63            public byte byte0;
 64        }
 65#endif
 66
 67        /// <summary>
 68        /// Length of the partial string data.
 69        /// </summary>
 70        private byte _partialStringDataLength;
 71
 72        // The highest order bit of _currentDepth is used to discern whether we are writing the first item in a list or 
 73        // if (_currentDepth >> 31) == 1, add a list separator before writing the item
 74        // else, no list separator is needed since we are writing the first item.
 75        private int _currentDepth;
 76
 77        private JsonWriterOptions _options; // Since JsonWriterOptions is a struct, use a field to avoid a copy for inte
 78
 79        // Cache indentation settings from JsonWriterOptions to avoid recomputing them in the hot path.
 80        private byte _indentByte;
 81        private int _indentLength;
 82
 83        // A length of 1 will emit LF for indented writes, a length of 2 will emit CRLF. Other values are invalid.
 84        private int _newLineLength;
 85
 86        /// <summary>
 87        /// Returns the amount of bytes written by the <see cref="Utf8JsonWriter"/> so far
 88        /// that have not yet been flushed to the output and committed.
 89        /// </summary>
 28090        public int BytesPending { get; private set; }
 91
 92        /// <summary>
 93        /// Returns the amount of bytes committed to the output by the <see cref="Utf8JsonWriter"/> so far.
 94        /// </summary>
 95        /// <remarks>
 96        /// In the case of IBufferwriter, this is how much the IBufferWriter has advanced.
 97        /// In the case of Stream, this is how much data has been written to the stream.
 98        /// </remarks>
 8499        public long BytesCommitted { get; private set; }
 100
 101        /// <summary>
 102        /// Gets the custom behavior when writing JSON using
 103        /// the <see cref="Utf8JsonWriter"/> which indicates whether to format the output
 104        /// while writing and whether to skip structural JSON validation or not.
 105        /// </summary>
 0106        public JsonWriterOptions Options => _options;
 107
 0108        private int Indentation => CurrentDepth * _indentLength;
 109
 0110        internal JsonTokenType TokenType => _tokenType;
 111
 112        /// <summary>
 113        /// Tracks the recursive depth of the nested objects / arrays within the JSON text
 114        /// written so far. This provides the depth of the current token.
 115        /// </summary>
 0116        public int CurrentDepth => _currentDepth & JsonConstants.RemoveFlagsBitMask;
 117
 118        /// <summary>
 119        /// The partial UTF-8 code point.
 120        /// </summary>
 121        private ReadOnlySpan<byte> PartialUtf8StringData
 122        {
 123            get
 0124            {
 0125                Debug.Assert(_enclosingContainer == EnclosingContainerType.Utf8StringSequence);
 126
 0127                ReadOnlySpan<byte> partialStringDataBytes = PartialStringDataRaw;
 0128                Debug.Assert(partialStringDataBytes.Length == 3);
 129
 0130                byte length = _partialStringDataLength;
 0131                Debug.Assert(length < 4);
 132
 0133                return partialStringDataBytes.Slice(0, length);
 0134            }
 135
 136            set
 0137            {
 0138                Debug.Assert(value.Length <= 3);
 139
 0140                Span<byte> partialStringDataBytes = PartialStringDataRaw;
 141
 0142                value.CopyTo(partialStringDataBytes);
 0143                _partialStringDataLength = (byte)value.Length;
 0144            }
 145        }
 146
 147        /// <summary>
 148        /// The partial UTF-16 code point.
 149        /// </summary>
 150        private ReadOnlySpan<char> PartialUtf16StringData
 151        {
 152            get
 0153            {
 0154                Debug.Assert(_enclosingContainer == EnclosingContainerType.Utf16StringSequence);
 155
 0156                ReadOnlySpan<byte> partialStringDataBytes = PartialStringDataRaw;
 0157                Debug.Assert(partialStringDataBytes.Length == 3);
 158
 0159                byte length = _partialStringDataLength;
 0160                Debug.Assert(length is 2 or 0);
 161
 0162                return MemoryMarshal.Cast<byte, char>(partialStringDataBytes.Slice(0, length));
 0163            }
 164            set
 0165            {
 0166                Debug.Assert(value.Length <= 1);
 167
 0168                Span<byte> partialStringDataBytes = PartialStringDataRaw;
 169
 0170                value.CopyTo(MemoryMarshal.Cast<byte, char>(partialStringDataBytes));
 0171                _partialStringDataLength = (byte)(2 * value.Length);
 0172            }
 173        }
 174
 175        /// <summary>
 176        /// The partial base64 data.
 177        /// </summary>
 178        private ReadOnlySpan<byte> PartialBase64StringData
 179        {
 180            get
 0181            {
 0182                Debug.Assert(_enclosingContainer == EnclosingContainerType.Base64StringSequence);
 183
 0184                ReadOnlySpan<byte> partialStringDataBytes = PartialStringDataRaw;
 0185                Debug.Assert(partialStringDataBytes.Length == 3);
 186
 0187                byte length = _partialStringDataLength;
 0188                Debug.Assert(length < 3);
 189
 0190                return partialStringDataBytes.Slice(0, length);
 0191            }
 192            set
 0193            {
 0194                Debug.Assert(value.Length < 3);
 195
 0196                Span<byte> partialStringDataBytes = PartialStringDataRaw;
 197
 0198                value.CopyTo(partialStringDataBytes);
 0199                _partialStringDataLength = (byte)value.Length;
 0200            }
 201        }
 202
 1203        private Utf8JsonWriter()
 1204        {
 1205        }
 206
 207        /// <summary>
 208        /// Constructs a new <see cref="Utf8JsonWriter"/> instance with a specified <paramref name="bufferWriter"/>.
 209        /// </summary>
 210        /// <param name="bufferWriter">An instance of <see cref="IBufferWriter{Byte}" /> used as a destination for writi
 211        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonWriter"/>
 212        /// By default, the <see cref="Utf8JsonWriter"/> writes JSON minimized (that is, with no extra whitespace)
 213        /// and validates that the JSON being written is structurally valid according to JSON RFC.</param>
 214        /// <exception cref="ArgumentNullException">
 215        /// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null.
 216        /// </exception>
 0217        public Utf8JsonWriter(IBufferWriter<byte> bufferWriter, JsonWriterOptions options = default)
 0218        {
 0219            ArgumentNullException.ThrowIfNull(bufferWriter);
 220
 0221            _output = bufferWriter;
 0222            SetOptions(options);
 0223        }
 224
 225        /// <summary>
 226        /// Constructs a new <see cref="Utf8JsonWriter"/> instance with a specified <paramref name="utf8Json"/>.
 227        /// </summary>
 228        /// <param name="utf8Json">An instance of <see cref="Stream" /> used as a destination for writing JSON text into
 229        /// <param name="options">Defines the customized behavior of the <see cref="Utf8JsonWriter"/>
 230        /// By default, the <see cref="Utf8JsonWriter"/> writes JSON minimized (that is, with no extra whitespace)
 231        /// and validates that the JSON being written is structurally valid according to JSON RFC.</param>
 232        /// <exception cref="ArgumentNullException">
 233        /// Thrown when the instance of <see cref="Stream" /> that is passed in is null.
 234        /// </exception>
 0235        public Utf8JsonWriter(Stream utf8Json, JsonWriterOptions options = default)
 0236        {
 0237            ArgumentNullException.ThrowIfNull(utf8Json);
 238
 0239            if (!utf8Json.CanWrite)
 0240                throw new ArgumentException(SR.StreamNotWritable);
 241
 0242            _stream = utf8Json;
 0243            SetOptions(options);
 244
 0245            _arrayBufferWriter = new ArrayBufferWriter<byte>();
 0246        }
 247
 248        private void SetOptions(JsonWriterOptions options)
 28249        {
 28250            _options = options;
 28251            _indentByte = (byte)_options.IndentCharacter;
 28252            _indentLength = options.IndentSize;
 253
 28254            Debug.Assert(options.NewLine is "\n" or "\r\n", "Invalid NewLine string.");
 28255            _newLineLength = options.NewLine.Length;
 256
 28257            if (_options.MaxDepth == 0)
 28258            {
 28259                _options.MaxDepth = JsonWriterOptions.DefaultMaxDepth; // If max depth is not set, revert to the default
 28260            }
 28261        }
 262
 263        /// <summary>
 264        /// Resets the <see cref="Utf8JsonWriter"/> internal state so that it can be re-used.
 265        /// </summary>
 266        /// <remarks>
 267        /// The <see cref="Utf8JsonWriter"/> will continue to use the original writer options
 268        /// and the original output as the destination (either <see cref="IBufferWriter{Byte}" /> or <see cref="Stream" 
 269        /// </remarks>
 270        /// <exception cref="ObjectDisposedException">
 271        ///   The instance of <see cref="Utf8JsonWriter"/> has been disposed.
 272        /// </exception>
 273        public void Reset()
 0274        {
 0275            CheckNotDisposed();
 276
 0277            _arrayBufferWriter?.Clear();
 0278            ResetHelper();
 0279        }
 280
 281        /// <summary>
 282        /// Resets the <see cref="Utf8JsonWriter"/> internal state so that it can be re-used with the new instance of <s
 283        /// </summary>
 284        /// <param name="utf8Json">An instance of <see cref="Stream" /> used as a destination for writing JSON text into
 285        /// <remarks>
 286        /// The <see cref="Utf8JsonWriter"/> will continue to use the original writer options
 287        /// but now write to the passed in <see cref="Stream" /> as the new destination.
 288        /// </remarks>
 289        /// <exception cref="ArgumentNullException">
 290        /// Thrown when the instance of <see cref="Stream" /> that is passed in is null.
 291        /// </exception>
 292        /// <exception cref="ObjectDisposedException">
 293        ///   The instance of <see cref="Utf8JsonWriter"/> has been disposed.
 294        /// </exception>
 295        public void Reset(Stream utf8Json)
 0296        {
 0297            CheckNotDisposed();
 298
 0299            if (utf8Json == null)
 0300                throw new ArgumentNullException(nameof(utf8Json));
 0301            if (!utf8Json.CanWrite)
 0302                throw new ArgumentException(SR.StreamNotWritable);
 303
 0304            _stream = utf8Json;
 0305            if (_arrayBufferWriter == null)
 0306            {
 0307                _arrayBufferWriter = new ArrayBufferWriter<byte>();
 0308            }
 309            else
 0310            {
 0311                _arrayBufferWriter.Clear();
 0312            }
 0313            _output = null;
 314
 0315            ResetHelper();
 0316        }
 317
 318        /// <summary>
 319        /// Resets the <see cref="Utf8JsonWriter"/> internal state so that it can be re-used with the new instance of <s
 320        /// </summary>
 321        /// <param name="bufferWriter">An instance of <see cref="IBufferWriter{Byte}" /> used as a destination for writi
 322        /// <remarks>
 323        /// The <see cref="Utf8JsonWriter"/> will continue to use the original writer options
 324        /// but now write to the passed in <see cref="IBufferWriter{Byte}" /> as the new destination.
 325        /// </remarks>
 326        /// <exception cref="ArgumentNullException">
 327        /// Thrown when the instance of <see cref="IBufferWriter{Byte}" /> that is passed in is null.
 328        /// </exception>
 329        /// <exception cref="ObjectDisposedException">
 330        ///   The instance of <see cref="Utf8JsonWriter"/> has been disposed.
 331        /// </exception>
 332        public void Reset(IBufferWriter<byte> bufferWriter)
 0333        {
 0334            CheckNotDisposed();
 335
 0336            _output = bufferWriter ?? throw new ArgumentNullException(nameof(bufferWriter));
 0337            _stream = null;
 0338            _arrayBufferWriter = null;
 339
 0340            ResetHelper();
 0341        }
 342
 343        internal void ResetAllStateForCacheReuse()
 28344        {
 28345            ResetHelper();
 346
 28347            _stream = null;
 28348            _arrayBufferWriter = null;
 28349            _output = null;
 28350        }
 351
 352        internal void Reset(IBufferWriter<byte> bufferWriter, JsonWriterOptions options)
 28353        {
 28354            Debug.Assert(_output is null && _stream is null && _arrayBufferWriter is null);
 355
 28356            _output = bufferWriter;
 28357            SetOptions(options);
 28358        }
 359
 1360        internal static Utf8JsonWriter CreateEmptyInstanceForCaching() => new Utf8JsonWriter();
 361
 362        private void ResetHelper()
 28363        {
 28364            BytesPending = default;
 28365            BytesCommitted = default;
 28366            _memory = default;
 367
 28368            _enclosingContainer = default;
 28369            _tokenType = default;
 28370            _commentAfterNoneOrPropertyName = default;
 28371            _currentDepth = default;
 372
 28373            _bitStack = default;
 374
 28375            _partialStringData = default;
 28376            _partialStringDataLength = default;
 28377        }
 378
 379        private void CheckNotDisposed()
 28380        {
 28381            if (_stream == null)
 28382            {
 383                // The conditions are ordered with stream first as that would be the most common mode
 28384                if (_output == null)
 0385                {
 0386                    ThrowHelper.ThrowObjectDisposedException_Utf8JsonWriter();
 387                }
 28388            }
 28389        }
 390
 391        /// <summary>
 392        /// Commits the JSON text written so far which makes it visible to the output destination.
 393        /// </summary>
 394        /// <remarks>
 395        /// In the case of IBufferWriter, this advances the underlying <see cref="IBufferWriter{Byte}" /> based on what 
 396        /// In the case of Stream, this writes the data to the stream and flushes it.
 397        /// </remarks>
 398        /// <exception cref="ObjectDisposedException">
 399        ///   The instance of <see cref="Utf8JsonWriter"/> has been disposed.
 400        /// </exception>
 401        public void Flush()
 28402        {
 28403            CheckNotDisposed();
 404
 28405            _memory = default;
 406
 28407            if (_stream != null)
 0408            {
 0409                Debug.Assert(_arrayBufferWriter != null);
 0410                if (BytesPending != 0)
 0411                {
 0412                    _arrayBufferWriter.Advance(BytesPending);
 0413                    BytesPending = 0;
 414
 415#if NET
 0416                    _stream.Write(_arrayBufferWriter.WrittenSpan);
 417#else
 418                    _stream.Write(_arrayBufferWriter.WrittenMemory);
 419#endif
 420
 0421                    BytesCommitted += _arrayBufferWriter.WrittenCount;
 0422                    _arrayBufferWriter.Clear();
 0423                }
 0424                _stream.Flush();
 0425            }
 426            else
 28427            {
 28428                Debug.Assert(_output != null);
 28429                if (BytesPending != 0)
 28430                {
 28431                    _output.Advance(BytesPending);
 28432                    BytesCommitted += BytesPending;
 28433                    BytesPending = 0;
 28434                }
 28435            }
 28436        }
 437
 438        /// <summary>
 439        /// Commits any left over JSON text that has not yet been flushed and releases all resources used by the current
 440        /// </summary>
 441        /// <remarks>
 442        ///   <para>
 443        ///     In the case of IBufferWriter, this advances the underlying <see cref="IBufferWriter{Byte}" /> based on w
 444        ///     In the case of Stream, this writes the data to the stream and flushes it.
 445        ///   </para>
 446        ///   <para>
 447        ///     The <see cref="Utf8JsonWriter"/> instance cannot be re-used after disposing.
 448        ///   </para>
 449        /// </remarks>
 450        public void Dispose()
 0451        {
 0452            if (_stream == null)
 0453            {
 454                // The conditions are ordered with stream first as that would be the most common mode
 0455                if (_output == null)
 0456                {
 0457                    return;
 458                }
 0459            }
 460
 0461            Flush();
 0462            ResetHelper();
 463
 0464            _stream = null;
 0465            _arrayBufferWriter = null;
 0466            _output = null;
 0467        }
 468
 469        /// <summary>
 470        /// Asynchronously commits any left over JSON text that has not yet been flushed and releases all resources used
 471        /// </summary>
 472        /// <remarks>
 473        ///   <para>
 474        ///     In the case of IBufferWriter, this advances the underlying <see cref="IBufferWriter{Byte}" /> based on w
 475        ///     In the case of Stream, this writes the data to the stream and flushes it.
 476        ///   </para>
 477        ///   <para>
 478        ///     The <see cref="Utf8JsonWriter"/> instance cannot be re-used after disposing.
 479        ///   </para>
 480        /// </remarks>
 481        public async ValueTask DisposeAsync()
 0482        {
 0483            if (_stream == null)
 0484            {
 485                // The conditions are ordered with stream first as that would be the most common mode
 0486                if (_output == null)
 0487                {
 0488                    return;
 489                }
 0490            }
 491
 0492            await FlushAsync().ConfigureAwait(false);
 0493            ResetHelper();
 494
 0495            _stream = null;
 0496            _arrayBufferWriter = null;
 0497            _output = null;
 0498        }
 499
 500        /// <summary>
 501        /// Asynchronously commits the JSON text written so far which makes it visible to the output destination.
 502        /// </summary>
 503        /// <remarks>
 504        /// In the case of IBufferWriter, this advances the underlying <see cref="IBufferWriter{Byte}" /> based on what 
 505        /// In the case of Stream, this writes the data to the stream and flushes it asynchronously, while monitoring ca
 506        /// </remarks>
 507        /// <exception cref="ObjectDisposedException">
 508        ///   The instance of <see cref="Utf8JsonWriter"/> has been disposed.
 509        /// </exception>
 510        public async Task FlushAsync(CancellationToken cancellationToken = default)
 0511        {
 0512            CheckNotDisposed();
 513
 0514            _memory = default;
 515
 0516            if (_stream != null)
 0517            {
 0518                Debug.Assert(_arrayBufferWriter != null);
 0519                if (BytesPending != 0)
 0520                {
 0521                    _arrayBufferWriter.Advance(BytesPending);
 0522                    BytesPending = 0;
 523
 0524                    await _stream.WriteAsync(_arrayBufferWriter.WrittenMemory, cancellationToken).ConfigureAwait(false);
 525
 0526                    BytesCommitted += _arrayBufferWriter.WrittenCount;
 0527                    _arrayBufferWriter.Clear();
 0528                }
 0529                await _stream.FlushAsync(cancellationToken).ConfigureAwait(false);
 0530            }
 531            else
 0532            {
 0533                Debug.Assert(_output != null);
 0534                if (BytesPending != 0)
 0535                {
 0536                    _output.Advance(BytesPending);
 0537                    BytesCommitted += BytesPending;
 0538                    BytesPending = 0;
 0539                }
 0540            }
 0541        }
 542
 543        /// <summary>
 544        /// Writes the beginning of a JSON array.
 545        /// </summary>
 546        /// <exception cref="InvalidOperationException">
 547        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 548        /// OR if this would result in invalid JSON being written (while validation is enabled).
 549        /// </exception>
 550        public void WriteStartArray()
 0551        {
 0552            WriteStart(JsonConstants.OpenBracket);
 0553            _tokenType = JsonTokenType.StartArray;
 0554        }
 555
 556        /// <summary>
 557        /// Writes the beginning of a JSON object.
 558        /// </summary>
 559        /// <exception cref="InvalidOperationException">
 560        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 561        /// OR if this would result in invalid JSON being written (while validation is enabled).
 562        /// </exception>
 563        public void WriteStartObject()
 0564        {
 0565            WriteStart(JsonConstants.OpenBrace);
 0566            _tokenType = JsonTokenType.StartObject;
 0567        }
 568
 569        private void WriteStart(byte token)
 0570        {
 0571            if (CurrentDepth >= _options.MaxDepth)
 0572            {
 0573                ThrowInvalidOperationException_DepthTooLarge();
 574            }
 575
 0576            if (_options.IndentedOrNotSkipValidation)
 0577            {
 0578                WriteStartSlow(token);
 0579            }
 580            else
 0581            {
 0582                WriteStartMinimized(token);
 0583            }
 584
 0585            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0586            _currentDepth++;
 0587        }
 588
 589        private void WriteStartMinimized(byte token)
 0590        {
 0591            if (_memory.Length - BytesPending < 2)  // 1 start token, and optionally, 1 list separator
 0592            {
 0593                Grow(2);
 0594            }
 595
 0596            Span<byte> output = _memory.Span;
 0597            if (_currentDepth < 0)
 0598            {
 0599                output[BytesPending++] = JsonConstants.ListSeparator;
 0600            }
 0601            output[BytesPending++] = token;
 0602        }
 603
 604        private void WriteStartSlow(byte token)
 0605        {
 0606            Debug.Assert(_options.Indented || !_options.SkipValidation);
 607
 0608            if (_options.Indented)
 0609            {
 0610                if (!_options.SkipValidation)
 0611                {
 0612                    ValidateStart();
 0613                    UpdateBitStackOnStart(token);
 0614                }
 0615                WriteStartIndented(token);
 0616            }
 617            else
 0618            {
 0619                Debug.Assert(!_options.SkipValidation);
 0620                ValidateStart();
 0621                UpdateBitStackOnStart(token);
 0622                WriteStartMinimized(token);
 0623            }
 0624        }
 625
 626        private void ValidateStart()
 0627        {
 628            // Note that Start[Array|Object] indicates the start of a value, so the same check can be used.
 0629            if (!CanWriteValue)
 0630            {
 0631                OnValidateStartFailed();
 632            }
 0633        }
 634
 635        [DoesNotReturn]
 636        [MethodImpl(MethodImplOptions.NoInlining)]
 637        private void OnValidateStartFailed()
 0638        {
 639            // Make sure a new object or array is not attempted within an unfinalized string.
 0640            if (IsWritingPartialString)
 0641            {
 0642                ThrowInvalidOperationException(ExceptionResource.CannotWriteWithinString);
 643            }
 644
 0645            Debug.Assert(!HasPartialStringData);
 646
 0647            if (_enclosingContainer == EnclosingContainerType.Object)
 0648            {
 0649                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 0650                Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
 0651                ThrowInvalidOperationException(ExceptionResource.CannotStartObjectArrayWithoutProperty);
 652            }
 653            else
 0654            {
 0655                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 0656                Debug.Assert(_tokenType != JsonTokenType.StartObject);
 0657                Debug.Assert(CurrentDepth == 0 && _tokenType != JsonTokenType.None);
 0658                ThrowInvalidOperationException(ExceptionResource.CannotStartObjectArrayAfterPrimitiveOrClose);
 659            }
 660        }
 661
 662        private void WriteStartIndented(byte token)
 0663        {
 0664            int indent = Indentation;
 0665            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 666
 0667            int minRequired = indent + 1;   // 1 start token
 0668            int maxRequired = minRequired + 3; // Optionally, 1 list separator and 1-2 bytes for new line
 669
 0670            if (_memory.Length - BytesPending < maxRequired)
 0671            {
 0672                Grow(maxRequired);
 0673            }
 674
 0675            Span<byte> output = _memory.Span;
 676
 0677            if (_currentDepth < 0)
 0678            {
 0679                output[BytesPending++] = JsonConstants.ListSeparator;
 0680            }
 681
 0682            if (_tokenType is not JsonTokenType.PropertyName and not JsonTokenType.None || _commentAfterNoneOrPropertyNa
 0683            {
 0684                WriteNewLine(output);
 0685                WriteIndentation(output.Slice(BytesPending), indent);
 0686                BytesPending += indent;
 0687            }
 688
 0689            output[BytesPending++] = token;
 0690        }
 691
 692        /// <summary>
 693        /// Writes the beginning of a JSON array with a pre-encoded property name as the key.
 694        /// </summary>
 695        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 696        /// <exception cref="InvalidOperationException">
 697        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 698        /// OR if this would result in invalid JSON being written (while validation is enabled).
 699        /// </exception>
 700        public void WriteStartArray(JsonEncodedText propertyName)
 0701        {
 0702            WriteStartHelper(propertyName.EncodedUtf8Bytes, JsonConstants.OpenBracket);
 0703            _tokenType = JsonTokenType.StartArray;
 0704        }
 705
 706        /// <summary>
 707        /// Writes the beginning of a JSON object with a pre-encoded property name as the key.
 708        /// </summary>
 709        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 710        /// <exception cref="InvalidOperationException">
 711        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 712        /// OR if this would result in invalid JSON being written (while validation is enabled).
 713        /// </exception>
 714        public void WriteStartObject(JsonEncodedText propertyName)
 0715        {
 0716            WriteStartHelper(propertyName.EncodedUtf8Bytes, JsonConstants.OpenBrace);
 0717            _tokenType = JsonTokenType.StartObject;
 0718        }
 719
 720        private void WriteStartHelper(ReadOnlySpan<byte> utf8PropertyName, byte token)
 0721        {
 0722            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 723
 0724            ValidateDepth();
 725
 0726            WriteStartByOptions(utf8PropertyName, token);
 727
 0728            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0729            _currentDepth++;
 0730        }
 731
 732        /// <summary>
 733        /// Writes the beginning of a JSON array with a property name as the key.
 734        /// </summary>
 735        /// <param name="utf8PropertyName">The UTF-8 encoded property name of the JSON array to be written.</param>
 736        /// <remarks>
 737        /// The property name is escaped before writing.
 738        /// </remarks>
 739        /// <exception cref="ArgumentException">
 740        /// Thrown when the specified property name is too large.
 741        /// </exception>
 742        /// <exception cref="InvalidOperationException">
 743        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 744        /// OR if this would result in invalid JSON being written (while validation is enabled).
 745        /// </exception>
 746        public void WriteStartArray(ReadOnlySpan<byte> utf8PropertyName)
 0747        {
 0748            ValidatePropertyNameAndDepth(utf8PropertyName);
 749
 0750            WriteStartEscape(utf8PropertyName, JsonConstants.OpenBracket);
 751
 0752            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0753            _currentDepth++;
 0754            _tokenType = JsonTokenType.StartArray;
 0755        }
 756
 757        /// <summary>
 758        /// Writes the beginning of a JSON object with a property name as the key.
 759        /// </summary>
 760        /// <param name="utf8PropertyName">The UTF-8 encoded property name of the JSON object to be written.</param>
 761        /// <remarks>
 762        /// The property name is escaped before writing.
 763        /// </remarks>
 764        /// <exception cref="ArgumentException">
 765        /// Thrown when the specified property name is too large.
 766        /// </exception>
 767        /// <exception cref="InvalidOperationException">
 768        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 769        /// OR if this would result in invalid JSON being written (while validation is enabled).
 770        /// </exception>
 771        public void WriteStartObject(ReadOnlySpan<byte> utf8PropertyName)
 0772        {
 0773            ValidatePropertyNameAndDepth(utf8PropertyName);
 774
 0775            WriteStartEscape(utf8PropertyName, JsonConstants.OpenBrace);
 776
 0777            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0778            _currentDepth++;
 0779            _tokenType = JsonTokenType.StartObject;
 0780        }
 781
 782        private void WriteStartEscape(ReadOnlySpan<byte> utf8PropertyName, byte token)
 0783        {
 0784            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 785
 0786            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 787
 0788            if (propertyIdx != -1)
 0789            {
 0790                WriteStartEscapeProperty(utf8PropertyName, token, propertyIdx);
 0791            }
 792            else
 0793            {
 0794                WriteStartByOptions(utf8PropertyName, token);
 0795            }
 0796        }
 797
 798        private void WriteStartByOptions(ReadOnlySpan<byte> utf8PropertyName, byte token)
 0799        {
 0800            ValidateWritingProperty(token);
 801
 0802            if (_options.Indented)
 0803            {
 0804                WritePropertyNameIndented(utf8PropertyName, token);
 0805            }
 806            else
 0807            {
 0808                WritePropertyNameMinimized(utf8PropertyName, token);
 0809            }
 0810        }
 811
 812        private void WriteStartEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, byte token, int firstEscapeIndexProp)
 0813        {
 0814            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0815            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 816
 0817            byte[]? propertyArray = null;
 818
 0819            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 820
 0821            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0822                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0823                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 824
 0825            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 826
 0827            WriteStartByOptions(escapedPropertyName.Slice(0, written), token);
 828
 0829            if (propertyArray != null)
 0830            {
 0831                ArrayPool<byte>.Shared.Return(propertyArray);
 0832            }
 0833        }
 834
 835        /// <summary>
 836        /// Writes the beginning of a JSON array with a property name as the key.
 837        /// </summary>
 838        /// <param name="propertyName">The name of the property to write.</param>
 839        /// <remarks>
 840        /// The property name is escaped before writing.
 841        /// </remarks>
 842        /// <exception cref="ArgumentException">
 843        /// Thrown when the specified property name is too large.
 844        /// </exception>
 845        /// <exception cref="ArgumentNullException">
 846        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 847        /// </exception>
 848        /// <exception cref="InvalidOperationException">
 849        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 850        /// OR if this would result in invalid JSON being written (while validation is enabled).
 851        /// </exception>
 852        public void WriteStartArray(string propertyName)
 0853        {
 0854            ArgumentNullException.ThrowIfNull(propertyName);
 0855            WriteStartArray(propertyName.AsSpan());
 0856        }
 857
 858        /// <summary>
 859        /// Writes the beginning of a JSON object with a property name as the key.
 860        /// </summary>
 861        /// <param name="propertyName">The name of the property to write.</param>
 862        /// <remarks>
 863        /// The property name is escaped before writing.
 864        /// </remarks>
 865        /// <exception cref="ArgumentException">
 866        /// Thrown when the specified property name is too large.
 867        /// </exception>
 868        /// <exception cref="ArgumentNullException">
 869        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 870        /// </exception>
 871        /// <exception cref="InvalidOperationException">
 872        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 873        /// OR if this would result in invalid JSON being written (while validation is enabled).
 874        /// </exception>
 875        public void WriteStartObject(string propertyName)
 0876        {
 0877            ArgumentNullException.ThrowIfNull(propertyName);
 0878            WriteStartObject(propertyName.AsSpan());
 0879        }
 880
 881        /// <summary>
 882        /// Writes the beginning of a JSON array with a property name as the key.
 883        /// </summary>
 884        /// <param name="propertyName">The name of the property to write.</param>
 885        /// <remarks>
 886        /// The property name is escaped before writing.
 887        /// </remarks>
 888        /// <exception cref="ArgumentException">
 889        /// Thrown when the specified property name is too large.
 890        /// </exception>
 891        /// <exception cref="InvalidOperationException">
 892        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 893        /// OR if this would result in invalid JSON being written (while validation is enabled).
 894        /// </exception>
 895        public void WriteStartArray(ReadOnlySpan<char> propertyName)
 0896        {
 0897            ValidatePropertyNameAndDepth(propertyName);
 898
 0899            WriteStartEscape(propertyName, JsonConstants.OpenBracket);
 900
 0901            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0902            _currentDepth++;
 0903            _tokenType = JsonTokenType.StartArray;
 0904        }
 905
 906        /// <summary>
 907        /// Writes the beginning of a JSON object with a property name as the key.
 908        /// </summary>
 909        /// <param name="propertyName">The name of the property to write.</param>
 910        /// <remarks>
 911        /// The property name is escaped before writing.
 912        /// </remarks>
 913        /// <exception cref="ArgumentException">
 914        /// Thrown when the specified property name is too large.
 915        /// </exception>
 916        /// <exception cref="InvalidOperationException">
 917        /// Thrown when the depth of the JSON has exceeded the maximum depth of 1000
 918        /// OR if this would result in invalid JSON being written (while validation is enabled).
 919        /// </exception>
 920        public void WriteStartObject(ReadOnlySpan<char> propertyName)
 0921        {
 0922            ValidatePropertyNameAndDepth(propertyName);
 923
 0924            WriteStartEscape(propertyName, JsonConstants.OpenBrace);
 925
 0926            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0927            _currentDepth++;
 0928            _tokenType = JsonTokenType.StartObject;
 0929        }
 930
 931        private void WriteStartEscape(ReadOnlySpan<char> propertyName, byte token)
 0932        {
 0933            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 934
 0935            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 936
 0937            if (propertyIdx != -1)
 0938            {
 0939                WriteStartEscapeProperty(propertyName, token, propertyIdx);
 0940            }
 941            else
 0942            {
 0943                WriteStartByOptions(propertyName, token);
 0944            }
 0945        }
 946
 947        private void WriteStartByOptions(ReadOnlySpan<char> propertyName, byte token)
 0948        {
 0949            ValidateWritingProperty(token);
 950
 0951            if (_options.Indented)
 0952            {
 0953                WritePropertyNameIndented(propertyName, token);
 0954            }
 955            else
 0956            {
 0957                WritePropertyNameMinimized(propertyName, token);
 0958            }
 0959        }
 960
 961        private void WriteStartEscapeProperty(ReadOnlySpan<char> propertyName, byte token, int firstEscapeIndexProp)
 0962        {
 0963            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0964            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 965
 0966            char[]? propertyArray = null;
 967
 0968            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 969
 0970            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0971                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0972                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 973
 0974            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 975
 0976            WriteStartByOptions(escapedPropertyName.Slice(0, written), token);
 977
 0978            if (propertyArray != null)
 0979            {
 0980                ArrayPool<char>.Shared.Return(propertyArray);
 0981            }
 0982        }
 983
 984        /// <summary>
 985        /// Writes the end of a JSON array.
 986        /// </summary>
 987        /// <exception cref="InvalidOperationException">
 988        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 989        /// </exception>
 990        public void WriteEndArray()
 0991        {
 0992            WriteEnd(JsonConstants.CloseBracket);
 0993            _tokenType = JsonTokenType.EndArray;
 0994        }
 995
 996        /// <summary>
 997        /// Writes the end of a JSON object.
 998        /// </summary>
 999        /// <exception cref="InvalidOperationException">
 1000        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 1001        /// </exception>
 1002        public void WriteEndObject()
 01003        {
 01004            WriteEnd(JsonConstants.CloseBrace);
 01005            _tokenType = JsonTokenType.EndObject;
 01006        }
 1007
 1008        private void WriteEnd(byte token)
 01009        {
 01010            if (_options.IndentedOrNotSkipValidation)
 01011            {
 01012                WriteEndSlow(token);
 01013            }
 1014            else
 01015            {
 01016                WriteEndMinimized(token);
 01017            }
 1018
 01019            SetFlagToAddListSeparatorBeforeNextItem();
 1020            // Necessary if WriteEndX is called without a corresponding WriteStartX first.
 01021            if (CurrentDepth != 0)
 01022            {
 01023                _currentDepth--;
 01024            }
 01025        }
 1026
 1027        private void WriteEndMinimized(byte token)
 01028        {
 01029            if (_memory.Length - BytesPending < 1) // 1 end token
 01030            {
 01031                Grow(1);
 01032            }
 1033
 01034            Span<byte> output = _memory.Span;
 01035            output[BytesPending++] = token;
 01036        }
 1037
 1038        private void WriteEndSlow(byte token)
 01039        {
 01040            Debug.Assert(_options.Indented || !_options.SkipValidation);
 1041
 01042            if (_options.Indented)
 01043            {
 01044                if (!_options.SkipValidation)
 01045                {
 01046                    ValidateEnd(token);
 01047                }
 01048                WriteEndIndented(token);
 01049            }
 1050            else
 01051            {
 01052                Debug.Assert(!_options.SkipValidation);
 01053                ValidateEnd(token);
 01054                WriteEndMinimized(token);
 01055            }
 01056        }
 1057
 1058        // Performance degrades significantly in some scenarios when inlining is allowed.
 1059        [MethodImpl(MethodImplOptions.NoInlining)]
 1060        private void ValidateEnd(byte token)
 01061        {
 01062            if (_tokenType == JsonTokenType.PropertyName)
 01063                ThrowInvalidOperationException_MismatchedObjectArray(token);
 1064
 01065            if (token == JsonConstants.CloseBracket)
 01066            {
 01067                if (_enclosingContainer != EnclosingContainerType.Array)
 01068                {
 01069                    ThrowInvalidOperationException_MismatchedObjectArray(token);
 1070                }
 01071            }
 1072            else
 01073            {
 01074                Debug.Assert(token == JsonConstants.CloseBrace);
 1075
 01076                if (_enclosingContainer != EnclosingContainerType.Object)
 01077                {
 01078                    ThrowInvalidOperationException_MismatchedObjectArray(token);
 1079                }
 01080            }
 1081
 01082            EnclosingContainerType container = _bitStack.Pop() ? EnclosingContainerType.Object : EnclosingContainerType.
 01083            _enclosingContainer = _bitStack.CurrentDepth == 0 ? EnclosingContainerType.None : container;
 01084        }
 1085
 1086        private void WriteEndIndented(byte token)
 01087        {
 1088            // Do not format/indent empty JSON object/array.
 01089            if (_tokenType == JsonTokenType.StartObject || _tokenType == JsonTokenType.StartArray)
 01090            {
 01091                WriteEndMinimized(token);
 01092            }
 1093            else
 01094            {
 01095                int indent = Indentation;
 1096
 1097                // Necessary if WriteEndX is called without a corresponding WriteStartX first.
 01098                if (indent != 0)
 01099                {
 1100                    // The end token should be at an outer indent and since we haven't updated
 1101                    // current depth yet, explicitly subtract here.
 01102                    indent -= _indentLength;
 01103                }
 1104
 01105                Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 01106                Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.None);
 1107
 01108                int maxRequired = indent + 3; // 1 end token, 1-2 bytes for new line
 1109
 01110                if (_memory.Length - BytesPending < maxRequired)
 01111                {
 01112                    Grow(maxRequired);
 01113                }
 1114
 01115                Span<byte> output = _memory.Span;
 1116
 01117                WriteNewLine(output);
 1118
 01119                WriteIndentation(output.Slice(BytesPending), indent);
 01120                BytesPending += indent;
 1121
 01122                output[BytesPending++] = token;
 01123            }
 01124        }
 1125
 1126        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1127        private void WriteNewLine(Span<byte> output)
 01128        {
 1129            // Write '\r\n' OR '\n', depending on the configured new line string
 01130            Debug.Assert(_newLineLength is 1 or 2, "Invalid new line length.");
 01131            if (_newLineLength == 2)
 01132            {
 01133                output[BytesPending++] = JsonConstants.CarriageReturn;
 01134            }
 01135            output[BytesPending++] = JsonConstants.LineFeed;
 01136        }
 1137
 1138        private void WriteIndentation(Span<byte> buffer, int indent)
 01139        {
 01140            JsonWriterHelper.WriteIndentation(buffer, indent, _indentByte);
 01141        }
 1142
 1143        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 1144        private void UpdateBitStackOnStart(byte token)
 01145        {
 01146            if (token == JsonConstants.OpenBracket)
 01147            {
 01148                _bitStack.PushFalse();
 01149                _enclosingContainer = EnclosingContainerType.Array;
 01150            }
 1151            else
 01152            {
 01153                Debug.Assert(token == JsonConstants.OpenBrace);
 01154                _bitStack.PushTrue();
 01155                _enclosingContainer = EnclosingContainerType.Object;
 01156            }
 01157        }
 1158
 1159        private void Grow(int requiredSize)
 281160        {
 281161            Debug.Assert(requiredSize > 0);
 1162
 281163            if (_memory.Length == 0)
 281164            {
 281165                FirstCallToGetMemory(requiredSize);
 281166                return;
 1167            }
 1168
 01169            int sizeHint = Math.Max(DefaultGrowthSize, requiredSize);
 1170
 01171            Debug.Assert(BytesPending != 0);
 1172
 01173            if (_stream != null)
 01174            {
 01175                Debug.Assert(_arrayBufferWriter != null);
 1176
 01177                int needed = BytesPending + sizeHint;
 01178                JsonHelpers.ValidateInt32MaxArrayLength((uint)needed);
 1179
 01180                _memory = _arrayBufferWriter.GetMemory(needed);
 1181
 01182                Debug.Assert(_memory.Length >= sizeHint);
 01183            }
 1184            else
 01185            {
 01186                Debug.Assert(_output != null);
 1187
 01188                _output.Advance(BytesPending);
 01189                BytesCommitted += BytesPending;
 01190                BytesPending = 0;
 1191
 01192                _memory = _output.GetMemory(sizeHint);
 1193
 01194                if (_memory.Length < sizeHint)
 01195                {
 01196                    ThrowHelper.ThrowInvalidOperationException_NeedLargerSpan();
 1197                }
 01198            }
 281199        }
 1200
 1201        private void FirstCallToGetMemory(int requiredSize)
 281202        {
 281203            Debug.Assert(_memory.Length == 0);
 281204            Debug.Assert(BytesPending == 0);
 1205
 281206            int sizeHint = Math.Max(InitialGrowthSize, requiredSize);
 1207
 281208            if (_stream != null)
 01209            {
 01210                Debug.Assert(_arrayBufferWriter != null);
 01211                _memory = _arrayBufferWriter.GetMemory(sizeHint);
 01212                Debug.Assert(_memory.Length >= sizeHint);
 01213            }
 1214            else
 281215            {
 281216                Debug.Assert(_output != null);
 281217                _memory = _output.GetMemory(sizeHint);
 1218
 281219                if (_memory.Length < sizeHint)
 01220                {
 01221                    ThrowHelper.ThrowInvalidOperationException_NeedLargerSpan();
 1222                }
 281223            }
 281224        }
 1225
 1226        private void SetFlagToAddListSeparatorBeforeNextItem()
 281227        {
 281228            _currentDepth |= 1 << 31;
 281229        }
 1230
 1231        [MethodImpl(MethodImplOptions.NoInlining)]
 1232        [DoesNotReturn]
 1233        private void ThrowInvalidOperationException(ExceptionResource resource)
 01234            => ThrowHelper.ThrowInvalidOperationException(resource, currentDepth: default, maxDepth: _options.MaxDepth, 
 1235
 1236        [MethodImpl(MethodImplOptions.NoInlining)]
 1237        [DoesNotReturn]
 1238        private void ThrowInvalidOperationException_MismatchedObjectArray(byte token)
 01239            => ThrowHelper.ThrowInvalidOperationException(ExceptionResource.MismatchedObjectArray, currentDepth: default
 1240
 1241        [MethodImpl(MethodImplOptions.NoInlining)]
 1242        [DoesNotReturn]
 1243        private void ThrowInvalidOperationException_DepthTooLarge()
 01244            => ThrowHelper.ThrowInvalidOperationException(ExceptionResource.DepthTooLarge, _currentDepth, _options.MaxDe
 1245
 1246        [DebuggerBrowsable(DebuggerBrowsableState.Never)]
 01247        private string DebuggerDisplay => $"BytesCommitted = {BytesCommitted} BytesPending = {BytesPending} CurrentDepth
 1248
 1249        /// <summary>
 1250        /// Indicates whether the writer is currently writing a partial string value.
 1251        /// </summary>
 01252        private bool IsWritingPartialString => _enclosingContainer >= EnclosingContainerType.Utf8StringSequence;
 1253
 1254        /// <summary>
 1255        /// The type of container that is enclosing the current position. The underlying values have been chosen
 1256        /// to allow <see cref="CanWriteValue"/> to be done using bitwise operations and must be kept in sync with <see 
 1257        /// </summary>
 1258        internal enum EnclosingContainerType : byte
 1259        {
 1260            /// <summary>
 1261            /// Root level. The choice of <see cref="JsonTokenType.None"/> allows fast validation by equality comparison
 1262            /// since a value can be written at the root level only if there was no previous token.
 1263            /// </summary>
 1264            None = JsonTokenType.None,
 1265
 1266            /// <summary>
 1267            /// JSON object. The choice of <see cref="JsonTokenType.PropertyName"/> allows fast validation by equality c
 1268            /// since a value can be written inside a JSON object only if the previous token is a property name.
 1269            /// </summary>
 1270            Object = JsonTokenType.PropertyName,
 1271
 1272            /// <summary>
 1273            /// JSON array. Chosen so that its lower nibble is 0 to ensure it does not conflict with <see cref="JsonToke
 1274            /// </summary>
 1275            Array = 0x10,
 1276
 1277            /// <summary>
 1278            /// Partial UTF-8 string. This is a container if viewed as an array of "utf-8 string segment"-typed values. 
 1279            /// so <see cref="_bitStack"/> does not need to store its state.
 1280            /// <see cref="IsWritingPartialString"/> relies on the value of the partial string members being the largest
 1281            /// </summary>
 1282            Utf8StringSequence = 0x20,
 1283
 1284            /// <summary>
 1285            /// Partial UTF-16 string. This is a container if viewed as an array of "utf-16 string segment"-typed values
 1286            /// so <see cref="_bitStack"/> does not need to store its state.
 1287            /// <see cref="IsWritingPartialString"/> relies on the value of the partial string members being the largest
 1288            /// </summary>
 1289            Utf16StringSequence = 0x30,
 1290
 1291            /// <summary>
 1292            /// Partial Base64 string. This is a container if viewed as an array of "base64 string segment"-typed values
 1293            /// so <see cref="_bitStack"/> does not need to store its state.
 1294            /// <see cref="IsWritingPartialString"/> relies on the value of the partial string members being the largest
 1295            /// </summary>
 1296            Base64StringSequence = 0x40,
 1297        }
 1298    }
 1299}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Bytes.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and raw bytes value (as a Base64 encoded JSON string) as part of a name
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="bytes">The binary data to write as Base64 encoded text.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        public void WriteBase64String(JsonEncodedText propertyName, ReadOnlySpan<byte> bytes)
 021        {
 022            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 023            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 24
 025            WriteBase64ByOptions(utf8PropertyName, bytes);
 26
 027            SetFlagToAddListSeparatorBeforeNextItem();
 028            _tokenType = JsonTokenType.String;
 029        }
 30
 31        /// <summary>
 32        /// Writes the property name and raw bytes value (as a Base64 encoded JSON string) as part of a name/value pair 
 33        /// </summary>
 34        /// <param name="propertyName">The name of the property to write.</param>
 35        /// <param name="bytes">The binary data to write as Base64 encoded text.</param>
 36        /// <exception cref="ArgumentException">
 37        /// Thrown when the specified property name is too large.
 38        /// </exception>
 39        /// <exception cref="ArgumentNullException">
 40        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 41        /// </exception>
 42        /// <exception cref="InvalidOperationException">
 43        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 44        /// </exception>
 45        /// <remarks>
 46        /// The property name is escaped before writing.
 47        /// </remarks>
 48        public void WriteBase64String(string propertyName, ReadOnlySpan<byte> bytes)
 049        {
 050            ArgumentNullException.ThrowIfNull(propertyName);
 051            WriteBase64String(propertyName.AsSpan(), bytes);
 052        }
 53
 54        /// <summary>
 55        /// Writes the property name and raw bytes value (as a Base64 encoded JSON string) as part of a name/value pair 
 56        /// </summary>
 57        /// <param name="propertyName">The name of the property to write.</param>
 58        /// <param name="bytes">The binary data to write as Base64 encoded text.</param>
 59        /// <exception cref="ArgumentException">
 60        /// Thrown when the specified property name is too large.
 61        /// </exception>
 62        /// <exception cref="InvalidOperationException">
 63        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 64        /// </exception>
 65        /// <remarks>
 66        /// The property name is escaped before writing.
 67        /// </remarks>
 68        public void WriteBase64String(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes)
 069        {
 070            JsonWriterHelper.ValidatePropertyNameLength(propertyName);
 71
 072            WriteBase64Escape(propertyName, bytes);
 73
 074            SetFlagToAddListSeparatorBeforeNextItem();
 075            _tokenType = JsonTokenType.String;
 076        }
 77
 78        /// <summary>
 79        /// Writes the property name and raw bytes value (as a Base64 encoded JSON string) as part of a name/value pair 
 80        /// </summary>
 81        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 82        /// <param name="bytes">The binary data to write as Base64 encoded text.</param>
 83        /// <exception cref="ArgumentException">
 84        /// Thrown when the specified property name is too large.
 85        /// </exception>
 86        /// <exception cref="InvalidOperationException">
 87        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 88        /// </exception>
 89        /// <remarks>
 90        /// The property name is escaped before writing.
 91        /// </remarks>
 92        public void WriteBase64String(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes)
 093        {
 094            JsonWriterHelper.ValidatePropertyNameLength(utf8PropertyName);
 95
 096            WriteBase64Escape(utf8PropertyName, bytes);
 97
 098            SetFlagToAddListSeparatorBeforeNextItem();
 099            _tokenType = JsonTokenType.String;
 0100        }
 101
 102        private void WriteBase64Escape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes)
 0103        {
 0104            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 105
 0106            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 107
 0108            if (propertyIdx != -1)
 0109            {
 0110                WriteBase64EscapeProperty(propertyName, bytes, propertyIdx);
 0111            }
 112            else
 0113            {
 0114                WriteBase64ByOptions(propertyName, bytes);
 0115            }
 0116        }
 117
 118        private void WriteBase64Escape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes)
 0119        {
 0120            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 121
 0122            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 123
 0124            if (propertyIdx != -1)
 0125            {
 0126                WriteBase64EscapeProperty(utf8PropertyName, bytes, propertyIdx);
 0127            }
 128            else
 0129            {
 0130                WriteBase64ByOptions(utf8PropertyName, bytes);
 0131            }
 0132        }
 133
 134        private void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes, int firstEscap
 0135        {
 0136            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0137            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 138
 0139            char[]? propertyArray = null;
 140
 0141            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 142
 0143            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0144                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0145                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 146
 0147            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 148
 0149            WriteBase64ByOptions(escapedPropertyName.Slice(0, written), bytes);
 150
 0151            if (propertyArray != null)
 0152            {
 0153                ArrayPool<char>.Shared.Return(propertyArray);
 0154            }
 0155        }
 156
 157        private void WriteBase64EscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes, int firstE
 0158        {
 0159            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0160            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 161
 0162            byte[]? propertyArray = null;
 163
 0164            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 165
 0166            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0167                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0168                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 169
 0170            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 171
 0172            WriteBase64ByOptions(escapedPropertyName.Slice(0, written), bytes);
 173
 0174            if (propertyArray != null)
 0175            {
 0176                ArrayPool<byte>.Shared.Return(propertyArray);
 0177            }
 0178        }
 179
 180        private void WriteBase64ByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes)
 0181        {
 0182            ValidateWritingProperty();
 0183            if (_options.Indented)
 0184            {
 0185                WriteBase64Indented(propertyName, bytes);
 0186            }
 187            else
 0188            {
 0189                WriteBase64Minimized(propertyName, bytes);
 0190            }
 0191        }
 192
 193        private void WriteBase64ByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes)
 0194        {
 0195            ValidateWritingProperty();
 0196            if (_options.Indented)
 0197            {
 0198                WriteBase64Indented(utf8PropertyName, bytes);
 0199            }
 200            else
 0201            {
 0202                WriteBase64Minimized(utf8PropertyName, bytes);
 0203            }
 0204        }
 205
 206        private void WriteBase64Minimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> bytes)
 0207        {
 0208            int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 209
 0210            Debug.Assert(escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding < int.MaxValue - 
 211
 212            // All ASCII, 2 quotes for property name, 2 quotes to surround the base-64 encoded string value, and 1 colon
 213            // Optionally, 1 list separator, and up to 3x growth when transcoding.
 0214            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + encodedL
 215
 0216            if (_memory.Length - BytesPending < maxRequired)
 0217            {
 0218                Grow(maxRequired);
 0219            }
 220
 0221            Span<byte> output = _memory.Span;
 222
 0223            if (_currentDepth < 0)
 0224            {
 0225                output[BytesPending++] = JsonConstants.ListSeparator;
 0226            }
 0227            output[BytesPending++] = JsonConstants.Quote;
 228
 0229            TranscodeAndWrite(escapedPropertyName, output);
 230
 0231            output[BytesPending++] = JsonConstants.Quote;
 0232            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 233
 0234            output[BytesPending++] = JsonConstants.Quote;
 235
 0236            Base64EncodeAndWrite(bytes, output);
 237
 0238            output[BytesPending++] = JsonConstants.Quote;
 0239        }
 240
 241        private void WriteBase64Minimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> bytes)
 0242        {
 0243            int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 244
 0245            Debug.Assert(escapedPropertyName.Length < int.MaxValue - encodedLength - 6);
 246
 247            // 2 quotes for property name, 2 quotes to surround the base-64 encoded string value, and 1 colon => escaped
 248            // Optionally, 1 list separator.
 0249            int maxRequired = escapedPropertyName.Length + encodedLength + 6;
 250
 0251            if (_memory.Length - BytesPending < maxRequired)
 0252            {
 0253                Grow(maxRequired);
 0254            }
 255
 0256            Span<byte> output = _memory.Span;
 257
 0258            if (_currentDepth < 0)
 0259            {
 0260                output[BytesPending++] = JsonConstants.ListSeparator;
 0261            }
 0262            output[BytesPending++] = JsonConstants.Quote;
 263
 0264            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0265            BytesPending += escapedPropertyName.Length;
 266
 0267            output[BytesPending++] = JsonConstants.Quote;
 0268            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 269
 0270            output[BytesPending++] = JsonConstants.Quote;
 271
 0272            Base64EncodeAndWrite(bytes, output);
 273
 0274            output[BytesPending++] = JsonConstants.Quote;
 0275        }
 276
 277        private void WriteBase64Indented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> bytes)
 0278        {
 0279            int indent = Indentation;
 0280            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 281
 0282            int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 283
 0284            Debug.Assert(escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding < int.MaxValue - 
 285
 286            // All ASCII, 2 quotes for property name, 2 quotes to surround the base-64 encoded string value, 1 colon, an
 287            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding.
 0288            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 289
 0290            if (_memory.Length - BytesPending < maxRequired)
 0291            {
 0292                Grow(maxRequired);
 0293            }
 294
 0295            Span<byte> output = _memory.Span;
 296
 0297            if (_currentDepth < 0)
 0298            {
 0299                output[BytesPending++] = JsonConstants.ListSeparator;
 0300            }
 301
 0302            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 303
 0304            if (_tokenType != JsonTokenType.None)
 0305            {
 0306                WriteNewLine(output);
 0307            }
 308
 0309            WriteIndentation(output.Slice(BytesPending), indent);
 0310            BytesPending += indent;
 311
 0312            output[BytesPending++] = JsonConstants.Quote;
 313
 0314            TranscodeAndWrite(escapedPropertyName, output);
 315
 0316            output[BytesPending++] = JsonConstants.Quote;
 0317            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0318            output[BytesPending++] = JsonConstants.Space;
 319
 0320            output[BytesPending++] = JsonConstants.Quote;
 321
 0322            Base64EncodeAndWrite(bytes, output);
 323
 0324            output[BytesPending++] = JsonConstants.Quote;
 0325        }
 326
 327        private void WriteBase64Indented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> bytes)
 0328        {
 0329            int indent = Indentation;
 0330            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 331
 0332            int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 333
 0334            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - encodedLength - 7 - _newLineLength);
 335
 336            // 2 quotes for property name, 2 quotes to surround the base-64 encoded string value, 1 colon, and 1 space =
 337            // Optionally, 1 list separator, and 1-2 bytes for new line.
 0338            int maxRequired = indent + escapedPropertyName.Length + encodedLength + 7 + _newLineLength;
 339
 0340            if (_memory.Length - BytesPending < maxRequired)
 0341            {
 0342                Grow(maxRequired);
 0343            }
 344
 0345            Span<byte> output = _memory.Span;
 346
 0347            if (_currentDepth < 0)
 0348            {
 0349                output[BytesPending++] = JsonConstants.ListSeparator;
 0350            }
 351
 0352            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 353
 0354            if (_tokenType != JsonTokenType.None)
 0355            {
 0356                WriteNewLine(output);
 0357            }
 358
 0359            WriteIndentation(output.Slice(BytesPending), indent);
 0360            BytesPending += indent;
 361
 0362            output[BytesPending++] = JsonConstants.Quote;
 363
 0364            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0365            BytesPending += escapedPropertyName.Length;
 366
 0367            output[BytesPending++] = JsonConstants.Quote;
 0368            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0369            output[BytesPending++] = JsonConstants.Space;
 370
 0371            output[BytesPending++] = JsonConstants.Quote;
 372
 0373            Base64EncodeAndWrite(bytes, output);
 374
 0375            output[BytesPending++] = JsonConstants.Quote;
 0376        }
 377    }
 378}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTime.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="DateTime"/> value (as a JSON string) as part of a name/v
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="DateTime"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for example
 22        /// The property name should already be escaped when the instance of <see cref="JsonEncodedText"/> was created.
 23        /// </remarks>
 24        public void WriteString(JsonEncodedText propertyName, DateTime value)
 025        {
 026            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 027            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 28
 029            WriteStringByOptions(utf8PropertyName, value);
 30
 031            SetFlagToAddListSeparatorBeforeNextItem();
 032            _tokenType = JsonTokenType.String;
 033        }
 34
 35        /// <summary>
 36        /// Writes the property name and <see cref="DateTime"/> value (as a JSON string) as part of a name/value pair of
 37        /// </summary>
 38        /// <param name="propertyName">The name of the property to write.</param>
 39        /// <param name="value">The value to write.</param>
 40        /// <exception cref="ArgumentException">
 41        /// Thrown when the specified property name is too large.
 42        /// </exception>
 43        /// <exception cref="ArgumentNullException">
 44        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 45        /// </exception>
 46        /// <exception cref="InvalidOperationException">
 47        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 48        /// </exception>
 49        /// <remarks>
 50        /// Writes the <see cref="DateTime"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for example
 51        /// The property name is escaped before writing.
 52        /// </remarks>
 53        public void WriteString(string propertyName, DateTime value)
 054        {
 055            ArgumentNullException.ThrowIfNull(propertyName);
 056            WriteString(propertyName.AsSpan(), value);
 057        }
 58
 59        /// <summary>
 60        /// Writes the property name and <see cref="DateTime"/> value (as a JSON string) as part of a name/value pair of
 61        /// </summary>
 62        /// <param name="propertyName">The name of the property to write.</param>
 63        /// <param name="value">The value to write.</param>
 64        /// <exception cref="ArgumentException">
 65        /// Thrown when the specified property name is too large.
 66        /// </exception>
 67        /// <exception cref="InvalidOperationException">
 68        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 69        /// </exception>
 70        /// <remarks>
 71        /// Writes the <see cref="DateTime"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for example
 72        /// The property name is escaped before writing.
 73        /// </remarks>
 74        public void WriteString(ReadOnlySpan<char> propertyName, DateTime value)
 075        {
 076            JsonWriterHelper.ValidateProperty(propertyName);
 77
 078            WriteStringEscape(propertyName, value);
 79
 080            SetFlagToAddListSeparatorBeforeNextItem();
 081            _tokenType = JsonTokenType.String;
 082        }
 83
 84        /// <summary>
 85        /// Writes the property name and <see cref="DateTime"/> value (as a JSON string) as part of a name/value pair of
 86        /// </summary>
 87        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 88        /// <param name="value">The value to write.</param>
 89        /// <exception cref="ArgumentException">
 90        /// Thrown when the specified property name is too large.
 91        /// </exception>
 92        /// <exception cref="InvalidOperationException">
 93        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 94        /// </exception>
 95        /// <remarks>
 96        /// Writes the <see cref="DateTime"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for example
 97        /// The property name is escaped before writing.
 98        /// </remarks>
 99        public void WriteString(ReadOnlySpan<byte> utf8PropertyName, DateTime value)
 0100        {
 0101            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 102
 0103            WriteStringEscape(utf8PropertyName, value);
 104
 0105            SetFlagToAddListSeparatorBeforeNextItem();
 0106            _tokenType = JsonTokenType.String;
 0107        }
 108
 109        private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTime value)
 0110        {
 0111            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 112
 0113            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 114
 0115            if (propertyIdx != -1)
 0116            {
 0117                WriteStringEscapeProperty(propertyName, value, propertyIdx);
 0118            }
 119            else
 0120            {
 0121                WriteStringByOptions(propertyName, value);
 0122            }
 0123        }
 124
 125        private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTime value)
 0126        {
 0127            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 128
 0129            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 130
 0131            if (propertyIdx != -1)
 0132            {
 0133                WriteStringEscapeProperty(utf8PropertyName, value, propertyIdx);
 0134            }
 135            else
 0136            {
 0137                WriteStringByOptions(utf8PropertyName, value);
 0138            }
 0139        }
 140
 141        private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime value, int firstEscapeIndexProp
 0142        {
 0143            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0144            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 145
 0146            char[]? propertyArray = null;
 147
 0148            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 149
 0150            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0151                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0152                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 153
 0154            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 155
 0156            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 157
 0158            if (propertyArray != null)
 0159            {
 0160                ArrayPool<char>.Shared.Return(propertyArray);
 0161            }
 0162        }
 163
 164        private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, DateTime value, int firstEscapeIndex
 0165        {
 0166            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0167            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 168
 0169            byte[]? propertyArray = null;
 170
 0171            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 172
 0173            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0174                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0175                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 176
 0177            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 178
 0179            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 180
 0181            if (propertyArray != null)
 0182            {
 0183                ArrayPool<byte>.Shared.Return(propertyArray);
 0184            }
 0185        }
 186
 187        private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTime value)
 0188        {
 0189            ValidateWritingProperty();
 0190            if (_options.Indented)
 0191            {
 0192                WriteStringIndented(propertyName, value);
 0193            }
 194            else
 0195            {
 0196                WriteStringMinimized(propertyName, value);
 0197            }
 0198        }
 199
 200        private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTime value)
 0201        {
 0202            ValidateWritingProperty();
 0203            if (_options.Indented)
 0204            {
 0205                WriteStringIndented(utf8PropertyName, value);
 0206            }
 207            else
 0208            {
 0209                WriteStringMinimized(utf8PropertyName, value);
 0210            }
 0211        }
 212
 213        private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, DateTime value)
 0214        {
 0215            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 216
 217            // All ASCII, 2 quotes for property name, 2 quotes for date, and 1 colon => escapedPropertyName.Length + Jso
 218            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0219            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 220
 0221            if (_memory.Length - BytesPending < maxRequired)
 0222            {
 0223                Grow(maxRequired);
 0224            }
 225
 0226            Span<byte> output = _memory.Span;
 227
 0228            if (_currentDepth < 0)
 0229            {
 0230                output[BytesPending++] = JsonConstants.ListSeparator;
 0231            }
 0232            output[BytesPending++] = JsonConstants.Quote;
 233
 0234            TranscodeAndWrite(escapedPropertyName, output);
 235
 0236            output[BytesPending++] = JsonConstants.Quote;
 0237            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 238
 0239            output[BytesPending++] = JsonConstants.Quote;
 240
 0241            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0242            BytesPending += bytesWritten;
 243
 0244            output[BytesPending++] = JsonConstants.Quote;
 0245        }
 246
 247        private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, DateTime value)
 0248        {
 0249            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatDateTimeOffsetLength - 6
 250
 0251            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatDateTimeOffsetLength + 5; // 2 quo
 0252            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 253
 0254            if (_memory.Length - BytesPending < maxRequired)
 0255            {
 0256                Grow(maxRequired);
 0257            }
 258
 0259            Span<byte> output = _memory.Span;
 260
 0261            if (_currentDepth < 0)
 0262            {
 0263                output[BytesPending++] = JsonConstants.ListSeparator;
 0264            }
 0265            output[BytesPending++] = JsonConstants.Quote;
 266
 0267            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0268            BytesPending += escapedPropertyName.Length;
 269
 0270            output[BytesPending++] = JsonConstants.Quote;
 0271            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 272
 0273            output[BytesPending++] = JsonConstants.Quote;
 274
 0275            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0276            BytesPending += bytesWritten;
 277
 0278            output[BytesPending++] = JsonConstants.Quote;
 0279        }
 280
 281        private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTime value)
 0282        {
 0283            int indent = Indentation;
 0284            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 285
 0286            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 287
 288            // All ASCII, 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space => escapedPropertyName.Len
 289            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0290            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 291
 0292            if (_memory.Length - BytesPending < maxRequired)
 0293            {
 0294                Grow(maxRequired);
 0295            }
 296
 0297            Span<byte> output = _memory.Span;
 298
 0299            if (_currentDepth < 0)
 0300            {
 0301                output[BytesPending++] = JsonConstants.ListSeparator;
 0302            }
 303
 0304            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 305
 0306            if (_tokenType != JsonTokenType.None)
 0307            {
 0308                WriteNewLine(output);
 0309            }
 310
 0311            WriteIndentation(output.Slice(BytesPending), indent);
 0312            BytesPending += indent;
 313
 0314            output[BytesPending++] = JsonConstants.Quote;
 315
 0316            TranscodeAndWrite(escapedPropertyName, output);
 317
 0318            output[BytesPending++] = JsonConstants.Quote;
 0319            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0320            output[BytesPending++] = JsonConstants.Space;
 321
 0322            output[BytesPending++] = JsonConstants.Quote;
 323
 0324            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0325            BytesPending += bytesWritten;
 326
 0327            output[BytesPending++] = JsonConstants.Quote;
 0328        }
 329
 330        private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTime value)
 0331        {
 0332            int indent = Indentation;
 0333            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 334
 0335            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDateTimeOffsetL
 336
 0337            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatDateTimeOffsetLength + 6;
 0338            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 339
 0340            if (_memory.Length - BytesPending < maxRequired)
 0341            {
 0342                Grow(maxRequired);
 0343            }
 344
 0345            Span<byte> output = _memory.Span;
 346
 0347            if (_currentDepth < 0)
 0348            {
 0349                output[BytesPending++] = JsonConstants.ListSeparator;
 0350            }
 351
 0352            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 353
 0354            if (_tokenType != JsonTokenType.None)
 0355            {
 0356                WriteNewLine(output);
 0357            }
 358
 0359            WriteIndentation(output.Slice(BytesPending), indent);
 0360            BytesPending += indent;
 361
 0362            output[BytesPending++] = JsonConstants.Quote;
 363
 0364            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0365            BytesPending += escapedPropertyName.Length;
 366
 0367            output[BytesPending++] = JsonConstants.Quote;
 0368            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0369            output[BytesPending++] = JsonConstants.Space;
 370
 0371            output[BytesPending++] = JsonConstants.Quote;
 372
 0373            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0374            BytesPending += bytesWritten;
 375
 0376            output[BytesPending++] = JsonConstants.Quote;
 0377        }
 378
 379        internal void WritePropertyName(DateTime value)
 0380        {
 0381            Span<byte> buffer = stackalloc byte[JsonConstants.MaximumFormatDateTimeOffsetLength];
 0382            JsonWriterHelper.WriteDateTimeTrimmed(buffer, value, out int bytesWritten);
 0383            WritePropertyNameUnescaped(buffer.Slice(0, bytesWritten));
 0384        }
 385    }
 386}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.DateTimeOffset.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="DateTimeOffset"/> value (as a JSON string) as part of a 
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="DateTimeOffset"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for e
 22        /// </remarks>
 23        public void WriteString(JsonEncodedText propertyName, DateTimeOffset value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            WriteStringByOptions(utf8PropertyName, value);
 29
 030            SetFlagToAddListSeparatorBeforeNextItem();
 031            _tokenType = JsonTokenType.String;
 032        }
 33
 34        /// <summary>
 35        /// Writes the property name and <see cref="DateTimeOffset"/> value (as a JSON string) as part of a name/value p
 36        /// </summary>
 37        /// <param name="propertyName">The name of the property to write.</param>
 38        /// <param name="value">The value to write.</param>
 39        /// <exception cref="ArgumentException">
 40        /// Thrown when the specified property name is too large.
 41        /// </exception>
 42        /// <exception cref="ArgumentNullException">
 43        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 44        /// </exception>
 45        /// <exception cref="InvalidOperationException">
 46        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 47        /// </exception>
 48        /// <remarks>
 49        /// Writes the <see cref="DateTimeOffset"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for e
 50        /// The property name is escaped before writing.
 51        /// </remarks>
 52        public void WriteString(string propertyName, DateTimeOffset value)
 053        {
 054            ArgumentNullException.ThrowIfNull(propertyName);
 055            WriteString(propertyName.AsSpan(), value);
 056        }
 57
 58        /// <summary>
 59        /// Writes the property name and <see cref="DateTimeOffset"/> value (as a JSON string) as part of a name/value p
 60        /// </summary>
 61        /// <param name="propertyName">The name of the property to write.</param>
 62        /// <param name="value">The value to write.</param>
 63        /// <exception cref="ArgumentException">
 64        /// Thrown when the specified property name is too large.
 65        /// </exception>
 66        /// <exception cref="InvalidOperationException">
 67        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 68        /// </exception>
 69        /// <remarks>
 70        /// Writes the <see cref="DateTimeOffset"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for e
 71        /// The property name is escaped before writing.
 72        /// </remarks>
 73        public void WriteString(ReadOnlySpan<char> propertyName, DateTimeOffset value)
 074        {
 075            JsonWriterHelper.ValidateProperty(propertyName);
 76
 077            WriteStringEscape(propertyName, value);
 78
 079            SetFlagToAddListSeparatorBeforeNextItem();
 080            _tokenType = JsonTokenType.String;
 081        }
 82
 83        /// <summary>
 84        /// Writes the property name and <see cref="DateTimeOffset"/> value (as a JSON string) as part of a name/value p
 85        /// </summary>
 86        /// <param name="utf8PropertyName">The UTF-8 encoded property name of the JSON object to be written.</param>
 87        /// <param name="value">The value to write.</param>
 88        /// <exception cref="ArgumentException">
 89        /// Thrown when the specified property name is too large.
 90        /// </exception>
 91        /// <exception cref="InvalidOperationException">
 92        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 93        /// </exception>
 94        /// <remarks>
 95        /// Writes the <see cref="DateTimeOffset"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for e
 96        /// The property name is escaped before writing.
 97        /// </remarks>
 98        public void WriteString(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value)
 099        {
 0100            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 101
 0102            WriteStringEscape(utf8PropertyName, value);
 103
 0104            SetFlagToAddListSeparatorBeforeNextItem();
 0105            _tokenType = JsonTokenType.String;
 0106        }
 107
 108        private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTimeOffset value)
 0109        {
 0110            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 111
 0112            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 113
 0114            if (propertyIdx != -1)
 0115            {
 0116                WriteStringEscapeProperty(propertyName, value, propertyIdx);
 0117            }
 118            else
 0119            {
 0120                WriteStringByOptions(propertyName, value);
 0121            }
 0122        }
 123
 124        private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value)
 0125        {
 0126            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 127
 0128            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 129
 0130            if (propertyIdx != -1)
 0131            {
 0132                WriteStringEscapeProperty(utf8PropertyName, value, propertyIdx);
 0133            }
 134            else
 0135            {
 0136                WriteStringByOptions(utf8PropertyName, value);
 0137            }
 0138        }
 139
 140        private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTimeOffset value, int firstEscapeInd
 0141        {
 0142            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0143            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 144
 0145            char[]? propertyArray = null;
 146
 0147            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 148
 0149            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0150                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0151                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 152
 0153            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 154
 0155            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 156
 0157            if (propertyArray != null)
 0158            {
 0159                ArrayPool<char>.Shared.Return(propertyArray);
 0160            }
 0161        }
 162
 163        private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value, int firstEscap
 0164        {
 0165            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0166            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 167
 0168            byte[]? propertyArray = null;
 169
 0170            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 171
 0172            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0173                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0174                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 175
 0176            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 177
 0178            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 179
 0180            if (propertyArray != null)
 0181            {
 0182                ArrayPool<byte>.Shared.Return(propertyArray);
 0183            }
 0184        }
 185
 186        private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTimeOffset value)
 0187        {
 0188            ValidateWritingProperty();
 0189            if (_options.Indented)
 0190            {
 0191                WriteStringIndented(propertyName, value);
 0192            }
 193            else
 0194            {
 0195                WriteStringMinimized(propertyName, value);
 0196            }
 0197        }
 198
 199        private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value)
 0200        {
 0201            ValidateWritingProperty();
 0202            if (_options.Indented)
 0203            {
 0204                WriteStringIndented(utf8PropertyName, value);
 0205            }
 206            else
 0207            {
 0208                WriteStringMinimized(utf8PropertyName, value);
 0209            }
 0210        }
 211
 212        private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, DateTimeOffset value)
 0213        {
 0214            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 215
 216            // All ASCII, 2 quotes for property name, 2 quotes for date, and 1 colon => escapedPropertyName.Length + Jso
 217            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0218            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 219
 0220            if (_memory.Length - BytesPending < maxRequired)
 0221            {
 0222                Grow(maxRequired);
 0223            }
 224
 0225            Span<byte> output = _memory.Span;
 226
 0227            if (_currentDepth < 0)
 0228            {
 0229                output[BytesPending++] = JsonConstants.ListSeparator;
 0230            }
 0231            output[BytesPending++] = JsonConstants.Quote;
 232
 0233            TranscodeAndWrite(escapedPropertyName, output);
 234
 0235            output[BytesPending++] = JsonConstants.Quote;
 0236            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 237
 0238            output[BytesPending++] = JsonConstants.Quote;
 239
 0240            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0241            BytesPending += bytesWritten;
 242
 0243            output[BytesPending++] = JsonConstants.Quote;
 0244        }
 245
 246        private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, DateTimeOffset value)
 0247        {
 0248            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatDateTimeOffsetLength - 6
 249
 0250            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatDateTimeOffsetLength + 5; // 2 quo
 0251            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 252
 0253            if (_memory.Length - BytesPending < maxRequired)
 0254            {
 0255                Grow(maxRequired);
 0256            }
 257
 0258            Span<byte> output = _memory.Span;
 259
 0260            if (_currentDepth < 0)
 0261            {
 0262                output[BytesPending++] = JsonConstants.ListSeparator;
 0263            }
 0264            output[BytesPending++] = JsonConstants.Quote;
 265
 0266            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0267            BytesPending += escapedPropertyName.Length;
 268
 0269            output[BytesPending++] = JsonConstants.Quote;
 0270            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 271
 0272            output[BytesPending++] = JsonConstants.Quote;
 273
 0274            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0275            BytesPending += bytesWritten;
 276
 0277            output[BytesPending++] = JsonConstants.Quote;
 0278        }
 279
 280        private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTimeOffset value)
 0281        {
 0282            int indent = Indentation;
 0283            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 284
 0285            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 286
 287            // All ASCII, 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space => escapedPropertyName.Len
 288            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0289            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 290
 0291            if (_memory.Length - BytesPending < maxRequired)
 0292            {
 0293                Grow(maxRequired);
 0294            }
 295
 0296            Span<byte> output = _memory.Span;
 297
 0298            if (_currentDepth < 0)
 0299            {
 0300                output[BytesPending++] = JsonConstants.ListSeparator;
 0301            }
 302
 0303            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 304
 0305            if (_tokenType != JsonTokenType.None)
 0306            {
 0307                WriteNewLine(output);
 0308            }
 309
 0310            WriteIndentation(output.Slice(BytesPending), indent);
 0311            BytesPending += indent;
 312
 0313            output[BytesPending++] = JsonConstants.Quote;
 314
 0315            TranscodeAndWrite(escapedPropertyName, output);
 316
 0317            output[BytesPending++] = JsonConstants.Quote;
 0318            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0319            output[BytesPending++] = JsonConstants.Space;
 320
 0321            output[BytesPending++] = JsonConstants.Quote;
 322
 0323            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0324            BytesPending += bytesWritten;
 325
 0326            output[BytesPending++] = JsonConstants.Quote;
 0327        }
 328
 329        private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTimeOffset value)
 0330        {
 0331            int indent = Indentation;
 0332            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 333
 0334            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDateTimeOffsetL
 335
 0336            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatDateTimeOffsetLength + 6;
 0337            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 338
 0339            if (_memory.Length - BytesPending < maxRequired)
 0340            {
 0341                Grow(maxRequired);
 0342            }
 343
 0344            Span<byte> output = _memory.Span;
 345
 0346            if (_currentDepth < 0)
 0347            {
 0348                output[BytesPending++] = JsonConstants.ListSeparator;
 0349            }
 350
 0351            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 352
 0353            if (_tokenType != JsonTokenType.None)
 0354            {
 0355                WriteNewLine(output);
 0356            }
 357
 0358            WriteIndentation(output.Slice(BytesPending), indent);
 0359            BytesPending += indent;
 360
 0361            output[BytesPending++] = JsonConstants.Quote;
 362
 0363            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0364            BytesPending += escapedPropertyName.Length;
 365
 0366            output[BytesPending++] = JsonConstants.Quote;
 0367            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0368            output[BytesPending++] = JsonConstants.Space;
 369
 0370            output[BytesPending++] = JsonConstants.Quote;
 371
 0372            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0373            BytesPending += bytesWritten;
 374
 0375            output[BytesPending++] = JsonConstants.Quote;
 0376        }
 377
 378        internal void WritePropertyName(DateTimeOffset value)
 0379        {
 0380            Span<byte> buffer = stackalloc byte[JsonConstants.MaximumFormatDateTimeOffsetLength];
 0381            JsonWriterHelper.WriteDateTimeOffsetTrimmed(buffer, value, out int bytesWritten);
 0382            WritePropertyNameUnescaped(buffer.Slice(0, bytesWritten));
 0383        }
 384    }
 385}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Decimal.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="decimal"/> value (as a JSON number) as part of a name/va
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="decimal"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 22        /// </remarks>
 23        public void WriteNumber(JsonEncodedText propertyName, decimal value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            WriteNumberByOptions(utf8PropertyName, value);
 29
 030            SetFlagToAddListSeparatorBeforeNextItem();
 031            _tokenType = JsonTokenType.Number;
 032        }
 33
 34        /// <summary>
 35        /// Writes the property name and <see cref="decimal"/> value (as a JSON number) as part of a name/value pair of 
 36        /// </summary>
 37        /// <param name="propertyName">The name of the property to write.</param>
 38        /// <param name="value">The value to write.</param>
 39        /// <exception cref="ArgumentException">
 40        /// Thrown when the specified property name is too large.
 41        /// </exception>
 42        /// <exception cref="ArgumentNullException">
 43        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 44        /// </exception>
 45        /// <exception cref="InvalidOperationException">
 46        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 47        /// </exception>
 48        /// <remarks>
 49        /// Writes the <see cref="decimal"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 50        /// The property name is escaped before writing.
 51        /// </remarks>
 52        public void WriteNumber(string propertyName, decimal value)
 053        {
 054            ArgumentNullException.ThrowIfNull(propertyName);
 055            WriteNumber(propertyName.AsSpan(), value);
 056        }
 57
 58        /// <summary>
 59        /// Writes the property name and <see cref="decimal"/> value (as a JSON number) as part of a name/value pair of 
 60        /// </summary>
 61        /// <param name="propertyName">The name of the property to write.</param>
 62        /// <param name="value">The value to write.</param>
 63        /// <exception cref="ArgumentException">
 64        /// Thrown when the specified property name is too large.
 65        /// </exception>
 66        /// <exception cref="InvalidOperationException">
 67        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 68        /// </exception>
 69        /// <remarks>
 70        /// Writes the <see cref="decimal"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 71        /// The property name is escaped before writing.
 72        /// </remarks>
 73        public void WriteNumber(ReadOnlySpan<char> propertyName, decimal value)
 074        {
 075            JsonWriterHelper.ValidateProperty(propertyName);
 76
 077            WriteNumberEscape(propertyName, value);
 78
 079            SetFlagToAddListSeparatorBeforeNextItem();
 080            _tokenType = JsonTokenType.Number;
 081        }
 82
 83        /// <summary>
 84        /// Writes the property name and <see cref="decimal"/> value (as a JSON number) as part of a name/value pair of 
 85        /// </summary>
 86        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 87        /// <param name="value">The value to write.</param>
 88        /// <exception cref="ArgumentException">
 89        /// Thrown when the specified property name is too large.
 90        /// </exception>
 91        /// <exception cref="InvalidOperationException">
 92        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 93        /// </exception>
 94        /// <remarks>
 95        /// Writes the <see cref="decimal"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 96        /// The property name is escaped before writing.
 97        /// </remarks>
 98        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, decimal value)
 099        {
 0100            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 101
 0102            WriteNumberEscape(utf8PropertyName, value);
 103
 0104            SetFlagToAddListSeparatorBeforeNextItem();
 0105            _tokenType = JsonTokenType.Number;
 0106        }
 107
 108        private void WriteNumberEscape(ReadOnlySpan<char> propertyName, decimal value)
 0109        {
 0110            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 111
 0112            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 113
 0114            if (propertyIdx != -1)
 0115            {
 0116                WriteNumberEscapeProperty(propertyName, value, propertyIdx);
 0117            }
 118            else
 0119            {
 0120                WriteNumberByOptions(propertyName, value);
 0121            }
 0122        }
 123
 124        private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, decimal value)
 0125        {
 0126            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 127
 0128            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 129
 0130            if (propertyIdx != -1)
 0131            {
 0132                WriteNumberEscapeProperty(utf8PropertyName, value, propertyIdx);
 0133            }
 134            else
 0135            {
 0136                WriteNumberByOptions(utf8PropertyName, value);
 0137            }
 0138        }
 139
 140        private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal value, int firstEscapeIndexProp)
 0141        {
 0142            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0143            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 144
 0145            char[]? propertyArray = null;
 146
 0147            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 148
 0149            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0150                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0151                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 152
 0153            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 154
 0155            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 156
 0157            if (propertyArray != null)
 0158            {
 0159                ArrayPool<char>.Shared.Return(propertyArray);
 0160            }
 0161        }
 162
 163        private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, decimal value, int firstEscapeIndexP
 0164        {
 0165            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0166            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 167
 0168            byte[]? propertyArray = null;
 169
 0170            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 171
 0172            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0173                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0174                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 175
 0176            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 177
 0178            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 179
 0180            if (propertyArray != null)
 0181            {
 0182                ArrayPool<byte>.Shared.Return(propertyArray);
 0183            }
 0184        }
 185
 186        private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, decimal value)
 0187        {
 0188            ValidateWritingProperty();
 0189            if (_options.Indented)
 0190            {
 0191                WriteNumberIndented(propertyName, value);
 0192            }
 193            else
 0194            {
 0195                WriteNumberMinimized(propertyName, value);
 0196            }
 0197        }
 198
 199        private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, decimal value)
 0200        {
 0201            ValidateWritingProperty();
 0202            if (_options.Indented)
 0203            {
 0204                WriteNumberIndented(utf8PropertyName, value);
 0205            }
 206            else
 0207            {
 0208                WriteNumberMinimized(utf8PropertyName, value);
 0209            }
 0210        }
 211
 212        private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, decimal value)
 0213        {
 0214            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 215
 216            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + JsonConstants.MaximumF
 217            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0218            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 219
 0220            if (_memory.Length - BytesPending < maxRequired)
 0221            {
 0222                Grow(maxRequired);
 0223            }
 224
 0225            Span<byte> output = _memory.Span;
 226
 0227            if (_currentDepth < 0)
 0228            {
 0229                output[BytesPending++] = JsonConstants.ListSeparator;
 0230            }
 0231            output[BytesPending++] = JsonConstants.Quote;
 232
 0233            TranscodeAndWrite(escapedPropertyName, output);
 234
 0235            output[BytesPending++] = JsonConstants.Quote;
 0236            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 237
 0238            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0239            Debug.Assert(result);
 0240            BytesPending += bytesWritten;
 0241        }
 242
 243        private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, decimal value)
 0244        {
 0245            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatDecimalLength - 4);
 246
 0247            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatDecimalLength + 3; // 2 quotes for
 0248            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 249
 0250            if (_memory.Length - BytesPending < maxRequired)
 0251            {
 0252                Grow(maxRequired);
 0253            }
 254
 0255            Span<byte> output = _memory.Span;
 256
 0257            if (_currentDepth < 0)
 0258            {
 0259                output[BytesPending++] = JsonConstants.ListSeparator;
 0260            }
 0261            output[BytesPending++] = JsonConstants.Quote;
 262
 0263            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0264            BytesPending += escapedPropertyName.Length;
 265
 0266            output[BytesPending++] = JsonConstants.Quote;
 0267            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 268
 0269            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0270            Debug.Assert(result);
 0271            BytesPending += bytesWritten;
 0272        }
 273
 274        private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, decimal value)
 0275        {
 0276            int indent = Indentation;
 0277            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 278
 0279            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 280
 281            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants
 282            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0283            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 284
 0285            if (_memory.Length - BytesPending < maxRequired)
 0286            {
 0287                Grow(maxRequired);
 0288            }
 289
 0290            Span<byte> output = _memory.Span;
 291
 0292            if (_currentDepth < 0)
 0293            {
 0294                output[BytesPending++] = JsonConstants.ListSeparator;
 0295            }
 296
 0297            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 298
 0299            if (_tokenType != JsonTokenType.None)
 0300            {
 0301                WriteNewLine(output);
 0302            }
 303
 0304            WriteIndentation(output.Slice(BytesPending), indent);
 0305            BytesPending += indent;
 306
 0307            output[BytesPending++] = JsonConstants.Quote;
 308
 0309            TranscodeAndWrite(escapedPropertyName, output);
 310
 0311            output[BytesPending++] = JsonConstants.Quote;
 0312            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0313            output[BytesPending++] = JsonConstants.Space;
 314
 0315            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0316            Debug.Assert(result);
 0317            BytesPending += bytesWritten;
 0318        }
 319
 320        private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, decimal value)
 0321        {
 0322            int indent = Indentation;
 0323            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 324
 0325            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDecimalLength -
 326
 0327            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatDecimalLength + 4; // 2 q
 0328            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 329
 0330            if (_memory.Length - BytesPending < maxRequired)
 0331            {
 0332                Grow(maxRequired);
 0333            }
 334
 0335            Span<byte> output = _memory.Span;
 336
 0337            if (_currentDepth < 0)
 0338            {
 0339                output[BytesPending++] = JsonConstants.ListSeparator;
 0340            }
 341
 0342            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 343
 0344            if (_tokenType != JsonTokenType.None)
 0345            {
 0346                WriteNewLine(output);
 0347            }
 348
 0349            WriteIndentation(output.Slice(BytesPending), indent);
 0350            BytesPending += indent;
 351
 0352            output[BytesPending++] = JsonConstants.Quote;
 353
 0354            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0355            BytesPending += escapedPropertyName.Length;
 356
 0357            output[BytesPending++] = JsonConstants.Quote;
 0358            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0359            output[BytesPending++] = JsonConstants.Space;
 360
 0361            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0362            Debug.Assert(result);
 0363            BytesPending += bytesWritten;
 0364        }
 365
 366        internal void WritePropertyName(decimal value)
 0367        {
 0368            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatDecimalLength];
 0369            bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
 0370            Debug.Assert(result);
 0371            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0372        }
 373    }
 374}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Double.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="double"/> value (as a JSON number) as part of a name/val
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="double"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 22        /// </remarks>
 23        public void WriteNumber(JsonEncodedText propertyName, double value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            JsonWriterHelper.ValidateDouble(value);
 29
 030            WriteNumberByOptions(utf8PropertyName, value);
 31
 032            SetFlagToAddListSeparatorBeforeNextItem();
 033            _tokenType = JsonTokenType.Number;
 034        }
 35
 36        /// <summary>
 37        /// Writes the property name and <see cref="double"/> value (as a JSON number) as part of a name/value pair of a
 38        /// </summary>
 39        /// <param name="propertyName">The name of the property to write.</param>
 40        /// <param name="value">The value to write.</param>
 41        /// <exception cref="ArgumentException">
 42        /// Thrown when the specified property name is too large.
 43        /// </exception>
 44        /// <exception cref="ArgumentNullException">
 45        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 46        /// </exception>
 47        /// <exception cref="InvalidOperationException">
 48        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 49        /// </exception>
 50        /// <remarks>
 51        /// Writes the <see cref="double"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 52        /// The property name is escaped before writing.
 53        /// </remarks>
 54        public void WriteNumber(string propertyName, double value)
 055        {
 056            ArgumentNullException.ThrowIfNull(propertyName);
 057            WriteNumber(propertyName.AsSpan(), value);
 058        }
 59
 60        /// <summary>
 61        /// Writes the property name and <see cref="double"/> value (as a JSON number) as part of a name/value pair of a
 62        /// </summary>
 63        /// <param name="propertyName">The name of the property to write.</param>
 64        /// <param name="value">The value to write.</param>
 65        /// <exception cref="ArgumentException">
 66        /// Thrown when the specified property name is too large.
 67        /// </exception>
 68        /// <exception cref="InvalidOperationException">
 69        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 70        /// </exception>
 71        /// <remarks>
 72        /// Writes the <see cref="double"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 73        /// The property name is escaped before writing.
 74        /// </remarks>
 75        public void WriteNumber(ReadOnlySpan<char> propertyName, double value)
 076        {
 077            JsonWriterHelper.ValidateProperty(propertyName);
 078            JsonWriterHelper.ValidateDouble(value);
 79
 080            WriteNumberEscape(propertyName, value);
 81
 082            SetFlagToAddListSeparatorBeforeNextItem();
 083            _tokenType = JsonTokenType.Number;
 084        }
 85
 86        /// <summary>
 87        /// Writes the property name and <see cref="double"/> value (as a JSON number) as part of a name/value pair of a
 88        /// </summary>
 89        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 90        /// <param name="value">The value to write.</param>
 91        /// <exception cref="ArgumentException">
 92        /// Thrown when the specified property name is too large.
 93        /// </exception>
 94        /// <exception cref="InvalidOperationException">
 95        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 96        /// </exception>
 97        /// <remarks>
 98        /// Writes the <see cref="double"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 99        /// The property name is escaped before writing.
 100        /// </remarks>
 101        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, double value)
 0102        {
 0103            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 0104            JsonWriterHelper.ValidateDouble(value);
 105
 0106            WriteNumberEscape(utf8PropertyName, value);
 107
 0108            SetFlagToAddListSeparatorBeforeNextItem();
 0109            _tokenType = JsonTokenType.Number;
 0110        }
 111
 112        private void WriteNumberEscape(ReadOnlySpan<char> propertyName, double value)
 0113        {
 0114            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 115
 0116            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 117
 0118            if (propertyIdx != -1)
 0119            {
 0120                WriteNumberEscapeProperty(propertyName, value, propertyIdx);
 0121            }
 122            else
 0123            {
 0124                WriteNumberByOptions(propertyName, value);
 0125            }
 0126        }
 127
 128        private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, double value)
 0129        {
 0130            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 131
 0132            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 133
 0134            if (propertyIdx != -1)
 0135            {
 0136                WriteNumberEscapeProperty(utf8PropertyName, value, propertyIdx);
 0137            }
 138            else
 0139            {
 0140                WriteNumberByOptions(utf8PropertyName, value);
 0141            }
 0142        }
 143
 144        private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, double value, int firstEscapeIndexProp)
 0145        {
 0146            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0147            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 148
 0149            char[]? propertyArray = null;
 150
 0151            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 152
 0153            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0154                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0155                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 156
 0157            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 158
 0159            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 160
 0161            if (propertyArray != null)
 0162            {
 0163                ArrayPool<char>.Shared.Return(propertyArray);
 0164            }
 0165        }
 166
 167        private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, double value, int firstEscapeIndexPr
 0168        {
 0169            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0170            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 171
 0172            byte[]? propertyArray = null;
 173
 0174            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 175
 0176            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0177                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0178                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 179
 0180            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 181
 0182            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 183
 0184            if (propertyArray != null)
 0185            {
 0186                ArrayPool<byte>.Shared.Return(propertyArray);
 0187            }
 0188        }
 189
 190        private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, double value)
 0191        {
 0192            ValidateWritingProperty();
 0193            if (_options.Indented)
 0194            {
 0195                WriteNumberIndented(propertyName, value);
 0196            }
 197            else
 0198            {
 0199                WriteNumberMinimized(propertyName, value);
 0200            }
 0201        }
 202
 203        private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, double value)
 0204        {
 0205            ValidateWritingProperty();
 0206            if (_options.Indented)
 0207            {
 0208                WriteNumberIndented(utf8PropertyName, value);
 0209            }
 210            else
 0211            {
 0212                WriteNumberMinimized(utf8PropertyName, value);
 0213            }
 0214        }
 215
 216        private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, double value)
 0217        {
 0218            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 219
 220            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + JsonConstants.MaximumF
 221            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0222            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 223
 0224            if (_memory.Length - BytesPending < maxRequired)
 0225            {
 0226                Grow(maxRequired);
 0227            }
 228
 0229            Span<byte> output = _memory.Span;
 230
 0231            if (_currentDepth < 0)
 0232            {
 0233                output[BytesPending++] = JsonConstants.ListSeparator;
 0234            }
 0235            output[BytesPending++] = JsonConstants.Quote;
 236
 0237            TranscodeAndWrite(escapedPropertyName, output);
 238
 0239            output[BytesPending++] = JsonConstants.Quote;
 0240            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 241
 0242            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 0243            Debug.Assert(result);
 0244            BytesPending += bytesWritten;
 0245        }
 246
 247        private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, double value)
 0248        {
 0249            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatDoubleLength - 4);
 250
 0251            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatDoubleLength + 3; // 2 quotes for 
 0252            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 253
 0254            if (_memory.Length - BytesPending < maxRequired)
 0255            {
 0256                Grow(maxRequired);
 0257            }
 258
 0259            Span<byte> output = _memory.Span;
 260
 0261            if (_currentDepth < 0)
 0262            {
 0263                output[BytesPending++] = JsonConstants.ListSeparator;
 0264            }
 0265            output[BytesPending++] = JsonConstants.Quote;
 266
 0267            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0268            BytesPending += escapedPropertyName.Length;
 269
 0270            output[BytesPending++] = JsonConstants.Quote;
 0271            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 272
 0273            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 0274            Debug.Assert(result);
 0275            BytesPending += bytesWritten;
 0276        }
 277
 278        private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, double value)
 0279        {
 0280            int indent = Indentation;
 0281            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 282
 0283            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 284
 285            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants
 286            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0287            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 288
 0289            if (_memory.Length - BytesPending < maxRequired)
 0290            {
 0291                Grow(maxRequired);
 0292            }
 293
 0294            Span<byte> output = _memory.Span;
 295
 0296            if (_currentDepth < 0)
 0297            {
 0298                output[BytesPending++] = JsonConstants.ListSeparator;
 0299            }
 300
 0301            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 302
 0303            if (_tokenType != JsonTokenType.None)
 0304            {
 0305                WriteNewLine(output);
 0306            }
 307
 0308            WriteIndentation(output.Slice(BytesPending), indent);
 0309            BytesPending += indent;
 310
 0311            output[BytesPending++] = JsonConstants.Quote;
 312
 0313            TranscodeAndWrite(escapedPropertyName, output);
 314
 0315            output[BytesPending++] = JsonConstants.Quote;
 0316            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0317            output[BytesPending++] = JsonConstants.Space;
 318
 0319            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 0320            Debug.Assert(result);
 0321            BytesPending += bytesWritten;
 0322        }
 323
 324        private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, double value)
 0325        {
 0326            int indent = Indentation;
 0327            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 328
 0329            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDoubleLength - 
 330
 0331            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatDoubleLength + 4; // 2 qu
 0332            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 333
 0334            if (_memory.Length - BytesPending < maxRequired)
 0335            {
 0336                Grow(maxRequired);
 0337            }
 338
 0339            Span<byte> output = _memory.Span;
 340
 0341            if (_currentDepth < 0)
 0342            {
 0343                output[BytesPending++] = JsonConstants.ListSeparator;
 0344            }
 345
 0346            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 347
 0348            if (_tokenType != JsonTokenType.None)
 0349            {
 0350                WriteNewLine(output);
 0351            }
 352
 0353            WriteIndentation(output.Slice(BytesPending), indent);
 0354            BytesPending += indent;
 355
 0356            output[BytesPending++] = JsonConstants.Quote;
 357
 0358            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0359            BytesPending += escapedPropertyName.Length;
 360
 0361            output[BytesPending++] = JsonConstants.Quote;
 0362            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0363            output[BytesPending++] = JsonConstants.Space;
 364
 0365            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 0366            Debug.Assert(result);
 0367            BytesPending += bytesWritten;
 0368        }
 369
 370        internal void WritePropertyName(double value)
 0371        {
 0372            JsonWriterHelper.ValidateDouble(value);
 0373            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatDoubleLength];
 0374            bool result = TryFormatDouble(value, utf8PropertyName, out int bytesWritten);
 0375            Debug.Assert(result);
 0376            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0377        }
 378    }
 379}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Float.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="float"/> value (as a JSON number) as part of a name/valu
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write..</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="float"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 22        /// </remarks>
 23        public void WriteNumber(JsonEncodedText propertyName, float value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            JsonWriterHelper.ValidateSingle(value);
 29
 030            WriteNumberByOptions(utf8PropertyName, value);
 31
 032            SetFlagToAddListSeparatorBeforeNextItem();
 033            _tokenType = JsonTokenType.Number;
 034        }
 35
 36        /// <summary>
 37        /// Writes the property name and <see cref="float"/> value (as a JSON number) as part of a name/value pair of a 
 38        /// </summary>
 39        /// <param name="propertyName">The name of the property to write..</param>
 40        /// <param name="value">The value to write.</param>
 41        /// <exception cref="ArgumentException">
 42        /// Thrown when the specified property name is too large.
 43        /// </exception>
 44        /// <exception cref="ArgumentNullException">
 45        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 46        /// </exception>
 47        /// <exception cref="InvalidOperationException">
 48        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 49        /// </exception>
 50        /// <remarks>
 51        /// Writes the <see cref="float"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 52        /// The property name is escaped before writing.
 53        /// </remarks>
 54        public void WriteNumber(string propertyName, float value)
 055        {
 056            ArgumentNullException.ThrowIfNull(propertyName);
 057            WriteNumber(propertyName.AsSpan(), value);
 058        }
 59
 60        /// <summary>
 61        /// Writes the property name and <see cref="float"/> value (as a JSON number) as part of a name/value pair of a 
 62        /// </summary>
 63        /// <param name="propertyName">The name of the property to write..</param>
 64        /// <param name="value">The value to write.</param>
 65        /// <exception cref="ArgumentException">
 66        /// Thrown when the specified property name is too large.
 67        /// </exception>
 68        /// <exception cref="InvalidOperationException">
 69        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 70        /// </exception>
 71        /// <remarks>
 72        /// Writes the <see cref="float"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 73        /// The property name is escaped before writing.
 74        /// </remarks>
 75        public void WriteNumber(ReadOnlySpan<char> propertyName, float value)
 076        {
 077            JsonWriterHelper.ValidateProperty(propertyName);
 078            JsonWriterHelper.ValidateSingle(value);
 79
 080            WriteNumberEscape(propertyName, value);
 81
 082            SetFlagToAddListSeparatorBeforeNextItem();
 083            _tokenType = JsonTokenType.Number;
 084        }
 85
 86        /// <summary>
 87        /// Writes the property name and <see cref="float"/> value (as a JSON number) as part of a name/value pair of a 
 88        /// </summary>
 89        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write</param>
 90        /// <param name="value">The value to write.</param>
 91        /// <exception cref="ArgumentException">
 92        /// Thrown when the specified property name is too large.
 93        /// </exception>
 94        /// <exception cref="InvalidOperationException">
 95        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 96        /// </exception>
 97        /// <remarks>
 98        /// Writes the <see cref="float"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 99        /// The property name is escaped before writing.
 100        /// </remarks>
 101        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, float value)
 0102        {
 0103            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 0104            JsonWriterHelper.ValidateSingle(value);
 105
 0106            WriteNumberEscape(utf8PropertyName, value);
 107
 0108            SetFlagToAddListSeparatorBeforeNextItem();
 0109            _tokenType = JsonTokenType.Number;
 0110        }
 111
 112        private void WriteNumberEscape(ReadOnlySpan<char> propertyName, float value)
 0113        {
 0114            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 115
 0116            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 117
 0118            if (propertyIdx != -1)
 0119            {
 0120                WriteNumberEscapeProperty(propertyName, value, propertyIdx);
 0121            }
 122            else
 0123            {
 0124                WriteNumberByOptions(propertyName, value);
 0125            }
 0126        }
 127
 128        private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, float value)
 0129        {
 0130            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 131
 0132            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 133
 0134            if (propertyIdx != -1)
 0135            {
 0136                WriteNumberEscapeProperty(utf8PropertyName, value, propertyIdx);
 0137            }
 138            else
 0139            {
 0140                WriteNumberByOptions(utf8PropertyName, value);
 0141            }
 0142        }
 143
 144        private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, float value, int firstEscapeIndexProp)
 0145        {
 0146            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0147            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 148
 0149            char[]? propertyArray = null;
 150
 0151            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 152
 0153            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0154                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0155                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 156
 0157            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 158
 0159            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 160
 0161            if (propertyArray != null)
 0162            {
 0163                ArrayPool<char>.Shared.Return(propertyArray);
 0164            }
 0165        }
 166
 167        private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, float value, int firstEscapeIndexPro
 0168        {
 0169            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0170            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 171
 0172            byte[]? propertyArray = null;
 173
 0174            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 175
 0176            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0177                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0178                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 179
 0180            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 181
 0182            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 183
 0184            if (propertyArray != null)
 0185            {
 0186                ArrayPool<byte>.Shared.Return(propertyArray);
 0187            }
 0188        }
 189
 190        private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, float value)
 0191        {
 0192            ValidateWritingProperty();
 0193            if (_options.Indented)
 0194            {
 0195                WriteNumberIndented(propertyName, value);
 0196            }
 197            else
 0198            {
 0199                WriteNumberMinimized(propertyName, value);
 0200            }
 0201        }
 202
 203        private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, float value)
 0204        {
 0205            ValidateWritingProperty();
 0206            if (_options.Indented)
 0207            {
 0208                WriteNumberIndented(utf8PropertyName, value);
 0209            }
 210            else
 0211            {
 0212                WriteNumberMinimized(utf8PropertyName, value);
 0213            }
 0214        }
 215
 216        private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, float value)
 0217        {
 0218            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 219
 220            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + JsonConstants.MaximumF
 221            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0222            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 223
 0224            if (_memory.Length - BytesPending < maxRequired)
 0225            {
 0226                Grow(maxRequired);
 0227            }
 228
 0229            Span<byte> output = _memory.Span;
 230
 0231            if (_currentDepth < 0)
 0232            {
 0233                output[BytesPending++] = JsonConstants.ListSeparator;
 0234            }
 0235            output[BytesPending++] = JsonConstants.Quote;
 236
 0237            TranscodeAndWrite(escapedPropertyName, output);
 238
 0239            output[BytesPending++] = JsonConstants.Quote;
 0240            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 241
 0242            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 0243            Debug.Assert(result);
 0244            BytesPending += bytesWritten;
 0245        }
 246
 247        private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, float value)
 0248        {
 0249            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatSingleLength - 4);
 250
 0251            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatSingleLength + 3; // 2 quotes for 
 0252            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 253
 0254            if (_memory.Length - BytesPending < maxRequired)
 0255            {
 0256                Grow(maxRequired);
 0257            }
 258
 0259            Span<byte> output = _memory.Span;
 260
 0261            if (_currentDepth < 0)
 0262            {
 0263                output[BytesPending++] = JsonConstants.ListSeparator;
 0264            }
 0265            output[BytesPending++] = JsonConstants.Quote;
 266
 0267            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0268            BytesPending += escapedPropertyName.Length;
 269
 0270            output[BytesPending++] = JsonConstants.Quote;
 0271            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 272
 0273            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 0274            Debug.Assert(result);
 0275            BytesPending += bytesWritten;
 0276        }
 277
 278        private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, float value)
 0279        {
 0280            int indent = Indentation;
 0281            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 282
 0283            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 284
 285            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants
 286            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0287            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 288
 0289            if (_memory.Length - BytesPending < maxRequired)
 0290            {
 0291                Grow(maxRequired);
 0292            }
 293
 0294            Span<byte> output = _memory.Span;
 295
 0296            if (_currentDepth < 0)
 0297            {
 0298                output[BytesPending++] = JsonConstants.ListSeparator;
 0299            }
 300
 0301            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 302
 0303            if (_tokenType != JsonTokenType.None)
 0304            {
 0305                WriteNewLine(output);
 0306            }
 307
 0308            WriteIndentation(output.Slice(BytesPending), indent);
 0309            BytesPending += indent;
 310
 0311            output[BytesPending++] = JsonConstants.Quote;
 312
 0313            TranscodeAndWrite(escapedPropertyName, output);
 314
 0315            output[BytesPending++] = JsonConstants.Quote;
 0316            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0317            output[BytesPending++] = JsonConstants.Space;
 318
 0319            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 0320            Debug.Assert(result);
 0321            BytesPending += bytesWritten;
 0322        }
 323
 324        private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, float value)
 0325        {
 0326            int indent = Indentation;
 0327            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 328
 0329            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatSingleLength - 
 330
 0331            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatSingleLength + 4; // 2 qu
 0332            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 333
 0334            if (_memory.Length - BytesPending < maxRequired)
 0335            {
 0336                Grow(maxRequired);
 0337            }
 338
 0339            Span<byte> output = _memory.Span;
 340
 0341            if (_currentDepth < 0)
 0342            {
 0343                output[BytesPending++] = JsonConstants.ListSeparator;
 0344            }
 345
 0346            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 347
 0348            if (_tokenType != JsonTokenType.None)
 0349            {
 0350                WriteNewLine(output);
 0351            }
 352
 0353            WriteIndentation(output.Slice(BytesPending), indent);
 0354            BytesPending += indent;
 355
 0356            output[BytesPending++] = JsonConstants.Quote;
 357
 0358            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0359            BytesPending += escapedPropertyName.Length;
 360
 0361            output[BytesPending++] = JsonConstants.Quote;
 0362            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0363            output[BytesPending++] = JsonConstants.Space;
 364
 0365            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 0366            Debug.Assert(result);
 0367            BytesPending += bytesWritten;
 0368        }
 369
 370        internal void WritePropertyName(float value)
 0371        {
 0372            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatSingleLength];
 0373            bool result = TryFormatSingle(value, utf8PropertyName, out int bytesWritten);
 0374            Debug.Assert(result);
 0375            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0376        }
 377    }
 378}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Guid.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="Guid"/> value (as a JSON string) as part of a name/value
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="Guid"/> using the default <see cref="StandardFormat"/> (that is, 'D'), as the form: nn
 22        /// </remarks>
 23        public void WriteString(JsonEncodedText propertyName, Guid value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            WriteStringByOptions(utf8PropertyName, value);
 29
 030            SetFlagToAddListSeparatorBeforeNextItem();
 031            _tokenType = JsonTokenType.String;
 032        }
 33
 34        /// <summary>
 35        /// Writes the property name and <see cref="Guid"/> value (as a JSON string) as part of a name/value pair of a J
 36        /// </summary>
 37        /// <param name="propertyName">The name of the property to write.</param>
 38        /// <param name="value">The value to write.</param>
 39        /// <exception cref="ArgumentException">
 40        /// Thrown when the specified property name is too large.
 41        /// </exception>
 42        /// <exception cref="ArgumentNullException">
 43        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 44        /// </exception>
 45        /// <exception cref="InvalidOperationException">
 46        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 47        /// </exception>
 48        /// <remarks>
 49        /// Writes the <see cref="Guid"/> using the default <see cref="StandardFormat"/> (that is, 'D'), as the form: nn
 50        /// The property name is escaped before writing.
 51        /// </remarks>
 52        public void WriteString(string propertyName, Guid value)
 053        {
 054            ArgumentNullException.ThrowIfNull(propertyName);
 055            WriteString(propertyName.AsSpan(), value);
 056        }
 57
 58        /// <summary>
 59        /// Writes the property name and <see cref="Guid"/> value (as a JSON string) as part of a name/value pair of a J
 60        /// </summary>
 61        /// <param name="propertyName">The name of the property to write.</param>
 62        /// <param name="value">The value to write.</param>
 63        /// <exception cref="ArgumentException">
 64        /// Thrown when the specified property name is too large.
 65        /// </exception>
 66        /// <exception cref="InvalidOperationException">
 67        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 68        /// </exception>
 69        /// <remarks>
 70        /// Writes the <see cref="Guid"/> using the default <see cref="StandardFormat"/> (that is, 'D'), as the form: nn
 71        /// The property name is escaped before writing.
 72        /// </remarks>
 73        public void WriteString(ReadOnlySpan<char> propertyName, Guid value)
 074        {
 075            JsonWriterHelper.ValidateProperty(propertyName);
 76
 077            WriteStringEscape(propertyName, value);
 78
 079            SetFlagToAddListSeparatorBeforeNextItem();
 080            _tokenType = JsonTokenType.String;
 081        }
 82
 83        /// <summary>
 84        /// Writes the property name and <see cref="Guid"/> value (as a JSON string) as part of a name/value pair of a J
 85        /// </summary>
 86        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 87        /// <param name="value">The value to write.</param>
 88        /// <exception cref="ArgumentException">
 89        /// Thrown when the specified property name is too large.
 90        /// </exception>
 91        /// <exception cref="InvalidOperationException">
 92        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 93        /// </exception>
 94        /// <remarks>
 95        /// Writes the <see cref="Guid"/> using the default <see cref="StandardFormat"/> (that is, 'D'), as the form: nn
 96        /// The property name is escaped before writing.
 97        /// </remarks>
 98        public void WriteString(ReadOnlySpan<byte> utf8PropertyName, Guid value)
 099        {
 0100            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 101
 0102            WriteStringEscape(utf8PropertyName, value);
 103
 0104            SetFlagToAddListSeparatorBeforeNextItem();
 0105            _tokenType = JsonTokenType.String;
 0106        }
 107
 108        private void WriteStringEscape(ReadOnlySpan<char> propertyName, Guid value)
 0109        {
 0110            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 111
 0112            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 113
 0114            if (propertyIdx != -1)
 0115            {
 0116                WriteStringEscapeProperty(propertyName, value, propertyIdx);
 0117            }
 118            else
 0119            {
 0120                WriteStringByOptions(propertyName, value);
 0121            }
 0122        }
 123
 124        private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, Guid value)
 0125        {
 0126            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 127
 0128            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 129
 0130            if (propertyIdx != -1)
 0131            {
 0132                WriteStringEscapeProperty(utf8PropertyName, value, propertyIdx);
 0133            }
 134            else
 0135            {
 0136                WriteStringByOptions(utf8PropertyName, value);
 0137            }
 0138        }
 139
 140        private void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, Guid value, int firstEscapeIndexProp)
 0141        {
 0142            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0143            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 144
 0145            char[]? propertyArray = null;
 146
 0147            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 148
 0149            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0150                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0151                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 152
 0153            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 154
 0155            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 156
 0157            if (propertyArray != null)
 0158            {
 0159                ArrayPool<char>.Shared.Return(propertyArray);
 0160            }
 0161        }
 162
 163        private void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Guid value, int firstEscapeIndexProp
 0164        {
 0165            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0166            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 167
 0168            byte[]? propertyArray = null;
 169
 0170            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 171
 0172            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0173                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0174                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 175
 0176            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 177
 0178            WriteStringByOptions(escapedPropertyName.Slice(0, written), value);
 179
 0180            if (propertyArray != null)
 0181            {
 0182                ArrayPool<byte>.Shared.Return(propertyArray);
 0183            }
 0184        }
 185
 186        private void WriteStringByOptions(ReadOnlySpan<char> propertyName, Guid value)
 0187        {
 0188            ValidateWritingProperty();
 0189            if (_options.Indented)
 0190            {
 0191                WriteStringIndented(propertyName, value);
 0192            }
 193            else
 0194            {
 0195                WriteStringMinimized(propertyName, value);
 0196            }
 0197        }
 198
 199        private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, Guid value)
 0200        {
 0201            ValidateWritingProperty();
 0202            if (_options.Indented)
 0203            {
 0204                WriteStringIndented(utf8PropertyName, value);
 0205            }
 206            else
 0207            {
 0208                WriteStringMinimized(utf8PropertyName, value);
 0209            }
 0210        }
 211
 212        private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, Guid value)
 0213        {
 0214            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 215
 216            // All ASCII, 2 quotes for property name, 2 quotes for date, and 1 colon => escapedPropertyName.Length + Jso
 217            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0218            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 219
 0220            if (_memory.Length - BytesPending < maxRequired)
 0221            {
 0222                Grow(maxRequired);
 0223            }
 224
 0225            Span<byte> output = _memory.Span;
 226
 0227            if (_currentDepth < 0)
 0228            {
 0229                output[BytesPending++] = JsonConstants.ListSeparator;
 0230            }
 0231            output[BytesPending++] = JsonConstants.Quote;
 232
 0233            TranscodeAndWrite(escapedPropertyName, output);
 234
 0235            output[BytesPending++] = JsonConstants.Quote;
 0236            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 237
 0238            output[BytesPending++] = JsonConstants.Quote;
 239
 0240            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0241            Debug.Assert(result);
 0242            BytesPending += bytesWritten;
 243
 0244            output[BytesPending++] = JsonConstants.Quote;
 0245        }
 246
 247        private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, Guid value)
 0248        {
 0249            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatGuidLength - 6);
 250
 0251            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatGuidLength + 5; // 2 quotes for pr
 0252            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 253
 0254            if (_memory.Length - BytesPending < maxRequired)
 0255            {
 0256                Grow(maxRequired);
 0257            }
 258
 0259            Span<byte> output = _memory.Span;
 260
 0261            if (_currentDepth < 0)
 0262            {
 0263                output[BytesPending++] = JsonConstants.ListSeparator;
 0264            }
 0265            output[BytesPending++] = JsonConstants.Quote;
 266
 0267            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0268            BytesPending += escapedPropertyName.Length;
 269
 0270            output[BytesPending++] = JsonConstants.Quote;
 0271            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 272
 0273            output[BytesPending++] = JsonConstants.Quote;
 274
 0275            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0276            Debug.Assert(result);
 0277            BytesPending += bytesWritten;
 278
 0279            output[BytesPending++] = JsonConstants.Quote;
 0280        }
 281
 282        private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, Guid value)
 0283        {
 0284            int indent = Indentation;
 0285            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 286
 0287            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 288
 289            // All ASCII, 2 quotes for property name, 2 quotes for date, 1 colon, and 1 space => escapedPropertyName.Len
 290            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0291            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 292
 0293            if (_memory.Length - BytesPending < maxRequired)
 0294            {
 0295                Grow(maxRequired);
 0296            }
 297
 0298            Span<byte> output = _memory.Span;
 299
 0300            if (_currentDepth < 0)
 0301            {
 0302                output[BytesPending++] = JsonConstants.ListSeparator;
 0303            }
 304
 0305            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 306
 0307            if (_tokenType != JsonTokenType.None)
 0308            {
 0309                WriteNewLine(output);
 0310            }
 311
 0312            WriteIndentation(output.Slice(BytesPending), indent);
 0313            BytesPending += indent;
 314
 0315            output[BytesPending++] = JsonConstants.Quote;
 316
 0317            TranscodeAndWrite(escapedPropertyName, output);
 318
 0319            output[BytesPending++] = JsonConstants.Quote;
 0320            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0321            output[BytesPending++] = JsonConstants.Space;
 322
 0323            output[BytesPending++] = JsonConstants.Quote;
 324
 0325            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0326            Debug.Assert(result);
 0327            BytesPending += bytesWritten;
 328
 0329            output[BytesPending++] = JsonConstants.Quote;
 0330        }
 331
 332        private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, Guid value)
 0333        {
 0334            int indent = Indentation;
 0335            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 336
 0337            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatGuidLength - 7 
 338
 0339            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatGuidLength + 6; // 2 quot
 0340            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 341
 0342            if (_memory.Length - BytesPending < maxRequired)
 0343            {
 0344                Grow(maxRequired);
 0345            }
 346
 0347            Span<byte> output = _memory.Span;
 348
 0349            if (_currentDepth < 0)
 0350            {
 0351                output[BytesPending++] = JsonConstants.ListSeparator;
 0352            }
 353
 0354            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 355
 0356            if (_tokenType != JsonTokenType.None)
 0357            {
 0358                WriteNewLine(output);
 0359            }
 360
 0361            WriteIndentation(output.Slice(BytesPending), indent);
 0362            BytesPending += indent;
 363
 0364            output[BytesPending++] = JsonConstants.Quote;
 365
 0366            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0367            BytesPending += escapedPropertyName.Length;
 368
 0369            output[BytesPending++] = JsonConstants.Quote;
 0370            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0371            output[BytesPending++] = JsonConstants.Space;
 372
 0373            output[BytesPending++] = JsonConstants.Quote;
 374
 0375            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0376            Debug.Assert(result);
 0377            BytesPending += bytesWritten;
 378
 0379            output[BytesPending++] = JsonConstants.Quote;
 0380        }
 381
 382        internal void WritePropertyName(Guid value)
 0383        {
 0384            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatGuidLength];
 0385            bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
 0386            Debug.Assert(result);
 0387            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0388        }
 389    }
 390}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Helpers.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7using System.Runtime.CompilerServices;
 8using System.Runtime.InteropServices;
 9
 10namespace System.Text.Json
 11{
 12    public sealed partial class Utf8JsonWriter
 13    {
 14        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 15        private void ValidatePropertyNameAndDepth(ReadOnlySpan<char> propertyName)
 016        {
 017            if (propertyName.Length > JsonConstants.MaxCharacterTokenSize || CurrentDepth >= _options.MaxDepth)
 018                ThrowHelper.ThrowInvalidOperationOrArgumentException(propertyName, _currentDepth, _options.MaxDepth);
 019        }
 20
 21        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 22        private void ValidatePropertyNameAndDepth(ReadOnlySpan<byte> utf8PropertyName)
 023        {
 024            if (utf8PropertyName.Length > JsonConstants.MaxUnescapedTokenSize || CurrentDepth >= _options.MaxDepth)
 025                ThrowHelper.ThrowInvalidOperationOrArgumentException(utf8PropertyName, _currentDepth, _options.MaxDepth)
 026        }
 27
 28        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 29        private void ValidateDepth()
 030        {
 031            if (CurrentDepth >= _options.MaxDepth)
 032                ThrowHelper.ThrowInvalidOperationException(_currentDepth, _options.MaxDepth);
 033        }
 34
 35        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 36        private void ValidateWritingProperty()
 037        {
 038            if (!_options.SkipValidation)
 039            {
 040                if (_enclosingContainer != EnclosingContainerType.Object || _tokenType == JsonTokenType.PropertyName)
 041                {
 042                    Debug.Assert(_tokenType != JsonTokenType.StartObject);
 043                    OnValidateWritingPropertyFailed();
 44                }
 045            }
 046        }
 47
 48        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 49        private void ValidateWritingProperty(byte token)
 050        {
 051            if (!_options.SkipValidation)
 052            {
 053                if (_enclosingContainer != EnclosingContainerType.Object || _tokenType == JsonTokenType.PropertyName)
 054                {
 055                    Debug.Assert(_tokenType != JsonTokenType.StartObject);
 056                    OnValidateWritingPropertyFailed();
 57                }
 058                UpdateBitStackOnStart(token);
 059            }
 060        }
 61
 62        [DoesNotReturn]
 63        [MethodImpl(MethodImplOptions.NoInlining)]
 64        private void OnValidateWritingPropertyFailed()
 065        {
 066            if (IsWritingPartialString)
 067            {
 068                ThrowInvalidOperationException(ExceptionResource.CannotWriteWithinString);
 69            }
 70
 071            Debug.Assert(_enclosingContainer != EnclosingContainerType.Object || _tokenType == JsonTokenType.PropertyNam
 072            ThrowInvalidOperationException(ExceptionResource.CannotWritePropertyWithinArray);
 73        }
 74
 75        private void WritePropertyNameMinimized(ReadOnlySpan<byte> escapedPropertyName, byte token)
 076        {
 077            Debug.Assert(escapedPropertyName.Length < int.MaxValue - 5);
 78
 079            int minRequired = escapedPropertyName.Length + 4; // 2 quotes, 1 colon, and 1 start token
 080            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 81
 082            if (_memory.Length - BytesPending < maxRequired)
 083            {
 084                Grow(maxRequired);
 085            }
 86
 087            Span<byte> output = _memory.Span;
 88
 089            if (_currentDepth < 0)
 090            {
 091                output[BytesPending++] = JsonConstants.ListSeparator;
 092            }
 093            output[BytesPending++] = JsonConstants.Quote;
 94
 095            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 096            BytesPending += escapedPropertyName.Length;
 97
 098            output[BytesPending++] = JsonConstants.Quote;
 099            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0100            output[BytesPending++] = token;
 0101        }
 102
 103        private void WritePropertyNameIndented(ReadOnlySpan<byte> escapedPropertyName, byte token)
 0104        {
 0105            int indent = Indentation;
 0106            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 107
 0108            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - 6 - _newLineLength);
 109
 0110            int minRequired = indent + escapedPropertyName.Length + 5; // 2 quotes, 1 colon, 1 space, and 1 start token
 0111            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 112
 0113            if (_memory.Length - BytesPending < maxRequired)
 0114            {
 0115                Grow(maxRequired);
 0116            }
 117
 0118            Span<byte> output = _memory.Span;
 119
 0120            if (_currentDepth < 0)
 0121            {
 0122                output[BytesPending++] = JsonConstants.ListSeparator;
 0123            }
 124
 0125            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 126
 0127            if (_tokenType != JsonTokenType.None)
 0128            {
 0129                WriteNewLine(output);
 0130            }
 131
 0132            WriteIndentation(output.Slice(BytesPending), indent);
 0133            BytesPending += indent;
 134
 0135            output[BytesPending++] = JsonConstants.Quote;
 136
 0137            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0138            BytesPending += escapedPropertyName.Length;
 139
 0140            output[BytesPending++] = JsonConstants.Quote;
 141
 0142            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0143            output[BytesPending++] = JsonConstants.Space;
 0144            output[BytesPending++] = token;
 0145        }
 146
 147        private void WritePropertyNameMinimized(ReadOnlySpan<char> escapedPropertyName, byte token)
 0148        {
 0149            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 150
 151            // All ASCII, 2 quotes, 1 colon, and 1 start token => escapedPropertyName.Length + 4
 152            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0153            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 5;
 154
 0155            if (_memory.Length - BytesPending < maxRequired)
 0156            {
 0157                Grow(maxRequired);
 0158            }
 159
 0160            Span<byte> output = _memory.Span;
 161
 0162            if (_currentDepth < 0)
 0163            {
 0164                output[BytesPending++] = JsonConstants.ListSeparator;
 0165            }
 0166            output[BytesPending++] = JsonConstants.Quote;
 167
 0168            TranscodeAndWrite(escapedPropertyName, output);
 169
 0170            output[BytesPending++] = JsonConstants.Quote;
 0171            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0172            output[BytesPending++] = token;
 0173        }
 174
 175        private void WritePropertyNameIndented(ReadOnlySpan<char> escapedPropertyName, byte token)
 0176        {
 0177            int indent = Indentation;
 0178            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 179
 0180            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 181
 182            // All ASCII, 2 quotes, 1 colon, 1 space, and 1 start token => indent + escapedPropertyName.Length + 5
 183            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0184            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 185
 0186            if (_memory.Length - BytesPending < maxRequired)
 0187            {
 0188                Grow(maxRequired);
 0189            }
 190
 0191            Span<byte> output = _memory.Span;
 192
 0193            if (_currentDepth < 0)
 0194            {
 0195                output[BytesPending++] = JsonConstants.ListSeparator;
 0196            }
 197
 0198            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 199
 0200            if (_tokenType != JsonTokenType.None)
 0201            {
 0202                WriteNewLine(output);
 0203            }
 204
 0205            WriteIndentation(output.Slice(BytesPending), indent);
 0206            BytesPending += indent;
 207
 0208            output[BytesPending++] = JsonConstants.Quote;
 209
 0210            TranscodeAndWrite(escapedPropertyName, output);
 211
 0212            output[BytesPending++] = JsonConstants.Quote;
 213
 0214            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0215            output[BytesPending++] = JsonConstants.Space;
 0216            output[BytesPending++] = token;
 0217        }
 218
 219        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 220        private void TranscodeAndWrite(ReadOnlySpan<char> escapedPropertyName, Span<byte> output)
 0221        {
 0222            OperationStatus status = JsonWriterHelper.ToUtf8(escapedPropertyName, output.Slice(BytesPending), out int wr
 0223            Debug.Assert(status == OperationStatus.Done);
 0224            BytesPending += written;
 0225        }
 226    }
 227}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.Literal.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7using System.Runtime.CompilerServices;
 8
 9namespace System.Text.Json
 10{
 11    public sealed partial class Utf8JsonWriter
 12    {
 13        /// <summary>
 14        /// Writes the pre-encoded property name and the JSON literal "null" as part of a name/value pair of a JSON obje
 15        /// </summary>
 16        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        public void WriteNull(JsonEncodedText propertyName)
 021        {
 022            WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.NullValue);
 023            _tokenType = JsonTokenType.Null;
 024        }
 25
 26        internal void WriteNullSection(ReadOnlySpan<byte> escapedPropertyNameSection)
 027        {
 028            if (_options.Indented)
 029            {
 030                ReadOnlySpan<byte> escapedName =
 031                    escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3);
 32
 033                WriteLiteralHelper(escapedName, JsonConstants.NullValue);
 034                _tokenType = JsonTokenType.Null;
 035            }
 36            else
 037            {
 038                Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxUnescapedTokenSize - 3);
 39
 040                ReadOnlySpan<byte> span = JsonConstants.NullValue;
 41
 042                WriteLiteralSection(escapedPropertyNameSection, span);
 43
 044                SetFlagToAddListSeparatorBeforeNextItem();
 045                _tokenType = JsonTokenType.Null;
 046            }
 047        }
 48
 49        private void WriteLiteralHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
 050        {
 051            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 52
 053            WriteLiteralByOptions(utf8PropertyName, value);
 54
 055            SetFlagToAddListSeparatorBeforeNextItem();
 056        }
 57
 58        /// <summary>
 59        /// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
 60        /// </summary>
 61        /// <param name="propertyName">The name of the property to write.</param>
 62        /// <exception cref="ArgumentException">
 63        /// Thrown when the specified property name is too large.
 64        /// </exception>
 65        /// <exception cref="ArgumentNullException">
 66        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 67        /// </exception>
 68        /// <exception cref="InvalidOperationException">
 69        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 70        /// </exception>
 71        /// <remarks>
 72        /// The property name is escaped before writing.
 73        /// </remarks>
 74        public void WriteNull(string propertyName)
 075        {
 076            ArgumentNullException.ThrowIfNull(propertyName);
 077            WriteNull(propertyName.AsSpan());
 078        }
 79
 80        /// <summary>
 81        /// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
 82        /// </summary>
 83        /// <param name="propertyName">The name of the property to write.</param>
 84        /// <exception cref="ArgumentException">
 85        /// Thrown when the specified property name is too large.
 86        /// </exception>
 87        /// <exception cref="InvalidOperationException">
 88        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 89        /// </exception>
 90        /// <remarks>
 91        /// The property name is escaped before writing.
 92        /// </remarks>
 93        public void WriteNull(ReadOnlySpan<char> propertyName)
 094        {
 095            JsonWriterHelper.ValidateProperty(propertyName);
 96
 097            ReadOnlySpan<byte> span = JsonConstants.NullValue;
 98
 099            WriteLiteralEscape(propertyName, span);
 100
 0101            SetFlagToAddListSeparatorBeforeNextItem();
 0102            _tokenType = JsonTokenType.Null;
 0103        }
 104
 105        /// <summary>
 106        /// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
 107        /// </summary>
 108        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 109        /// <exception cref="ArgumentException">
 110        /// Thrown when the specified property name is too large.
 111        /// </exception>
 112        /// <exception cref="InvalidOperationException">
 113        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 114        /// </exception>
 115        /// <remarks>
 116        /// The property name is escaped before writing.
 117        /// </remarks>
 118        public void WriteNull(ReadOnlySpan<byte> utf8PropertyName)
 0119        {
 0120            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 121
 0122            ReadOnlySpan<byte> span = JsonConstants.NullValue;
 123
 0124            WriteLiteralEscape(utf8PropertyName, span);
 125
 0126            SetFlagToAddListSeparatorBeforeNextItem();
 0127            _tokenType = JsonTokenType.Null;
 0128        }
 129
 130        /// <summary>
 131        /// Writes the pre-encoded property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as p
 132        /// </summary>
 133        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 134        /// <param name="value">The value to write.</param>
 135        /// <exception cref="InvalidOperationException">
 136        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 137        /// </exception>
 138        public void WriteBoolean(JsonEncodedText propertyName, bool value)
 0139        {
 0140            if (value)
 0141            {
 0142                WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.TrueValue);
 0143                _tokenType = JsonTokenType.True;
 0144            }
 145            else
 0146            {
 0147                WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.FalseValue);
 0148                _tokenType = JsonTokenType.False;
 0149            }
 0150        }
 151
 152        /// <summary>
 153        /// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a nam
 154        /// </summary>
 155        /// <param name="propertyName">The name of the property to write.</param>
 156        /// <param name="value">The value to write.</param>
 157        /// <exception cref="ArgumentException">
 158        /// Thrown when the specified property name is too large.
 159        /// </exception>
 160        /// <exception cref="ArgumentNullException">
 161        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 162        /// </exception>
 163        /// <exception cref="InvalidOperationException">
 164        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 165        /// </exception>
 166        /// <remarks>
 167        /// The property name is escaped before writing.
 168        /// </remarks>
 169        public void WriteBoolean(string propertyName, bool value)
 0170        {
 0171            ArgumentNullException.ThrowIfNull(propertyName);
 0172            WriteBoolean(propertyName.AsSpan(), value);
 0173        }
 174
 175        /// <summary>
 176        /// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a nam
 177        /// </summary>
 178        /// <param name="propertyName">The name of the property to write.</param>
 179        /// <param name="value">The value to write.</param>
 180        /// <exception cref="ArgumentException">
 181        /// Thrown when the specified property name is too large.
 182        /// </exception>
 183        /// <exception cref="InvalidOperationException">
 184        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 185        /// </exception>
 186        /// <remarks>
 187        /// The property name is escaped before writing.
 188        /// </remarks>
 189        public void WriteBoolean(ReadOnlySpan<char> propertyName, bool value)
 0190        {
 0191            JsonWriterHelper.ValidateProperty(propertyName);
 192
 0193            ReadOnlySpan<byte> span = value ? JsonConstants.TrueValue : JsonConstants.FalseValue;
 194
 0195            WriteLiteralEscape(propertyName, span);
 196
 0197            SetFlagToAddListSeparatorBeforeNextItem();
 0198            _tokenType = value ? JsonTokenType.True : JsonTokenType.False;
 0199        }
 200
 201        /// <summary>
 202        /// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a nam
 203        /// </summary>
 204        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 205        /// <param name="value">The value to write.</param>
 206        /// <exception cref="ArgumentException">
 207        /// Thrown when the specified property name is too large.
 208        /// </exception>
 209        /// <exception cref="InvalidOperationException">
 210        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 211        /// </exception>
 212        /// <remarks>
 213        /// The property name is escaped before writing.
 214        /// </remarks>
 215        public void WriteBoolean(ReadOnlySpan<byte> utf8PropertyName, bool value)
 0216        {
 0217            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 218
 0219            ReadOnlySpan<byte> span = value ? JsonConstants.TrueValue : JsonConstants.FalseValue;
 220
 0221            WriteLiteralEscape(utf8PropertyName, span);
 222
 0223            SetFlagToAddListSeparatorBeforeNextItem();
 0224            _tokenType = value ? JsonTokenType.True : JsonTokenType.False;
 0225        }
 226
 227        private void WriteLiteralEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
 0228        {
 0229            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 230
 0231            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 232
 0233            if (propertyIdx != -1)
 0234            {
 0235                WriteLiteralEscapeProperty(propertyName, value, propertyIdx);
 0236            }
 237            else
 0238            {
 0239                WriteLiteralByOptions(propertyName, value);
 0240            }
 0241        }
 242
 243        private void WriteLiteralEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
 0244        {
 0245            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 246
 0247            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 248
 0249            if (propertyIdx != -1)
 0250            {
 0251                WriteLiteralEscapeProperty(utf8PropertyName, value, propertyIdx);
 0252            }
 253            else
 0254            {
 0255                WriteLiteralByOptions(utf8PropertyName, value);
 0256            }
 0257        }
 258
 259        private void WriteLiteralEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value, int firstEsca
 0260        {
 0261            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0262            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 263
 0264            char[]? propertyArray = null;
 265
 0266            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 267
 0268            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0269                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0270                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 271
 0272            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 273
 0274            WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
 275
 0276            if (propertyArray != null)
 0277            {
 0278                ArrayPool<char>.Shared.Return(propertyArray);
 0279            }
 0280        }
 281
 282        private void WriteLiteralEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value, int first
 0283        {
 0284            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0285            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 286
 0287            byte[]? propertyArray = null;
 288
 0289            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 290
 0291            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0292                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0293                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 294
 0295            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 296
 0297            WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
 298
 0299            if (propertyArray != null)
 0300            {
 0301                ArrayPool<byte>.Shared.Return(propertyArray);
 0302            }
 0303        }
 304
 305        private void WriteLiteralByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
 0306        {
 0307            ValidateWritingProperty();
 0308            if (_options.Indented)
 0309            {
 0310                WriteLiteralIndented(propertyName, value);
 0311            }
 312            else
 0313            {
 0314                WriteLiteralMinimized(propertyName, value);
 0315            }
 0316        }
 317
 318        private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
 0319        {
 0320            ValidateWritingProperty();
 0321            if (_options.Indented)
 0322            {
 0323                WriteLiteralIndented(utf8PropertyName, value);
 0324            }
 325            else
 0326            {
 0327                WriteLiteralMinimized(utf8PropertyName, value);
 0328            }
 0329        }
 330
 331        private void WriteLiteralMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> value)
 0332        {
 0333            Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
 0334            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 335
 336            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + value.Length + 3
 337            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0338            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + value.Le
 339
 0340            if (_memory.Length - BytesPending < maxRequired)
 0341            {
 0342                Grow(maxRequired);
 0343            }
 344
 0345            Span<byte> output = _memory.Span;
 346
 0347            if (_currentDepth < 0)
 0348            {
 0349                output[BytesPending++] = JsonConstants.ListSeparator;
 0350            }
 0351            output[BytesPending++] = JsonConstants.Quote;
 352
 0353            TranscodeAndWrite(escapedPropertyName, output);
 354
 0355            output[BytesPending++] = JsonConstants.Quote;
 0356            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 357
 0358            value.CopyTo(output.Slice(BytesPending));
 0359            BytesPending += value.Length;
 0360        }
 361
 362        private void WriteLiteralMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value)
 0363        {
 0364            Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
 0365            Debug.Assert(escapedPropertyName.Length < int.MaxValue - value.Length - 4);
 366
 0367            int minRequired = escapedPropertyName.Length + value.Length + 3; // 2 quotes for property name, and 1 colon
 0368            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 369
 0370            if (_memory.Length - BytesPending < maxRequired)
 0371            {
 0372                Grow(maxRequired);
 0373            }
 374
 0375            Span<byte> output = _memory.Span;
 376
 0377            if (_currentDepth < 0)
 0378            {
 0379                output[BytesPending++] = JsonConstants.ListSeparator;
 0380            }
 0381            output[BytesPending++] = JsonConstants.Quote;
 382
 0383            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0384            BytesPending += escapedPropertyName.Length;
 385
 0386            output[BytesPending++] = JsonConstants.Quote;
 0387            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 388
 0389            value.CopyTo(output.Slice(BytesPending));
 0390            BytesPending += value.Length;
 0391        }
 392
 393        // AggressiveInlining used since this is only called from one location.
 394        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 395        private void WriteLiteralSection(ReadOnlySpan<byte> escapedPropertyNameSection, ReadOnlySpan<byte> value)
 0396        {
 0397            Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
 0398            Debug.Assert(escapedPropertyNameSection.Length < int.MaxValue - value.Length - 1);
 399
 0400            int minRequired = escapedPropertyNameSection.Length + value.Length;
 0401            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 402
 0403            if (_memory.Length - BytesPending < maxRequired)
 0404            {
 0405                Grow(maxRequired);
 0406            }
 407
 0408            Span<byte> output = _memory.Span;
 0409            if (_currentDepth < 0)
 0410            {
 0411                output[BytesPending++] = JsonConstants.ListSeparator;
 0412            }
 413
 0414            escapedPropertyNameSection.CopyTo(output.Slice(BytesPending));
 0415            BytesPending += escapedPropertyNameSection.Length;
 416
 0417            value.CopyTo(output.Slice(BytesPending));
 0418            BytesPending += value.Length;
 0419        }
 420
 421        private void WriteLiteralIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> value)
 0422        {
 0423            int indent = Indentation;
 0424            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 425
 0426            Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
 0427            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 428
 429            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + value.Length 
 430            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0431            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 432
 0433            if (_memory.Length - BytesPending < maxRequired)
 0434            {
 0435                Grow(maxRequired);
 0436            }
 437
 0438            Span<byte> output = _memory.Span;
 439
 0440            if (_currentDepth < 0)
 0441            {
 0442                output[BytesPending++] = JsonConstants.ListSeparator;
 0443            }
 444
 0445            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 446
 0447            if (_tokenType != JsonTokenType.None)
 0448            {
 0449                WriteNewLine(output);
 0450            }
 451
 0452            WriteIndentation(output.Slice(BytesPending), indent);
 0453            BytesPending += indent;
 454
 0455            output[BytesPending++] = JsonConstants.Quote;
 456
 0457            TranscodeAndWrite(escapedPropertyName, output);
 458
 0459            output[BytesPending++] = JsonConstants.Quote;
 0460            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0461            output[BytesPending++] = JsonConstants.Space;
 462
 0463            value.CopyTo(output.Slice(BytesPending));
 0464            BytesPending += value.Length;
 0465        }
 466
 467        private void WriteLiteralIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value)
 0468        {
 0469            int indent = Indentation;
 0470            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 471
 0472            Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
 0473            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - value.Length - 5 - _newLineLength);
 474
 0475            int minRequired = indent + escapedPropertyName.Length + value.Length + 4; // 2 quotes for property name, 1 c
 0476            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 477
 0478            if (_memory.Length - BytesPending < maxRequired)
 0479            {
 0480                Grow(maxRequired);
 0481            }
 482
 0483            Span<byte> output = _memory.Span;
 484
 0485            if (_currentDepth < 0)
 0486            {
 0487                output[BytesPending++] = JsonConstants.ListSeparator;
 0488            }
 489
 0490            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 491
 0492            if (_tokenType != JsonTokenType.None)
 0493            {
 0494                WriteNewLine(output);
 0495            }
 496
 0497            WriteIndentation(output.Slice(BytesPending), indent);
 0498            BytesPending += indent;
 499
 0500            output[BytesPending++] = JsonConstants.Quote;
 501
 0502            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0503            BytesPending += escapedPropertyName.Length;
 504
 0505            output[BytesPending++] = JsonConstants.Quote;
 0506            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0507            output[BytesPending++] = JsonConstants.Space;
 508
 0509            value.CopyTo(output.Slice(BytesPending));
 0510            BytesPending += value.Length;
 0511        }
 512
 513        internal void WritePropertyName(bool value)
 0514        {
 0515            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatBooleanLength];
 516
 0517            bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
 0518            Debug.Assert(result);
 519
 0520            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0521        }
 522    }
 523}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.SignedNumber.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="long"/> value (as a JSON number) as part of a name/value
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="long"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 22        /// </remarks>
 23        public void WriteNumber(JsonEncodedText propertyName, long value)
 024        {
 025            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 026            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 27
 028            WriteNumberByOptions(utf8PropertyName, value);
 29
 030            SetFlagToAddListSeparatorBeforeNextItem();
 031            _tokenType = JsonTokenType.Number;
 032        }
 33
 34        /// <summary>
 35        /// Writes the property name and <see cref="long"/> value (as a JSON number) as part of a name/value pair of a J
 36        /// </summary>
 37        /// <param name="propertyName">The name of the property to write.</param>
 38        /// <param name="value">The value to write.</param>
 39        /// <exception cref="ArgumentException">
 40        /// Thrown when the specified property name is too large.
 41        /// </exception>
 42        /// <exception cref="ArgumentNullException">
 43        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 44        /// </exception>
 45        /// <exception cref="InvalidOperationException">
 46        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 47        /// </exception>
 48        /// <remarks>
 49        /// Writes the <see cref="long"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 50        /// The property name is escaped before writing.
 51        /// </remarks>
 52        public void WriteNumber(string propertyName, long value)
 053        {
 054            ArgumentNullException.ThrowIfNull(propertyName);
 055            WriteNumber(propertyName.AsSpan(), value);
 056        }
 57
 58        /// <summary>
 59        /// Writes the property name and <see cref="long"/> value (as a JSON number) as part of a name/value pair of a J
 60        /// </summary>
 61        /// <param name="propertyName">The name of the property to write.</param>
 62        /// <param name="value">The value to write.</param>
 63        /// <exception cref="ArgumentException">
 64        /// Thrown when the specified property name is too large.
 65        /// </exception>
 66        /// <exception cref="InvalidOperationException">
 67        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 68        /// </exception>
 69        /// <remarks>
 70        /// Writes the <see cref="long"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 71        /// The property name is escaped before writing.
 72        /// </remarks>
 73        public void WriteNumber(ReadOnlySpan<char> propertyName, long value)
 074        {
 075            JsonWriterHelper.ValidateProperty(propertyName);
 76
 077            WriteNumberEscape(propertyName, value);
 78
 079            SetFlagToAddListSeparatorBeforeNextItem();
 080            _tokenType = JsonTokenType.Number;
 081        }
 82
 83        /// <summary>
 84        /// Writes the property name and <see cref="long"/> value (as a JSON number) as part of a name/value pair of a J
 85        /// </summary>
 86        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 87        /// <param name="value">The value to write.</param>
 88        /// <exception cref="ArgumentException">
 89        /// Thrown when the specified property name is too large.
 90        /// </exception>
 91        /// <exception cref="InvalidOperationException">
 92        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 93        /// </exception>
 94        /// <remarks>
 95        /// Writes the <see cref="long"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 96        /// The property name is escaped before writing.
 97        /// </remarks>
 98        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, long value)
 099        {
 0100            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 101
 0102            WriteNumberEscape(utf8PropertyName, value);
 103
 0104            SetFlagToAddListSeparatorBeforeNextItem();
 0105            _tokenType = JsonTokenType.Number;
 0106        }
 107
 108        /// <summary>
 109        /// Writes the pre-encoded property name and <see cref="int"/> value (as a JSON number) as part of a name/value 
 110        /// </summary>
 111        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 112        /// <param name="value">The value to write.</param>
 113        /// <exception cref="InvalidOperationException">
 114        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 115        /// </exception>
 116        /// <remarks>
 117        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 118        /// </remarks>
 119        public void WriteNumber(JsonEncodedText propertyName, int value)
 0120            => WriteNumber(propertyName, (long)value);
 121
 122        /// <summary>
 123        /// Writes the property name and <see cref="int"/> value (as a JSON number) as part of a name/value pair of a JS
 124        /// </summary>
 125        /// <param name="propertyName">The name of the property to write.</param>
 126        /// <param name="value">The value to write.</param>
 127        /// <exception cref="ArgumentException">
 128        /// Thrown when the specified property name is too large.
 129        /// </exception>
 130        /// <exception cref="ArgumentNullException">
 131        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 132        /// </exception>
 133        /// <exception cref="InvalidOperationException">
 134        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 135        /// </exception>
 136        /// <remarks>
 137        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 138        /// The property name is escaped before writing.
 139        /// </remarks>
 140        public void WriteNumber(string propertyName, int value)
 0141        {
 0142            ArgumentNullException.ThrowIfNull(propertyName);
 0143            WriteNumber(propertyName.AsSpan(), (long)value);
 0144        }
 145
 146        /// <summary>
 147        /// Writes the property name and <see cref="int"/> value (as a JSON number) as part of a name/value pair of a JS
 148        /// </summary>
 149        /// <param name="propertyName">The name of the property to write.</param>
 150        /// <param name="value">The value to write.</param>
 151        /// <exception cref="ArgumentException">
 152        /// Thrown when the specified property name is too large.
 153        /// </exception>
 154        /// <exception cref="InvalidOperationException">
 155        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 156        /// </exception>
 157        /// <remarks>
 158        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 159        /// </remarks>
 160        public void WriteNumber(ReadOnlySpan<char> propertyName, int value)
 0161            => WriteNumber(propertyName, (long)value);
 162
 163        /// <summary>
 164        /// Writes the property name and <see cref="int"/> value (as a JSON number) as part of a name/value pair of a JS
 165        /// </summary>
 166        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 167        /// <param name="value">The value to write.</param>
 168        /// <exception cref="ArgumentException">
 169        /// Thrown when the specified property name is too large.
 170        /// </exception>
 171        /// <exception cref="InvalidOperationException">
 172        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 173        /// </exception>
 174        /// <remarks>
 175        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 176        /// The property name is escaped before writing.
 177        /// </remarks>
 178        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, int value)
 0179            => WriteNumber(utf8PropertyName, (long)value);
 180
 181        private void WriteNumberEscape(ReadOnlySpan<char> propertyName, long value)
 0182        {
 0183            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 184
 0185            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 186
 0187            if (propertyIdx != -1)
 0188            {
 0189                WriteNumberEscapeProperty(propertyName, value, propertyIdx);
 0190            }
 191            else
 0192            {
 0193                WriteNumberByOptions(propertyName, value);
 0194            }
 0195        }
 196
 197        private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, long value)
 0198        {
 0199            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 200
 0201            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 202
 0203            if (propertyIdx != -1)
 0204            {
 0205                WriteNumberEscapeProperty(utf8PropertyName, value, propertyIdx);
 0206            }
 207            else
 0208            {
 0209                WriteNumberByOptions(utf8PropertyName, value);
 0210            }
 0211        }
 212
 213        private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, long value, int firstEscapeIndexProp)
 0214        {
 0215            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0216            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 217
 0218            char[]? propertyArray = null;
 219
 0220            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 221
 0222            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0223                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0224                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 225
 0226            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 227
 0228            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 229
 0230            if (propertyArray != null)
 0231            {
 0232                ArrayPool<char>.Shared.Return(propertyArray);
 0233            }
 0234        }
 235
 236        private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, long value, int firstEscapeIndexProp
 0237        {
 0238            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0239            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 240
 0241            byte[]? propertyArray = null;
 242
 0243            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 244
 0245            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0246                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0247                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 248
 0249            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 250
 0251            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 252
 0253            if (propertyArray != null)
 0254            {
 0255                ArrayPool<byte>.Shared.Return(propertyArray);
 0256            }
 0257        }
 258
 259        private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, long value)
 0260        {
 0261            ValidateWritingProperty();
 0262            if (_options.Indented)
 0263            {
 0264                WriteNumberIndented(propertyName, value);
 0265            }
 266            else
 0267            {
 0268                WriteNumberMinimized(propertyName, value);
 0269            }
 0270        }
 271
 272        private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, long value)
 0273        {
 0274            ValidateWritingProperty();
 0275            if (_options.Indented)
 0276            {
 0277                WriteNumberIndented(utf8PropertyName, value);
 0278            }
 279            else
 0280            {
 0281                WriteNumberMinimized(utf8PropertyName, value);
 0282            }
 0283        }
 284
 285        private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, long value)
 0286        {
 0287            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 288
 289            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + JsonConstants.MaximumF
 290            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0291            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 292
 0293            if (_memory.Length - BytesPending < maxRequired)
 0294            {
 0295                Grow(maxRequired);
 0296            }
 297
 0298            Span<byte> output = _memory.Span;
 299
 0300            if (_currentDepth < 0)
 0301            {
 0302                output[BytesPending++] = JsonConstants.ListSeparator;
 0303            }
 0304            output[BytesPending++] = JsonConstants.Quote;
 305
 0306            TranscodeAndWrite(escapedPropertyName, output);
 307
 0308            output[BytesPending++] = JsonConstants.Quote;
 0309            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 310
 0311            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0312            Debug.Assert(result);
 0313            BytesPending += bytesWritten;
 0314        }
 315
 316        private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, long value)
 0317        {
 0318            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatInt64Length - 4);
 319
 0320            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatInt64Length + 3; // 2 quotes for p
 0321            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 322
 0323            if (_memory.Length - BytesPending < maxRequired)
 0324            {
 0325                Grow(maxRequired);
 0326            }
 327
 0328            Span<byte> output = _memory.Span;
 329
 0330            if (_currentDepth < 0)
 0331            {
 0332                output[BytesPending++] = JsonConstants.ListSeparator;
 0333            }
 0334            output[BytesPending++] = JsonConstants.Quote;
 335
 0336            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0337            BytesPending += escapedPropertyName.Length;
 338
 0339            output[BytesPending++] = JsonConstants.Quote;
 0340            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 341
 0342            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0343            Debug.Assert(result);
 0344            BytesPending += bytesWritten;
 0345        }
 346
 347        private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, long value)
 0348        {
 0349            int indent = Indentation;
 0350            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 351
 0352            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 353
 354            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants
 355            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0356            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 357
 0358            if (_memory.Length - BytesPending < maxRequired)
 0359            {
 0360                Grow(maxRequired);
 0361            }
 362
 0363            Span<byte> output = _memory.Span;
 364
 0365            if (_currentDepth < 0)
 0366            {
 0367                output[BytesPending++] = JsonConstants.ListSeparator;
 0368            }
 369
 0370            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 371
 0372            if (_tokenType != JsonTokenType.None)
 0373            {
 0374                WriteNewLine(output);
 0375            }
 376
 0377            WriteIndentation(output.Slice(BytesPending), indent);
 0378            BytesPending += indent;
 379
 0380            output[BytesPending++] = JsonConstants.Quote;
 381
 0382            TranscodeAndWrite(escapedPropertyName, output);
 383
 0384            output[BytesPending++] = JsonConstants.Quote;
 0385            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0386            output[BytesPending++] = JsonConstants.Space;
 387
 0388            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0389            Debug.Assert(result);
 0390            BytesPending += bytesWritten;
 0391        }
 392
 393        private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, long value)
 0394        {
 0395            int indent = Indentation;
 0396            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 397
 0398            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatInt64Length - 5
 399
 0400            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatInt64Length + 4; // 2 quo
 0401            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 402
 0403            if (_memory.Length - BytesPending < maxRequired)
 0404            {
 0405                Grow(maxRequired);
 0406            }
 407
 0408            Span<byte> output = _memory.Span;
 409
 0410            if (_currentDepth < 0)
 0411            {
 0412                output[BytesPending++] = JsonConstants.ListSeparator;
 0413            }
 414
 0415            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 416
 0417            if (_tokenType != JsonTokenType.None)
 0418            {
 0419                WriteNewLine(output);
 0420            }
 421
 0422            WriteIndentation(output.Slice(BytesPending), indent);
 0423            BytesPending += indent;
 424
 0425            output[BytesPending++] = JsonConstants.Quote;
 426
 0427            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0428            BytesPending += escapedPropertyName.Length;
 429
 0430            output[BytesPending++] = JsonConstants.Quote;
 0431            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0432            output[BytesPending++] = JsonConstants.Space;
 433
 0434            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0435            Debug.Assert(result);
 0436            BytesPending += bytesWritten;
 0437        }
 438
 439        internal void WritePropertyName(int value)
 0440            => WritePropertyName((long)value);
 441
 442        internal void WritePropertyName(long value)
 0443        {
 0444            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatInt64Length];
 445
 0446            bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
 0447            Debug.Assert(result);
 448
 0449            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0450        }
 451    }
 452}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.String.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Runtime.CompilerServices;
 7
 8namespace 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)
 020            => WritePropertyNameHelper(propertyName.EncodedUtf8Bytes);
 21
 22        internal void WritePropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection)
 023        {
 024            if (_options.Indented)
 025            {
 026                ReadOnlySpan<byte> escapedPropertyName =
 027                    escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3);
 28
 029                WritePropertyNameHelper(escapedPropertyName);
 030            }
 31            else
 032            {
 033                Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxUnescapedTokenSize - 3);
 34
 035                WriteStringPropertyNameSection(escapedPropertyNameSection);
 36
 037                _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 038                _tokenType = JsonTokenType.PropertyName;
 039                _commentAfterNoneOrPropertyName = false;
 040            }
 041        }
 42
 43        private void WritePropertyNameHelper(ReadOnlySpan<byte> utf8PropertyName)
 044        {
 045            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 46
 047            WriteStringByOptionsPropertyName(utf8PropertyName);
 48
 049            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 050            _tokenType = JsonTokenType.PropertyName;
 051            _commentAfterNoneOrPropertyName = false;
 052        }
 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)
 071        {
 072            ArgumentNullException.ThrowIfNull(propertyName);
 073            WritePropertyName(propertyName.AsSpan());
 074        }
 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)
 090        {
 091            JsonWriterHelper.ValidateProperty(propertyName);
 92
 093            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 94
 095            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
 96
 097            if (propertyIdx != -1)
 098            {
 099                WriteStringEscapeProperty(propertyName, propertyIdx);
 0100            }
 101            else
 0102            {
 0103                WriteStringByOptionsPropertyName(propertyName);
 0104            }
 0105            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0106            _tokenType = JsonTokenType.PropertyName;
 0107            _commentAfterNoneOrPropertyName = false;
 0108        }
 109
 110        private void WriteStringEscapeProperty(scoped ReadOnlySpan<char> propertyName, int firstEscapeIndexProp)
 0111        {
 0112            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 113
 0114            char[]? propertyArray = null;
 115            scoped Span<char> escapedPropertyName;
 116
 0117            if (firstEscapeIndexProp != -1)
 0118            {
 0119                int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 120
 0121                if (length > JsonConstants.StackallocCharThreshold)
 0122                {
 0123                    propertyArray = ArrayPool<char>.Shared.Rent(length);
 0124                    escapedPropertyName = propertyArray;
 0125                }
 126                else
 0127                {
 0128                    escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold];
 0129                }
 130
 0131                JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 0132                propertyName = escapedPropertyName.Slice(0, written);
 0133            }
 134
 0135            WriteStringByOptionsPropertyName(propertyName);
 136
 0137            if (propertyArray != null)
 0138            {
 0139                ArrayPool<char>.Shared.Return(propertyArray);
 0140            }
 0141        }
 142
 143        private void WriteStringByOptionsPropertyName(ReadOnlySpan<char> propertyName)
 0144        {
 0145            ValidateWritingProperty();
 0146            if (_options.Indented)
 0147            {
 0148                WriteStringIndentedPropertyName(propertyName);
 0149            }
 150            else
 0151            {
 0152                WriteStringMinimizedPropertyName(propertyName);
 0153            }
 0154        }
 155
 156        private void WriteStringMinimizedPropertyName(ReadOnlySpan<char> escapedPropertyName)
 0157        {
 0158            Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
 0159            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
 0163            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 4;
 164
 0165            if (_memory.Length - BytesPending < maxRequired)
 0166            {
 0167                Grow(maxRequired);
 0168            }
 169
 0170            Span<byte> output = _memory.Span;
 171
 0172            if (_currentDepth < 0)
 0173            {
 0174                output[BytesPending++] = JsonConstants.ListSeparator;
 0175            }
 0176            output[BytesPending++] = JsonConstants.Quote;
 177
 0178            TranscodeAndWrite(escapedPropertyName, output);
 179
 0180            output[BytesPending++] = JsonConstants.Quote;
 0181            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0182        }
 183
 184        private void WriteStringIndentedPropertyName(ReadOnlySpan<char> escapedPropertyName)
 0185        {
 0186            int indent = Indentation;
 0187            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 188
 0189            Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
 0190            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
 0194            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 195
 0196            if (_memory.Length - BytesPending < maxRequired)
 0197            {
 0198                Grow(maxRequired);
 0199            }
 200
 0201            Span<byte> output = _memory.Span;
 202
 0203            if (_currentDepth < 0)
 0204            {
 0205                output[BytesPending++] = JsonConstants.ListSeparator;
 0206            }
 207
 0208            if (_tokenType != JsonTokenType.None)
 0209            {
 0210                WriteNewLine(output);
 0211            }
 212
 0213            WriteIndentation(output.Slice(BytesPending), indent);
 0214            BytesPending += indent;
 215
 0216            output[BytesPending++] = JsonConstants.Quote;
 217
 0218            TranscodeAndWrite(escapedPropertyName, output);
 219
 0220            output[BytesPending++] = JsonConstants.Quote;
 0221            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0222            output[BytesPending++] = JsonConstants.Space;
 0223        }
 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)
 0239        {
 0240            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 241
 0242            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 243
 0244            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
 245
 0246            if (propertyIdx != -1)
 0247            {
 0248                WriteStringEscapeProperty(utf8PropertyName, propertyIdx);
 0249            }
 250            else
 0251            {
 0252                WriteStringByOptionsPropertyName(utf8PropertyName);
 0253            }
 0254            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0255            _tokenType = JsonTokenType.PropertyName;
 0256            _commentAfterNoneOrPropertyName = false;
 0257        }
 258
 259        private void WritePropertyNameUnescaped(ReadOnlySpan<byte> utf8PropertyName)
 0260        {
 0261            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 0262            WriteStringByOptionsPropertyName(utf8PropertyName);
 263
 0264            _currentDepth &= JsonConstants.RemoveFlagsBitMask;
 0265            _tokenType = JsonTokenType.PropertyName;
 0266            _commentAfterNoneOrPropertyName = false;
 0267        }
 268
 269        private void WriteStringEscapeProperty(scoped ReadOnlySpan<byte> utf8PropertyName, int firstEscapeIndexProp)
 0270        {
 0271            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 272
 0273            byte[]? propertyArray = null;
 274            scoped Span<byte> escapedPropertyName;
 275
 0276            if (firstEscapeIndexProp != -1)
 0277            {
 0278                int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 279
 0280                if (length > JsonConstants.StackallocByteThreshold)
 0281                {
 0282                    propertyArray = ArrayPool<byte>.Shared.Rent(length);
 0283                    escapedPropertyName = propertyArray;
 0284                }
 285                else
 0286                {
 0287                    escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold];
 0288                }
 289
 0290                JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco
 0291                utf8PropertyName = escapedPropertyName.Slice(0, written);
 0292            }
 293
 0294            WriteStringByOptionsPropertyName(utf8PropertyName);
 295
 0296            if (propertyArray != null)
 0297            {
 0298                ArrayPool<byte>.Shared.Return(propertyArray);
 0299            }
 0300        }
 301
 302        private void WriteStringByOptionsPropertyName(ReadOnlySpan<byte> utf8PropertyName)
 0303        {
 0304            ValidateWritingProperty();
 0305            if (_options.Indented)
 0306            {
 0307                WriteStringIndentedPropertyName(utf8PropertyName);
 0308            }
 309            else
 0310            {
 0311                WriteStringMinimizedPropertyName(utf8PropertyName);
 0312            }
 0313        }
 314
 315        // AggressiveInlining used since this is only called from one location.
 316        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 317        private void WriteStringMinimizedPropertyName(ReadOnlySpan<byte> escapedPropertyName)
 0318        {
 0319            Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
 0320            Debug.Assert(escapedPropertyName.Length < int.MaxValue - 4);
 321
 0322            int minRequired = escapedPropertyName.Length + 3; // 2 quotes for property name, and 1 colon
 0323            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 324
 0325            if (_memory.Length - BytesPending < maxRequired)
 0326            {
 0327                Grow(maxRequired);
 0328            }
 329
 0330            Span<byte> output = _memory.Span;
 331
 0332            if (_currentDepth < 0)
 0333            {
 0334                output[BytesPending++] = JsonConstants.ListSeparator;
 0335            }
 0336            output[BytesPending++] = JsonConstants.Quote;
 337
 0338            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0339            BytesPending += escapedPropertyName.Length;
 340
 0341            output[BytesPending++] = JsonConstants.Quote;
 0342            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0343        }
 344
 345        // AggressiveInlining used since this is only called from one location.
 346        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 347        private void WriteStringPropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection)
 0348        {
 0349            Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxEscapedTokenSize - 3);
 0350            Debug.Assert(escapedPropertyNameSection.Length < int.MaxValue - 4);
 351
 0352            int maxRequired = escapedPropertyNameSection.Length + 1; // Optionally, 1 list separator
 353
 0354            if (_memory.Length - BytesPending < maxRequired)
 0355            {
 0356                Grow(maxRequired);
 0357            }
 358
 0359            Span<byte> output = _memory.Span;
 360
 0361            if (_currentDepth < 0)
 0362            {
 0363                output[BytesPending++] = JsonConstants.ListSeparator;
 0364            }
 365
 0366            escapedPropertyNameSection.CopyTo(output.Slice(BytesPending));
 0367            BytesPending += escapedPropertyNameSection.Length;
 0368        }
 369
 370        // AggressiveInlining used since this is only called from one location.
 371        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 372        private void WriteStringIndentedPropertyName(ReadOnlySpan<byte> escapedPropertyName)
 0373        {
 0374            int indent = Indentation;
 0375            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 376
 0377            Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
 0378            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - 5 - _newLineLength);
 379
 0380            int minRequired = indent + escapedPropertyName.Length + 4; // 2 quotes for property name, 1 colon, and 1 spa
 0381            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 382
 0383            if (_memory.Length - BytesPending < maxRequired)
 0384            {
 0385                Grow(maxRequired);
 0386            }
 387
 0388            Span<byte> output = _memory.Span;
 389
 0390            if (_currentDepth < 0)
 0391            {
 0392                output[BytesPending++] = JsonConstants.ListSeparator;
 0393            }
 394
 0395            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 396
 0397            if (_tokenType != JsonTokenType.None)
 0398            {
 0399                WriteNewLine(output);
 0400            }
 401
 0402            WriteIndentation(output.Slice(BytesPending), indent);
 0403            BytesPending += indent;
 404
 0405            output[BytesPending++] = JsonConstants.Quote;
 406
 0407            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0408            BytesPending += escapedPropertyName.Length;
 409
 0410            output[BytesPending++] = JsonConstants.Quote;
 0411            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0412            output[BytesPending++] = JsonConstants.Space;
 0413        }
 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)
 0424            => WriteStringHelper(propertyName.EncodedUtf8Bytes, value.EncodedUtf8Bytes);
 425
 426        private void WriteStringHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
 0427        {
 0428            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize && utf8Value.Length <= JsonConst
 429
 0430            WriteStringByOptions(utf8PropertyName, utf8Value);
 431
 0432            SetFlagToAddListSeparatorBeforeNextItem();
 0433            _tokenType = JsonTokenType.String;
 0434        }
 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)
 0454        {
 0455            ArgumentNullException.ThrowIfNull(propertyName);
 0456            WriteString(propertyName.AsSpan(), value);
 0457        }
 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)
 0483        {
 0484            ArgumentNullException.ThrowIfNull(propertyName);
 485
 0486            if (value == null)
 0487            {
 0488                WriteNull(propertyName.AsSpan());
 0489            }
 490            else
 0491            {
 0492                WriteString(propertyName.AsSpan(), value.AsSpan());
 0493            }
 0494        }
 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)
 0511        {
 0512            JsonWriterHelper.ValidatePropertyAndValue(propertyName, value);
 513
 0514            WriteStringEscape(propertyName, value);
 515
 0516            SetFlagToAddListSeparatorBeforeNextItem();
 0517            _tokenType = JsonTokenType.String;
 0518        }
 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)
 0535        {
 0536            JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, utf8Value);
 537
 0538            WriteStringEscape(utf8PropertyName, utf8Value);
 539
 0540            SetFlagToAddListSeparatorBeforeNextItem();
 0541            _tokenType = JsonTokenType.String;
 0542        }
 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)
 0565        {
 0566            if (value == null)
 0567            {
 0568                WriteNull(propertyName);
 0569            }
 570            else
 0571            {
 0572                WriteString(propertyName, value.AsSpan());
 0573            }
 0574        }
 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)
 0591            => WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, value);
 592
 593        private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value)
 0594        {
 0595            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 596
 0597            JsonWriterHelper.ValidateValue(value);
 598
 0599            int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
 600
 0601            Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
 602
 0603            if (valueIdx != -1)
 0604            {
 0605                WriteStringEscapeValueOnly(utf8PropertyName, value, valueIdx);
 0606            }
 607            else
 0608            {
 0609                WriteStringByOptions(utf8PropertyName, value);
 0610            }
 611
 0612            SetFlagToAddListSeparatorBeforeNextItem();
 0613            _tokenType = JsonTokenType.String;
 0614        }
 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)
 0634        {
 0635            ArgumentNullException.ThrowIfNull(propertyName);
 0636            WriteString(propertyName.AsSpan(), value);
 0637        }
 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)
 0654        {
 0655            JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, value);
 656
 0657            WriteStringEscape(utf8PropertyName, value);
 658
 0659            SetFlagToAddListSeparatorBeforeNextItem();
 0660            _tokenType = JsonTokenType.String;
 0661        }
 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)
 0678            => WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, utf8Value);
 679
 680        private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
 0681        {
 0682            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 683
 0684            JsonWriterHelper.ValidateValue(utf8Value);
 685
 0686            int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
 687
 0688            Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
 689
 0690            if (valueIdx != -1)
 0691            {
 0692                WriteStringEscapeValueOnly(utf8PropertyName, utf8Value, valueIdx);
 0693            }
 694            else
 0695            {
 0696                WriteStringByOptions(utf8PropertyName, utf8Value);
 0697            }
 698
 0699            SetFlagToAddListSeparatorBeforeNextItem();
 0700            _tokenType = JsonTokenType.String;
 0701        }
 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)
 0721        {
 0722            ArgumentNullException.ThrowIfNull(propertyName);
 0723            WriteString(propertyName.AsSpan(), utf8Value);
 0724        }
 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)
 0741        {
 0742            JsonWriterHelper.ValidatePropertyAndValue(propertyName, utf8Value);
 743
 0744            WriteStringEscape(propertyName, utf8Value);
 745
 0746            SetFlagToAddListSeparatorBeforeNextItem();
 0747            _tokenType = JsonTokenType.String;
 0748        }
 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)
 0765            => WriteStringHelperEscapeProperty(propertyName, value.EncodedUtf8Bytes);
 766
 767        private void WriteStringHelperEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value)
 0768        {
 0769            Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize);
 770
 0771            JsonWriterHelper.ValidateProperty(propertyName);
 772
 0773            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 774
 0775            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
 776
 0777            if (propertyIdx != -1)
 0778            {
 0779                WriteStringEscapePropertyOnly(propertyName, utf8Value, propertyIdx);
 0780            }
 781            else
 0782            {
 0783                WriteStringByOptions(propertyName, utf8Value);
 0784            }
 785
 0786            SetFlagToAddListSeparatorBeforeNextItem();
 0787            _tokenType = JsonTokenType.String;
 0788        }
 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)
 0811        {
 0812            if (value == null)
 0813            {
 0814                WriteNull(propertyName);
 0815            }
 816            else
 0817            {
 0818                WriteString(propertyName, value.AsSpan());
 0819            }
 0820        }
 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)
 0837            => WriteStringHelperEscapeProperty(utf8PropertyName, value.EncodedUtf8Bytes);
 838
 839        private void WriteStringHelperEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
 0840        {
 0841            Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize);
 842
 0843            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 844
 0845            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 846
 0847            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
 848
 0849            if (propertyIdx != -1)
 0850            {
 0851                WriteStringEscapePropertyOnly(utf8PropertyName, utf8Value, propertyIdx);
 0852            }
 853            else
 0854            {
 0855                WriteStringByOptions(utf8PropertyName, utf8Value);
 0856            }
 857
 0858            SetFlagToAddListSeparatorBeforeNextItem();
 0859            _tokenType = JsonTokenType.String;
 0860        }
 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)
 0883        {
 0884            if (value == null)
 0885            {
 0886                WriteNull(utf8PropertyName);
 0887            }
 888            else
 0889            {
 0890                WriteString(utf8PropertyName, value.AsSpan());
 0891            }
 0892        }
 893
 894        private void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> utf8Value, in
 0895        {
 0896            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length);
 0897            Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < utf8Value.Length);
 898
 0899            byte[]? valueArray = null;
 900
 0901            int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndex);
 902
 0903            Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
 0904                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0905                (valueArray = ArrayPool<byte>.Shared.Rent(length));
 906
 0907            JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndex, _options.Encoder, out int written);
 908
 0909            WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written));
 910
 0911            if (valueArray != null)
 0912            {
 0913                ArrayPool<byte>.Shared.Return(valueArray);
 0914            }
 0915        }
 916
 917        private void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> value, int fi
 0918        {
 0919            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length);
 0920            Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < value.Length);
 921
 0922            char[]? valueArray = null;
 923
 0924            int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndex);
 925
 0926            Span<char> escapedValue = length <= JsonConstants.StackallocCharThreshold ?
 0927                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0928                (valueArray = ArrayPool<char>.Shared.Rent(length));
 929
 0930            JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, _options.Encoder, out int written);
 931
 0932            WriteStringByOptions(escapedPropertyName, escapedValue.Slice(0, written));
 933
 0934            if (valueArray != null)
 0935            {
 0936                ArrayPool<char>.Shared.Return(valueArray);
 0937            }
 0938        }
 939
 940        private void WriteStringEscapePropertyOnly(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> escapedValue, int
 0941        {
 0942            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0943            Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < propertyName.Length);
 944
 0945            char[]? propertyArray = null;
 946
 0947            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndex);
 948
 0949            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0950                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0951                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 952
 0953            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int
 954
 0955            WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue);
 956
 0957            if (propertyArray != null)
 0958            {
 0959                ArrayPool<char>.Shared.Return(propertyArray);
 0960            }
 0961        }
 962
 963        private void WriteStringEscapePropertyOnly(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> escapedValue,
 0964        {
 0965            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0966            Debug.Assert(firstEscapeIndex >= 0 && firstEscapeIndex < utf8PropertyName.Length);
 967
 0968            byte[]? propertyArray = null;
 969
 0970            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndex);
 971
 0972            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0973                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0974                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 975
 0976            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out
 977
 0978            WriteStringByOptions(escapedPropertyName.Slice(0, written), escapedValue);
 979
 0980            if (propertyArray != null)
 0981            {
 0982                ArrayPool<byte>.Shared.Return(propertyArray);
 0983            }
 0984        }
 985
 986        private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value)
 0987        {
 0988            int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
 0989            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 990
 0991            Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
 0992            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
 993
 994            // Equivalent to: valueIdx != -1 || propertyIdx != -1
 0995            if (valueIdx + propertyIdx != -2)
 0996            {
 0997                WriteStringEscapePropertyOrValue(propertyName, value, propertyIdx, valueIdx);
 0998            }
 999            else
 01000            {
 01001                WriteStringByOptions(propertyName, value);
 01002            }
 01003        }
 1004
 1005        private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
 01006        {
 01007            int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
 01008            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 1009
 01010            Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
 01011            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
 1012
 1013            // Equivalent to: valueIdx != -1 || propertyIdx != -1
 01014            if (valueIdx + propertyIdx != -2)
 01015            {
 01016                WriteStringEscapePropertyOrValue(utf8PropertyName, utf8Value, propertyIdx, valueIdx);
 01017            }
 1018            else
 01019            {
 01020                WriteStringByOptions(utf8PropertyName, utf8Value);
 01021            }
 01022        }
 1023
 1024        private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value)
 01025        {
 01026            int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
 01027            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 1028
 01029            Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length && valueIdx < int.MaxValue / 2);
 01030            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length && propertyIdx < int.MaxValue / 2);
 1031
 1032            // Equivalent to: valueIdx != -1 || propertyIdx != -1
 01033            if (valueIdx + propertyIdx != -2)
 01034            {
 01035                WriteStringEscapePropertyOrValue(propertyName, utf8Value, propertyIdx, valueIdx);
 01036            }
 1037            else
 01038            {
 01039                WriteStringByOptions(propertyName, utf8Value);
 01040            }
 01041        }
 1042
 1043        private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value)
 01044        {
 01045            int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
 01046            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 1047
 01048            Debug.Assert(valueIdx >= -1 && valueIdx < value.Length && valueIdx < int.MaxValue / 2);
 01049            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length && propertyIdx < int.MaxValue / 2);
 1050
 1051            // Equivalent to: valueIdx != -1 || propertyIdx != -1
 01052            if (valueIdx + propertyIdx != -2)
 01053            {
 01054                WriteStringEscapePropertyOrValue(utf8PropertyName, value, propertyIdx, valueIdx);
 01055            }
 1056            else
 01057            {
 01058                WriteStringByOptions(utf8PropertyName, value);
 01059            }
 01060        }
 1061
 1062        private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<char> propertyName, scoped ReadOnlySpan<char> 
 01063        {
 01064            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length);
 01065            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 1066
 01067            char[]? valueArray = null;
 01068            char[]? propertyArray = null;
 1069            scoped Span<char> escapedValue;
 1070
 01071            if (firstEscapeIndexVal != -1)
 01072            {
 01073                int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal);
 1074
 01075                if (length > JsonConstants.StackallocCharThreshold)
 01076                {
 01077                    valueArray = ArrayPool<char>.Shared.Rent(length);
 01078                    escapedValue = valueArray;
 01079                }
 1080                else
 01081                {
 01082                    escapedValue = stackalloc char[JsonConstants.StackallocCharThreshold];
 01083                }
 1084
 01085                JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int writte
 01086                value = escapedValue.Slice(0, written);
 01087            }
 1088
 1089            scoped Span<char> escapedPropertyName;
 1090
 01091            if (firstEscapeIndexProp != -1)
 01092            {
 01093                int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 1094
 01095                if (length > JsonConstants.StackallocCharThreshold)
 01096                {
 01097                    propertyArray = ArrayPool<char>.Shared.Rent(length);
 01098                    escapedPropertyName = propertyArray;
 01099                }
 1100                else
 01101                {
 01102                    escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold];
 01103                }
 1104
 01105                JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 01106                propertyName = escapedPropertyName.Slice(0, written);
 01107            }
 1108
 01109            WriteStringByOptions(propertyName, value);
 1110
 01111            if (valueArray != null)
 01112            {
 01113                ArrayPool<char>.Shared.Return(valueArray);
 01114            }
 1115
 01116            if (propertyArray != null)
 01117            {
 01118                ArrayPool<char>.Shared.Return(propertyArray);
 01119            }
 01120        }
 1121
 1122        private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<byte> utf8PropertyName, scoped ReadOnlySpan<by
 01123        {
 01124            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length);
 01125            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 1126
 01127            byte[]? valueArray = null;
 01128            byte[]? propertyArray = null;
 1129            scoped Span<byte> escapedValue;
 1130
 01131            if (firstEscapeIndexVal != -1)
 01132            {
 01133                int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
 1134
 01135                if (length > JsonConstants.StackallocByteThreshold)
 01136                {
 01137                    valueArray = ArrayPool<byte>.Shared.Rent(length);
 01138                    escapedValue = valueArray;
 01139                }
 1140                else
 01141                {
 01142                    escapedValue = stackalloc byte[JsonConstants.StackallocByteThreshold];
 01143                }
 1144
 01145                JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int wr
 01146                utf8Value = escapedValue.Slice(0, written);
 01147            }
 1148
 1149            scoped Span<byte> escapedPropertyName;
 1150
 01151            if (firstEscapeIndexProp != -1)
 01152            {
 01153                int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 1154
 01155                if (length > JsonConstants.StackallocByteThreshold)
 01156                {
 01157                    propertyArray = ArrayPool<byte>.Shared.Rent(length);
 01158                    escapedPropertyName = propertyArray;
 01159                }
 1160                else
 01161                {
 01162                    escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold];
 01163                }
 1164
 01165                JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco
 01166                utf8PropertyName = escapedPropertyName.Slice(0, written);
 01167            }
 1168
 01169            WriteStringByOptions(utf8PropertyName, utf8Value);
 1170
 01171            if (valueArray != null)
 01172            {
 01173                ArrayPool<byte>.Shared.Return(valueArray);
 01174            }
 1175
 01176            if (propertyArray != null)
 01177            {
 01178                ArrayPool<byte>.Shared.Return(propertyArray);
 01179            }
 01180        }
 1181
 1182        private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<char> propertyName, scoped ReadOnlySpan<byte> 
 01183        {
 01184            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length);
 01185            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 1186
 01187            byte[]? valueArray = null;
 01188            char[]? propertyArray = null;
 1189            scoped Span<byte> escapedValue;
 1190
 01191            if (firstEscapeIndexVal != -1)
 01192            {
 01193                int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
 1194
 01195                if (length > JsonConstants.StackallocByteThreshold)
 01196                {
 01197                    valueArray = ArrayPool<byte>.Shared.Rent(length);
 01198                    escapedValue = valueArray;
 01199                }
 1200                else
 01201                {
 01202                    escapedValue = stackalloc byte[JsonConstants.StackallocByteThreshold];
 01203                }
 1204
 01205                JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int wr
 01206                utf8Value = escapedValue.Slice(0, written);
 01207            }
 1208
 1209            scoped Span<char> escapedPropertyName;
 1210
 01211            if (firstEscapeIndexProp != -1)
 01212            {
 01213                int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 1214
 01215                if (length > JsonConstants.StackallocCharThreshold)
 01216                {
 01217                    propertyArray = ArrayPool<char>.Shared.Rent(length);
 01218                    escapedPropertyName = propertyArray;
 01219                }
 1220                else
 01221                {
 01222                    escapedPropertyName = stackalloc char[JsonConstants.StackallocCharThreshold];
 01223                }
 1224
 01225                JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 01226                propertyName = escapedPropertyName.Slice(0, written);
 01227            }
 1228
 01229            WriteStringByOptions(propertyName, utf8Value);
 1230
 01231            if (valueArray != null)
 01232            {
 01233                ArrayPool<byte>.Shared.Return(valueArray);
 01234            }
 1235
 01236            if (propertyArray != null)
 01237            {
 01238                ArrayPool<char>.Shared.Return(propertyArray);
 01239            }
 01240        }
 1241
 1242        private void WriteStringEscapePropertyOrValue(scoped ReadOnlySpan<byte> utf8PropertyName, scoped ReadOnlySpan<ch
 01243        {
 01244            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length);
 01245            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 1246
 01247            char[]? valueArray = null;
 01248            byte[]? propertyArray = null;
 1249            scoped Span<char> escapedValue;
 1250
 01251            if (firstEscapeIndexVal != -1)
 01252            {
 01253                int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal);
 1254
 01255                if (length > JsonConstants.StackallocCharThreshold)
 01256                {
 01257                    valueArray = ArrayPool<char>.Shared.Rent(length);
 01258                    escapedValue = valueArray;
 01259                }
 1260                else
 01261                {
 01262                    escapedValue = stackalloc char[JsonConstants.StackallocCharThreshold];
 01263                }
 1264
 01265                JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int writte
 01266                value = escapedValue.Slice(0, written);
 01267            }
 1268
 1269            scoped Span<byte> escapedPropertyName;
 1270
 01271            if (firstEscapeIndexProp != -1)
 01272            {
 01273                int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 1274
 01275                if (length > JsonConstants.StackallocByteThreshold)
 01276                {
 01277                    propertyArray = ArrayPool<byte>.Shared.Rent(length);
 01278                    escapedPropertyName = propertyArray;
 01279                }
 1280                else
 01281                {
 01282                    escapedPropertyName = stackalloc byte[JsonConstants.StackallocByteThreshold];
 01283                }
 1284
 01285                JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Enco
 01286                utf8PropertyName = escapedPropertyName.Slice(0, written);
 01287            }
 1288
 01289            WriteStringByOptions(utf8PropertyName, value);
 1290
 01291            if (valueArray != null)
 01292            {
 01293                ArrayPool<char>.Shared.Return(valueArray);
 01294            }
 1295
 01296            if (propertyArray != null)
 01297            {
 01298                ArrayPool<byte>.Shared.Return(propertyArray);
 01299            }
 01300        }
 1301
 1302        private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value)
 01303        {
 01304            ValidateWritingProperty();
 01305            if (_options.Indented)
 01306            {
 01307                WriteStringIndented(propertyName, value);
 01308            }
 1309            else
 01310            {
 01311                WriteStringMinimized(propertyName, value);
 01312            }
 01313        }
 1314
 1315        private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value)
 01316        {
 01317            ValidateWritingProperty();
 01318            if (_options.Indented)
 01319            {
 01320                WriteStringIndented(utf8PropertyName, utf8Value);
 01321            }
 1322            else
 01323            {
 01324                WriteStringMinimized(utf8PropertyName, utf8Value);
 01325            }
 01326        }
 1327
 1328        private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value)
 01329        {
 01330            ValidateWritingProperty();
 01331            if (_options.Indented)
 01332            {
 01333                WriteStringIndented(propertyName, utf8Value);
 01334            }
 1335            else
 01336            {
 01337                WriteStringMinimized(propertyName, utf8Value);
 01338            }
 01339        }
 1340
 1341        private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value)
 01342        {
 01343            ValidateWritingProperty();
 01344            if (_options.Indented)
 01345            {
 01346                WriteStringIndented(utf8PropertyName, value);
 01347            }
 1348            else
 01349            {
 01350                WriteStringMinimized(utf8PropertyName, value);
 01351            }
 01352        }
 1353
 1354        // TODO: https://github.com/dotnet/runtime/issues/29293
 1355        private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue)
 01356        {
 01357            Debug.Assert(escapedValue.Length <= JsonConstants.MaxUnescapedTokenSize);
 01358            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
 01362            int maxRequired = ((escapedPropertyName.Length + escapedValue.Length) * JsonConstants.MaxExpansionFactorWhil
 1363
 01364            if (_memory.Length - BytesPending < maxRequired)
 01365            {
 01366                Grow(maxRequired);
 01367            }
 1368
 01369            Span<byte> output = _memory.Span;
 1370
 01371            if (_currentDepth < 0)
 01372            {
 01373                output[BytesPending++] = JsonConstants.ListSeparator;
 01374            }
 01375            output[BytesPending++] = JsonConstants.Quote;
 1376
 01377            TranscodeAndWrite(escapedPropertyName, output);
 1378
 01379            output[BytesPending++] = JsonConstants.Quote;
 01380            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 1381
 01382            output[BytesPending++] = JsonConstants.Quote;
 1383
 01384            TranscodeAndWrite(escapedValue, output);
 1385
 01386            output[BytesPending++] = JsonConstants.Quote;
 01387        }
 1388
 1389        // TODO: https://github.com/dotnet/runtime/issues/29293
 1390        private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue)
 01391        {
 01392            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01393            Debug.Assert(escapedPropertyName.Length < int.MaxValue - escapedValue.Length - 6);
 1394
 01395            int minRequired = escapedPropertyName.Length + escapedValue.Length + 5; // 2 quotes for property name, 2 quo
 01396            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 1397
 01398            if (_memory.Length - BytesPending < maxRequired)
 01399            {
 01400                Grow(maxRequired);
 01401            }
 1402
 01403            Span<byte> output = _memory.Span;
 1404
 01405            if (_currentDepth < 0)
 01406            {
 01407                output[BytesPending++] = JsonConstants.ListSeparator;
 01408            }
 01409            output[BytesPending++] = JsonConstants.Quote;
 1410
 01411            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 01412            BytesPending += escapedPropertyName.Length;
 1413
 01414            output[BytesPending++] = JsonConstants.Quote;
 01415            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 1416
 01417            output[BytesPending++] = JsonConstants.Quote;
 1418
 01419            escapedValue.CopyTo(output.Slice(BytesPending));
 01420            BytesPending += escapedValue.Length;
 1421
 01422            output[BytesPending++] = JsonConstants.Quote;
 01423        }
 1424
 1425        // TODO: https://github.com/dotnet/runtime/issues/29293
 1426        private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue)
 01427        {
 01428            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01429            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
 01433            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escapedV
 1434
 01435            if (_memory.Length - BytesPending < maxRequired)
 01436            {
 01437                Grow(maxRequired);
 01438            }
 1439
 01440            Span<byte> output = _memory.Span;
 1441
 01442            if (_currentDepth < 0)
 01443            {
 01444                output[BytesPending++] = JsonConstants.ListSeparator;
 01445            }
 01446            output[BytesPending++] = JsonConstants.Quote;
 1447
 01448            TranscodeAndWrite(escapedPropertyName, output);
 1449
 01450            output[BytesPending++] = JsonConstants.Quote;
 01451            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 1452
 01453            output[BytesPending++] = JsonConstants.Quote;
 1454
 01455            escapedValue.CopyTo(output.Slice(BytesPending));
 01456            BytesPending += escapedValue.Length;
 1457
 01458            output[BytesPending++] = JsonConstants.Quote;
 01459        }
 1460
 1461        // TODO: https://github.com/dotnet/runtime/issues/29293
 1462        private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue)
 01463        {
 01464            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01465            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
 01469            int maxRequired = (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escapedProperty
 1470
 01471            if (_memory.Length - BytesPending < maxRequired)
 01472            {
 01473                Grow(maxRequired);
 01474            }
 1475
 01476            Span<byte> output = _memory.Span;
 1477
 01478            if (_currentDepth < 0)
 01479            {
 01480                output[BytesPending++] = JsonConstants.ListSeparator;
 01481            }
 01482            output[BytesPending++] = JsonConstants.Quote;
 1483
 01484            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 01485            BytesPending += escapedPropertyName.Length;
 1486
 01487            output[BytesPending++] = JsonConstants.Quote;
 01488            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 1489
 01490            output[BytesPending++] = JsonConstants.Quote;
 1491
 01492            TranscodeAndWrite(escapedValue, output);
 1493
 01494            output[BytesPending++] = JsonConstants.Quote;
 01495        }
 1496
 1497        // TODO: https://github.com/dotnet/runtime/issues/29293
 1498        private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue)
 01499        {
 01500            int indent = Indentation;
 01501            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 1502
 01503            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01504            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
 01508            int maxRequired = indent + ((escapedPropertyName.Length + escapedValue.Length) * JsonConstants.MaxExpansionF
 1509
 01510            if (_memory.Length - BytesPending < maxRequired)
 01511            {
 01512                Grow(maxRequired);
 01513            }
 1514
 01515            Span<byte> output = _memory.Span;
 1516
 01517            if (_currentDepth < 0)
 01518            {
 01519                output[BytesPending++] = JsonConstants.ListSeparator;
 01520            }
 1521
 01522            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 1523
 01524            if (_tokenType != JsonTokenType.None)
 01525            {
 01526                WriteNewLine(output);
 01527            }
 1528
 01529            WriteIndentation(output.Slice(BytesPending), indent);
 01530            BytesPending += indent;
 1531
 01532            output[BytesPending++] = JsonConstants.Quote;
 1533
 01534            TranscodeAndWrite(escapedPropertyName, output);
 1535
 01536            output[BytesPending++] = JsonConstants.Quote;
 01537            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 01538            output[BytesPending++] = JsonConstants.Space;
 1539
 01540            output[BytesPending++] = JsonConstants.Quote;
 1541
 01542            TranscodeAndWrite(escapedValue, output);
 1543
 01544            output[BytesPending++] = JsonConstants.Quote;
 01545        }
 1546
 1547        // TODO: https://github.com/dotnet/runtime/issues/29293
 1548        private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue)
 01549        {
 01550            int indent = Indentation;
 01551            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 1552
 01553            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01554            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - escapedValue.Length - 7 - _newLineLength);
 1555
 01556            int minRequired = indent + escapedPropertyName.Length + escapedValue.Length + 6; // 2 quotes for property na
 01557            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 1558
 01559            if (_memory.Length - BytesPending < maxRequired)
 01560            {
 01561                Grow(maxRequired);
 01562            }
 1563
 01564            Span<byte> output = _memory.Span;
 1565
 01566            if (_currentDepth < 0)
 01567            {
 01568                output[BytesPending++] = JsonConstants.ListSeparator;
 01569            }
 1570
 01571            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 1572
 01573            if (_tokenType != JsonTokenType.None)
 01574            {
 01575                WriteNewLine(output);
 01576            }
 1577
 01578            WriteIndentation(output.Slice(BytesPending), indent);
 01579            BytesPending += indent;
 1580
 01581            output[BytesPending++] = JsonConstants.Quote;
 1582
 01583            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 01584            BytesPending += escapedPropertyName.Length;
 1585
 01586            output[BytesPending++] = JsonConstants.Quote;
 01587            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 01588            output[BytesPending++] = JsonConstants.Space;
 1589
 01590            output[BytesPending++] = JsonConstants.Quote;
 1591
 01592            escapedValue.CopyTo(output.Slice(BytesPending));
 01593            BytesPending += escapedValue.Length;
 1594
 01595            output[BytesPending++] = JsonConstants.Quote;
 01596        }
 1597
 1598        // TODO: https://github.com/dotnet/runtime/issues/29293
 1599        private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue)
 01600        {
 01601            int indent = Indentation;
 01602            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 1603
 01604            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01605            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
 01609            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 1610
 01611            if (_memory.Length - BytesPending < maxRequired)
 01612            {
 01613                Grow(maxRequired);
 01614            }
 1615
 01616            Span<byte> output = _memory.Span;
 1617
 01618            if (_currentDepth < 0)
 01619            {
 01620                output[BytesPending++] = JsonConstants.ListSeparator;
 01621            }
 1622
 01623            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 1624
 01625            if (_tokenType != JsonTokenType.None)
 01626            {
 01627                WriteNewLine(output);
 01628            }
 1629
 01630            WriteIndentation(output.Slice(BytesPending), indent);
 01631            BytesPending += indent;
 1632
 01633            output[BytesPending++] = JsonConstants.Quote;
 1634
 01635            TranscodeAndWrite(escapedPropertyName, output);
 1636
 01637            output[BytesPending++] = JsonConstants.Quote;
 01638            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 01639            output[BytesPending++] = JsonConstants.Space;
 1640
 01641            output[BytesPending++] = JsonConstants.Quote;
 1642
 01643            escapedValue.CopyTo(output.Slice(BytesPending));
 01644            BytesPending += escapedValue.Length;
 1645
 01646            output[BytesPending++] = JsonConstants.Quote;
 01647        }
 1648
 1649        // TODO: https://github.com/dotnet/runtime/issues/29293
 1650        private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue)
 01651        {
 01652            int indent = Indentation;
 01653            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 1654
 01655            Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
 01656            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
 01660            int maxRequired = indent + (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + escape
 1661
 01662            if (_memory.Length - BytesPending < maxRequired)
 01663            {
 01664                Grow(maxRequired);
 01665            }
 1666
 01667            Span<byte> output = _memory.Span;
 1668
 01669            if (_currentDepth < 0)
 01670            {
 01671                output[BytesPending++] = JsonConstants.ListSeparator;
 01672            }
 1673
 01674            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 1675
 01676            if (_tokenType != JsonTokenType.None)
 01677            {
 01678                WriteNewLine(output);
 01679            }
 1680
 01681            WriteIndentation(output.Slice(BytesPending), indent);
 01682            BytesPending += indent;
 1683
 01684            output[BytesPending++] = JsonConstants.Quote;
 1685
 01686            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 01687            BytesPending += escapedPropertyName.Length;
 1688
 01689            output[BytesPending++] = JsonConstants.Quote;
 01690            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 01691            output[BytesPending++] = JsonConstants.Space;
 1692
 01693            output[BytesPending++] = JsonConstants.Quote;
 1694
 01695            TranscodeAndWrite(escapedValue, output);
 1696
 01697            output[BytesPending++] = JsonConstants.Quote;
 01698        }
 1699    }
 1700}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteProperties.UnsignedNumber.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the pre-encoded property name and <see cref="ulong"/> value (as a JSON number) as part of a name/valu
 14        /// </summary>
 15        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="ulong"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 3
 22        /// </remarks>
 23        [CLSCompliant(false)]
 24        public void WriteNumber(JsonEncodedText propertyName, ulong value)
 025        {
 026            ReadOnlySpan<byte> utf8PropertyName = propertyName.EncodedUtf8Bytes;
 027            Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
 28
 029            WriteNumberByOptions(utf8PropertyName, value);
 30
 031            SetFlagToAddListSeparatorBeforeNextItem();
 032            _tokenType = JsonTokenType.Number;
 033        }
 34
 35        /// <summary>
 36        /// Writes the property name and <see cref="ulong"/> value (as a JSON number) as part of a name/value pair of a 
 37        /// </summary>
 38        /// <param name="propertyName">The name of the property to write.</param>
 39        /// <param name="value">The value to write.</param>
 40        /// <exception cref="ArgumentException">
 41        /// Thrown when the specified property name is too large.
 42        /// </exception>
 43        /// <exception cref="ArgumentNullException">
 44        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 45        /// </exception>
 46        /// <exception cref="InvalidOperationException">
 47        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 48        /// </exception>
 49        /// <remarks>
 50        /// Writes the <see cref="ulong"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 3
 51        /// The property name is escaped before writing.
 52        /// </remarks>
 53        [CLSCompliant(false)]
 54        public void WriteNumber(string propertyName, ulong value)
 055        {
 056            ArgumentNullException.ThrowIfNull(propertyName);
 057            WriteNumber(propertyName.AsSpan(), value);
 058        }
 59
 60        /// <summary>
 61        /// Writes the property name and <see cref="ulong"/> value (as a JSON number) as part of a name/value pair of a 
 62        /// </summary>
 63        /// <param name="propertyName">The name of the property to write.</param>
 64        /// <param name="value">The value to write.</param>
 65        /// <exception cref="ArgumentException">
 66        /// Thrown when the specified property name is too large.
 67        /// </exception>
 68        /// <exception cref="InvalidOperationException">
 69        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 70        /// </exception>
 71        /// <remarks>
 72        /// Writes the <see cref="ulong"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 3
 73        /// The property name is escaped before writing.
 74        /// </remarks>
 75        [CLSCompliant(false)]
 76        public void WriteNumber(ReadOnlySpan<char> propertyName, ulong value)
 077        {
 078            JsonWriterHelper.ValidateProperty(propertyName);
 79
 080            WriteNumberEscape(propertyName, value);
 81
 082            SetFlagToAddListSeparatorBeforeNextItem();
 083            _tokenType = JsonTokenType.Number;
 084        }
 85
 86        /// <summary>
 87        /// Writes the property name and <see cref="ulong"/> value (as a JSON number) as part of a name/value pair of a 
 88        /// </summary>
 89        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 90        /// <param name="value">The value to write.</param>
 91        /// <exception cref="ArgumentException">
 92        /// Thrown when the specified property name is too large.
 93        /// </exception>
 94        /// <exception cref="InvalidOperationException">
 95        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 96        /// </exception>
 97        /// <remarks>
 98        /// Writes the <see cref="ulong"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 3
 99        /// The property name is escaped before writing.
 100        /// </remarks>
 101        [CLSCompliant(false)]
 102        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, ulong value)
 0103        {
 0104            JsonWriterHelper.ValidateProperty(utf8PropertyName);
 105
 0106            WriteNumberEscape(utf8PropertyName, value);
 107
 0108            SetFlagToAddListSeparatorBeforeNextItem();
 0109            _tokenType = JsonTokenType.Number;
 0110        }
 111
 112        /// <summary>
 113        /// Writes the pre-encoded property name and <see cref="uint"/> value (as a JSON number) as part of a name/value
 114        /// </summary>
 115        /// <param name="propertyName">The JSON-encoded name of the property to write.</param>
 116        /// <param name="value">The value to write.</param>
 117        /// <exception cref="InvalidOperationException">
 118        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 119        /// </exception>
 120        /// <remarks>
 121        /// Writes the <see cref="uint"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 122        /// </remarks>
 123        [CLSCompliant(false)]
 124        public void WriteNumber(JsonEncodedText propertyName, uint value)
 0125            => WriteNumber(propertyName, (ulong)value);
 126
 127        /// <summary>
 128        /// Writes the property name and <see cref="uint"/> value (as a JSON number) as part of a name/value pair of a J
 129        /// </summary>
 130        /// <param name="propertyName">The name of the property to write.</param>
 131        /// <param name="value">The value to write.</param>
 132        /// <exception cref="ArgumentException">
 133        /// Thrown when the specified property name is too large.
 134        /// </exception>
 135        /// <exception cref="ArgumentNullException">
 136        /// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
 137        /// </exception>
 138        /// <exception cref="InvalidOperationException">
 139        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 140        /// </exception>
 141        /// <remarks>
 142        /// Writes the <see cref="uint"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 143        /// The property name is escaped before writing.
 144        /// </remarks>
 145        [CLSCompliant(false)]
 146        public void WriteNumber(string propertyName, uint value)
 0147        {
 0148            ArgumentNullException.ThrowIfNull(propertyName);
 0149            WriteNumber(propertyName.AsSpan(), (ulong)value);
 0150        }
 151
 152        /// <summary>
 153        /// Writes the property name and <see cref="uint"/> value (as a JSON number) as part of a name/value pair of a J
 154        /// </summary>
 155        /// <param name="propertyName">The name of the property to write.</param>
 156        /// <param name="value">The value to write.</param>
 157        /// <exception cref="ArgumentException">
 158        /// Thrown when the specified property name is too large.
 159        /// </exception>
 160        /// <exception cref="InvalidOperationException">
 161        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 162        /// </exception>
 163        /// <remarks>
 164        /// Writes the <see cref="uint"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 165        /// The property name is escaped before writing.
 166        /// </remarks>
 167        [CLSCompliant(false)]
 168        public void WriteNumber(ReadOnlySpan<char> propertyName, uint value)
 0169            => WriteNumber(propertyName, (ulong)value);
 170
 171        /// <summary>
 172        /// Writes the property name and <see cref="uint"/> value (as a JSON number) as part of a name/value pair of a J
 173        /// </summary>
 174        /// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
 175        /// <param name="value">The value to write.</param>
 176        /// <exception cref="ArgumentException">
 177        /// Thrown when the specified property name is too large.
 178        /// </exception>
 179        /// <exception cref="InvalidOperationException">
 180        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 181        /// </exception>
 182        /// <remarks>
 183        /// Writes the <see cref="uint"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 184        /// The property name is escaped before writing.
 185        /// </remarks>
 186        [CLSCompliant(false)]
 187        public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, uint value)
 0188            => WriteNumber(utf8PropertyName, (ulong)value);
 189
 190        private void WriteNumberEscape(ReadOnlySpan<char> propertyName, ulong value)
 0191        {
 0192            int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
 193
 0194            Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
 195
 0196            if (propertyIdx != -1)
 0197            {
 0198                WriteNumberEscapeProperty(propertyName, value, propertyIdx);
 0199            }
 200            else
 0201            {
 0202                WriteNumberByOptions(propertyName, value);
 0203            }
 0204        }
 205
 206        private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, ulong value)
 0207        {
 0208            int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
 209
 0210            Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
 211
 0212            if (propertyIdx != -1)
 0213            {
 0214                WriteNumberEscapeProperty(utf8PropertyName, value, propertyIdx);
 0215            }
 216            else
 0217            {
 0218                WriteNumberByOptions(utf8PropertyName, value);
 0219            }
 0220        }
 221
 222        private void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, ulong value, int firstEscapeIndexProp)
 0223        {
 0224            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
 0225            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
 226
 0227            char[]? propertyArray = null;
 228
 0229            int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
 230
 0231            Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
 0232                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0233                (propertyArray = ArrayPool<char>.Shared.Rent(length));
 234
 0235            JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out
 236
 0237            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 238
 0239            if (propertyArray != null)
 0240            {
 0241                ArrayPool<char>.Shared.Return(propertyArray);
 0242            }
 0243        }
 244
 245        private void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ulong value, int firstEscapeIndexPro
 0246        {
 0247            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
 0248            Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
 249
 0250            byte[]? propertyArray = null;
 251
 0252            int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
 253
 0254            Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
 0255                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0256                (propertyArray = ArrayPool<byte>.Shared.Rent(length));
 257
 0258            JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder,
 259
 0260            WriteNumberByOptions(escapedPropertyName.Slice(0, written), value);
 261
 0262            if (propertyArray != null)
 0263            {
 0264                ArrayPool<byte>.Shared.Return(propertyArray);
 0265            }
 0266        }
 267
 268        private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, ulong value)
 0269        {
 0270            ValidateWritingProperty();
 0271            if (_options.Indented)
 0272            {
 0273                WriteNumberIndented(propertyName, value);
 0274            }
 275            else
 0276            {
 0277                WriteNumberMinimized(propertyName, value);
 0278            }
 0279        }
 280
 281        private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, ulong value)
 0282        {
 0283            ValidateWritingProperty();
 0284            if (_options.Indented)
 0285            {
 0286                WriteNumberIndented(utf8PropertyName, value);
 0287            }
 288            else
 0289            {
 0290                WriteNumberMinimized(utf8PropertyName, value);
 0291            }
 0292        }
 293
 294        private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, ulong value)
 0295        {
 0296            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 297
 298            // All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + JsonConstants.MaximumF
 299            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0300            int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + JsonCons
 301
 0302            if (_memory.Length - BytesPending < maxRequired)
 0303            {
 0304                Grow(maxRequired);
 0305            }
 306
 0307            Span<byte> output = _memory.Span;
 308
 0309            if (_currentDepth < 0)
 0310            {
 0311                output[BytesPending++] = JsonConstants.ListSeparator;
 0312            }
 0313            output[BytesPending++] = JsonConstants.Quote;
 314
 0315            TranscodeAndWrite(escapedPropertyName, output);
 316
 0317            output[BytesPending++] = JsonConstants.Quote;
 0318            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 319
 0320            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0321            Debug.Assert(result);
 0322            BytesPending += bytesWritten;
 0323        }
 324
 325        private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, ulong value)
 0326        {
 0327            Debug.Assert(escapedPropertyName.Length < int.MaxValue - JsonConstants.MaximumFormatUInt64Length - 4);
 328
 0329            int minRequired = escapedPropertyName.Length + JsonConstants.MaximumFormatUInt64Length + 3; // 2 quotes for 
 0330            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 331
 0332            if (_memory.Length - BytesPending < maxRequired)
 0333            {
 0334                Grow(maxRequired);
 0335            }
 336
 0337            Span<byte> output = _memory.Span;
 338
 0339            if (_currentDepth < 0)
 0340            {
 0341                output[BytesPending++] = JsonConstants.ListSeparator;
 0342            }
 0343            output[BytesPending++] = JsonConstants.Quote;
 344
 0345            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0346            BytesPending += escapedPropertyName.Length;
 347
 0348            output[BytesPending++] = JsonConstants.Quote;
 0349            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 350
 0351            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0352            Debug.Assert(result);
 0353            BytesPending += bytesWritten;
 0354        }
 355
 356        private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, ulong value)
 0357        {
 0358            int indent = Indentation;
 0359            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 360
 0361            Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) 
 362
 363            // All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + JsonConstants
 364            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0365            int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) +
 366
 0367            if (_memory.Length - BytesPending < maxRequired)
 0368            {
 0369                Grow(maxRequired);
 0370            }
 371
 0372            Span<byte> output = _memory.Span;
 373
 0374            if (_currentDepth < 0)
 0375            {
 0376                output[BytesPending++] = JsonConstants.ListSeparator;
 0377            }
 378
 0379            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 380
 0381            if (_tokenType != JsonTokenType.None)
 0382            {
 0383                WriteNewLine(output);
 0384            }
 385
 0386            WriteIndentation(output.Slice(BytesPending), indent);
 0387            BytesPending += indent;
 388
 0389            output[BytesPending++] = JsonConstants.Quote;
 390
 0391            TranscodeAndWrite(escapedPropertyName, output);
 392
 0393            output[BytesPending++] = JsonConstants.Quote;
 0394            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0395            output[BytesPending++] = JsonConstants.Space;
 396
 0397            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0398            Debug.Assert(result);
 0399            BytesPending += bytesWritten;
 0400        }
 401
 402        private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, ulong value)
 0403        {
 0404            int indent = Indentation;
 0405            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 406
 0407            Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatUInt64Length - 
 408
 0409            int minRequired = indent + escapedPropertyName.Length + JsonConstants.MaximumFormatUInt64Length + 4; // 2 qu
 0410            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 411
 0412            if (_memory.Length - BytesPending < maxRequired)
 0413            {
 0414                Grow(maxRequired);
 0415            }
 416
 0417            Span<byte> output = _memory.Span;
 418
 0419            if (_currentDepth < 0)
 0420            {
 0421                output[BytesPending++] = JsonConstants.ListSeparator;
 0422            }
 423
 0424            Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
 425
 0426            if (_tokenType != JsonTokenType.None)
 0427            {
 0428                WriteNewLine(output);
 0429            }
 430
 0431            WriteIndentation(output.Slice(BytesPending), indent);
 0432            BytesPending += indent;
 433
 0434            output[BytesPending++] = JsonConstants.Quote;
 435
 0436            escapedPropertyName.CopyTo(output.Slice(BytesPending));
 0437            BytesPending += escapedPropertyName.Length;
 438
 0439            output[BytesPending++] = JsonConstants.Quote;
 0440            output[BytesPending++] = JsonConstants.KeyValueSeparator;
 0441            output[BytesPending++] = JsonConstants.Space;
 442
 0443            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0444            Debug.Assert(result);
 0445            BytesPending += bytesWritten;
 0446        }
 447
 448        internal void WritePropertyName(uint value)
 0449            => WritePropertyName((ulong)value);
 450
 451        internal void WritePropertyName(ulong value)
 0452        {
 0453            Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatUInt64Length];
 454
 0455            bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
 0456            Debug.Assert(result);
 457
 0458            WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
 0459        }
 460    }
 461}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Bytes.cs

#LineLine coverage
 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
 4using System.Buffers.Text;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    public sealed partial class Utf8JsonWriter
 10    {
 11        /// <summary>
 12        /// Writes the raw bytes value as a Base64 encoded JSON string as an element of a JSON array.
 13        /// </summary>
 14        /// <param name="bytes">The binary data to write as Base64 encoded text.</param>
 15        /// <exception cref="ArgumentException">
 16        /// Thrown when the specified value is too large.
 17        /// </exception>
 18        /// <exception cref="InvalidOperationException">
 19        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 20        /// </exception>
 21        /// <remarks>
 22        /// The bytes are encoded before writing.
 23        /// </remarks>
 24        public void WriteBase64StringValue(ReadOnlySpan<byte> bytes)
 025        {
 026            WriteBase64ByOptions(bytes);
 27
 028            SetFlagToAddListSeparatorBeforeNextItem();
 029            _tokenType = JsonTokenType.String;
 030        }
 31
 32        private void WriteBase64ByOptions(ReadOnlySpan<byte> bytes)
 033        {
 034            if (!_options.SkipValidation)
 035            {
 036                ValidateWritingValue();
 037            }
 38
 039            if (_options.Indented)
 040            {
 041                WriteBase64Indented(bytes);
 042            }
 43            else
 044            {
 045                WriteBase64Minimized(bytes);
 046            }
 047        }
 48
 49        // TODO: https://github.com/dotnet/runtime/issues/29293
 50        private void WriteBase64Minimized(ReadOnlySpan<byte> bytes)
 051        {
 52            // Base64.GetMaxEncodedToUtf8Length checks to make sure the length is <= int.MaxValue / 4 * 3,
 53            // as a length longer than that would overflow int.MaxValue when Base64 encoded. To ensure we
 54            // throw an appropriate exception, we check the same condition here first.
 55            const int MaxLengthAllowed = int.MaxValue / 4 * 3;
 056            if (bytes.Length > MaxLengthAllowed)
 057            {
 058                ThrowHelper.ThrowArgumentException_ValueTooLarge(bytes.Length);
 59            }
 60
 061            int encodingLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 062            Debug.Assert(encodingLength <= int.MaxValue - 3);
 63
 64            // 2 quotes to surround the base-64 encoded string value.
 65            // Optionally, 1 list separator
 066            int maxRequired = encodingLength + 3;
 067            Debug.Assert((uint)maxRequired <= int.MaxValue);
 68
 069            if (_memory.Length - BytesPending < maxRequired)
 070            {
 071                Grow(maxRequired);
 072            }
 73
 074            Span<byte> output = _memory.Span;
 75
 076            if (_currentDepth < 0)
 077            {
 078                output[BytesPending++] = JsonConstants.ListSeparator;
 079            }
 080            output[BytesPending++] = JsonConstants.Quote;
 81
 082            Base64EncodeAndWrite(bytes, output);
 83
 084            output[BytesPending++] = JsonConstants.Quote;
 085        }
 86
 87        // TODO: https://github.com/dotnet/runtime/issues/29293
 88        private void WriteBase64Indented(ReadOnlySpan<byte> bytes)
 089        {
 090            int indent = Indentation;
 091            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 92
 93            // Base64.GetMaxEncodedToUtf8Length checks to make sure the length is <= int.MaxValue / 4 * 3,
 94            // as a length longer than that would overflow int.MaxValue when Base64 encoded. However, we
 95            // also need the indentation + 2 quotes, and optionally a list separate and 1-2 bytes for a new line.
 96            // Validate the encoded bytes length won't overflow with all of the length.
 097            int extraSpaceRequired = indent + 3 + _newLineLength;
 098            int maxLengthAllowed = int.MaxValue / 4 * 3 - extraSpaceRequired;
 099            if (bytes.Length > maxLengthAllowed)
 0100            {
 0101                ThrowHelper.ThrowArgumentException_ValueTooLarge(bytes.Length);
 102            }
 103
 0104            int encodingLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 105
 0106            int maxRequired = encodingLength + extraSpaceRequired;
 0107            Debug.Assert((uint)maxRequired <= int.MaxValue - 3);
 108
 0109            if (_memory.Length - BytesPending < maxRequired)
 0110            {
 0111                Grow(maxRequired);
 0112            }
 113
 0114            Span<byte> output = _memory.Span;
 115
 0116            if (_currentDepth < 0)
 0117            {
 0118                output[BytesPending++] = JsonConstants.ListSeparator;
 0119            }
 120
 0121            if (_tokenType != JsonTokenType.PropertyName)
 0122            {
 0123                if (_tokenType != JsonTokenType.None)
 0124                {
 0125                    WriteNewLine(output);
 0126                }
 0127                WriteIndentation(output.Slice(BytesPending), indent);
 0128                BytesPending += indent;
 0129            }
 130
 0131            output[BytesPending++] = JsonConstants.Quote;
 132
 0133            Base64EncodeAndWrite(bytes, output);
 134
 0135            output[BytesPending++] = JsonConstants.Quote;
 0136        }
 137    }
 138}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Comment.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    public sealed partial class Utf8JsonWriter
 10    {
 011        private static readonly char[] s_singleLineCommentDelimiter = new char[2] { '*', '/' };
 012        private static ReadOnlySpan<byte> SingleLineCommentDelimiterUtf8 => "*/"u8;
 13
 14        /// <summary>
 15        /// Writes the string text value (as a JSON comment).
 16        /// </summary>
 17        /// <param name="value">The value to write as a JSON comment within /*..*/.</param>
 18        /// <exception cref="ArgumentException">
 19        /// Thrown when the specified value is too large OR if the given string text value contains a comment delimiter 
 20        /// </exception>
 21        /// <exception cref="ArgumentNullException">
 22        /// The <paramref name="value"/> parameter is <see langword="null"/>.
 23        /// </exception>
 24        /// <remarks>
 25        /// The comment value is not escaped before writing.
 26        /// </remarks>
 27        public void WriteCommentValue(string value)
 028        {
 029            ArgumentNullException.ThrowIfNull(value);
 030            WriteCommentValue(value.AsSpan());
 031        }
 32
 33        /// <summary>
 34        /// Writes the text value (as a JSON comment).
 35        /// </summary>
 36        /// <param name="value">The value to write as a JSON comment within /*..*/.</param>
 37        /// <exception cref="ArgumentException">
 38        /// Thrown when the specified value is too large OR if the given text value contains a comment delimiter (that i
 39        /// </exception>
 40        /// <remarks>
 41        /// The comment value is not escaped before writing.
 42        /// </remarks>
 43        public void WriteCommentValue(ReadOnlySpan<char> value)
 044        {
 045            JsonWriterHelper.ValidateValue(value);
 46
 047            if (value.Contains(s_singleLineCommentDelimiter, StringComparison.Ordinal))
 048            {
 049                ThrowHelper.ThrowArgumentException_InvalidCommentValue();
 50            }
 51
 052            WriteCommentByOptions(value);
 053            if (_tokenType is JsonTokenType.PropertyName or JsonTokenType.None)
 054            {
 055                _commentAfterNoneOrPropertyName = true;
 056            }
 057        }
 58
 59        private void WriteCommentByOptions(ReadOnlySpan<char> value)
 060        {
 061            if (!_options.SkipValidation)
 062            {
 63                // Comments generally can be placed anywhere in JSON, but not after a non-final
 64                // string segment.
 065                ValidateNotWithinUnfinalizedString();
 066            }
 67
 068            if (_options.Indented)
 069            {
 070                WriteCommentIndented(value);
 071            }
 72            else
 073            {
 074                WriteCommentMinimized(value);
 075            }
 076        }
 77
 78        private void WriteCommentMinimized(ReadOnlySpan<char> value)
 079        {
 080            Debug.Assert(value.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - 4);
 81
 82            // All ASCII, /*...*/ => escapedValue.Length + 4
 83            // Optionally, up to 3x growth when transcoding
 084            int maxRequired = (value.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 4;
 85
 086            if (_memory.Length - BytesPending < maxRequired)
 087            {
 088                Grow(maxRequired);
 089            }
 90
 091            Span<byte> output = _memory.Span;
 92
 093            output[BytesPending++] = JsonConstants.Slash;
 094            output[BytesPending++] = JsonConstants.Asterisk;
 95
 096            OperationStatus status = JsonWriterHelper.ToUtf8(value, output.Slice(BytesPending), out int written);
 097            Debug.Assert(status != OperationStatus.DestinationTooSmall);
 098            if (status == OperationStatus.InvalidData)
 099            {
 0100                ThrowHelper.ThrowArgumentException_InvalidUTF16(value[written]);
 101            }
 102
 0103            BytesPending += written;
 104
 0105            output[BytesPending++] = JsonConstants.Asterisk;
 0106            output[BytesPending++] = JsonConstants.Slash;
 0107        }
 108
 109        private void WriteCommentIndented(ReadOnlySpan<char> value)
 0110        {
 0111            int indent = Indentation;
 0112            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 113
 0114            Debug.Assert(value.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - 4 -
 115
 116            // All ASCII, /*...*/ => escapedValue.Length + 4
 117            // Optionally, 1-2 bytes for new line, and up to 3x growth when transcoding
 0118            int maxRequired = indent + (value.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 4 + _newLineL
 119
 0120            if (_memory.Length - BytesPending < maxRequired)
 0121            {
 0122                Grow(maxRequired);
 0123            }
 124
 0125            Span<byte> output = _memory.Span;
 126
 0127            if (_tokenType != JsonTokenType.None || _commentAfterNoneOrPropertyName)
 0128            {
 0129                WriteNewLine(output);
 0130                WriteIndentation(output.Slice(BytesPending), indent);
 0131                BytesPending += indent;
 0132            }
 133
 0134            output[BytesPending++] = JsonConstants.Slash;
 0135            output[BytesPending++] = JsonConstants.Asterisk;
 136
 0137            OperationStatus status = JsonWriterHelper.ToUtf8(value, output.Slice(BytesPending), out int written);
 0138            Debug.Assert(status != OperationStatus.DestinationTooSmall);
 0139            if (status == OperationStatus.InvalidData)
 0140            {
 0141                ThrowHelper.ThrowArgumentException_InvalidUTF16(value[written]);
 142            }
 143
 0144            BytesPending += written;
 145
 0146            output[BytesPending++] = JsonConstants.Asterisk;
 0147            output[BytesPending++] = JsonConstants.Slash;
 0148        }
 149
 150        /// <summary>
 151        /// Writes the UTF-8 text value (as a JSON comment).
 152        /// </summary>
 153        /// <param name="utf8Value">The UTF-8 encoded value to be written as a JSON comment within /*..*/.</param>
 154        /// <remarks>
 155        /// The comment value is not escaped before writing.
 156        /// </remarks>
 157        /// <exception cref="ArgumentException">
 158        /// Thrown when the specified value is too large OR if the given UTF-8 text value contains a comment delimiter (
 159        /// </exception>
 160        public void WriteCommentValue(ReadOnlySpan<byte> utf8Value)
 0161        {
 0162            JsonWriterHelper.ValidateValue(utf8Value);
 163
 0164            if (utf8Value.IndexOf(SingleLineCommentDelimiterUtf8) >= 0)
 0165            {
 0166                ThrowHelper.ThrowArgumentException_InvalidCommentValue();
 167            }
 168
 0169            if (!JsonWriterHelper.IsValidUtf8String(utf8Value))
 0170            {
 0171                ThrowHelper.ThrowArgumentException_InvalidUTF8(utf8Value);
 172            }
 173
 0174            WriteCommentByOptions(utf8Value);
 0175            if (_tokenType is JsonTokenType.PropertyName or JsonTokenType.None)
 0176            {
 0177                _commentAfterNoneOrPropertyName = true;
 0178            }
 0179        }
 180
 181        private void WriteCommentByOptions(ReadOnlySpan<byte> utf8Value)
 0182        {
 0183            if (_options.Indented)
 0184            {
 0185                WriteCommentIndented(utf8Value);
 0186            }
 187            else
 0188            {
 0189                WriteCommentMinimized(utf8Value);
 0190            }
 0191        }
 192
 193        private void WriteCommentMinimized(ReadOnlySpan<byte> utf8Value)
 0194        {
 0195            Debug.Assert(utf8Value.Length < int.MaxValue - 4);
 196
 0197            int maxRequired = utf8Value.Length + 4; // /*...*/
 198
 0199            if (_memory.Length - BytesPending < maxRequired)
 0200            {
 0201                Grow(maxRequired);
 0202            }
 203
 0204            Span<byte> output = _memory.Span;
 205
 0206            output[BytesPending++] = JsonConstants.Slash;
 0207            output[BytesPending++] = JsonConstants.Asterisk;
 208
 0209            utf8Value.CopyTo(output.Slice(BytesPending));
 0210            BytesPending += utf8Value.Length;
 211
 0212            output[BytesPending++] = JsonConstants.Asterisk;
 0213            output[BytesPending++] = JsonConstants.Slash;
 0214        }
 215
 216        private void WriteCommentIndented(ReadOnlySpan<byte> utf8Value)
 0217        {
 0218            int indent = Indentation;
 0219            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 220
 0221            Debug.Assert(utf8Value.Length < int.MaxValue - indent - 4 - _newLineLength);
 222
 0223            int minRequired = indent + utf8Value.Length + 4; // /*...*/
 0224            int maxRequired = minRequired + _newLineLength; // Optionally, 1-2 bytes for new line
 225
 0226            if (_memory.Length - BytesPending < maxRequired)
 0227            {
 0228                Grow(maxRequired);
 0229            }
 230
 0231            Span<byte> output = _memory.Span;
 232
 0233            if (_tokenType != JsonTokenType.None || _commentAfterNoneOrPropertyName)
 0234            {
 0235                WriteNewLine(output);
 236
 0237                WriteIndentation(output.Slice(BytesPending), indent);
 0238                BytesPending += indent;
 0239            }
 240
 0241            output[BytesPending++] = JsonConstants.Slash;
 0242            output[BytesPending++] = JsonConstants.Asterisk;
 243
 0244            utf8Value.CopyTo(output.Slice(BytesPending));
 0245            BytesPending += utf8Value.Length;
 246
 0247            output[BytesPending++] = JsonConstants.Asterisk;
 0248            output[BytesPending++] = JsonConstants.Slash;
 0249        }
 250    }
 251}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTime.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the <see cref="DateTime"/> value (as a JSON string) as an element of a JSON array.
 14        /// </summary>
 15        /// <param name="value">The value 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        /// <remarks>
 20        /// Writes the <see cref="DateTime"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for example
 21        /// </remarks>
 22        public void WriteStringValue(DateTime value)
 023        {
 024            if (!_options.SkipValidation)
 025            {
 026                ValidateWritingValue();
 027            }
 28
 029            if (_options.Indented)
 030            {
 031                WriteStringValueIndented(value);
 032            }
 33            else
 034            {
 035                WriteStringValueMinimized(value);
 036            }
 37
 038            SetFlagToAddListSeparatorBeforeNextItem();
 039            _tokenType = JsonTokenType.String;
 040        }
 41
 42        private void WriteStringValueMinimized(DateTime value)
 043        {
 044            int maxRequired = JsonConstants.MaximumFormatDateTimeOffsetLength + 3; // 2 quotes, and optionally, 1 list s
 45
 046            if (_memory.Length - BytesPending < maxRequired)
 047            {
 048                Grow(maxRequired);
 049            }
 50
 051            Span<byte> output = _memory.Span;
 52
 053            if (_currentDepth < 0)
 054            {
 055                output[BytesPending++] = JsonConstants.ListSeparator;
 056            }
 57
 058            output[BytesPending++] = JsonConstants.Quote;
 59
 060            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 061            BytesPending += bytesWritten;
 62
 063            output[BytesPending++] = JsonConstants.Quote;
 064        }
 65
 66        private void WriteStringValueIndented(DateTime value)
 067        {
 068            int indent = Indentation;
 069            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 70
 71            // 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
 072            int maxRequired = indent + JsonConstants.MaximumFormatDateTimeOffsetLength + 3 + _newLineLength;
 73
 074            if (_memory.Length - BytesPending < maxRequired)
 075            {
 076                Grow(maxRequired);
 077            }
 78
 079            Span<byte> output = _memory.Span;
 80
 081            if (_currentDepth < 0)
 082            {
 083                output[BytesPending++] = JsonConstants.ListSeparator;
 084            }
 85
 086            if (_tokenType != JsonTokenType.PropertyName)
 087            {
 088                if (_tokenType != JsonTokenType.None)
 089                {
 090                    WriteNewLine(output);
 091                }
 092                WriteIndentation(output.Slice(BytesPending), indent);
 093                BytesPending += indent;
 094            }
 95
 096            output[BytesPending++] = JsonConstants.Quote;
 97
 098            JsonWriterHelper.WriteDateTimeTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 099            BytesPending += bytesWritten;
 100
 0101            output[BytesPending++] = JsonConstants.Quote;
 0102        }
 103    }
 104}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.DateTimeOffset.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8
 9namespace System.Text.Json
 10{
 11    public sealed partial class Utf8JsonWriter
 12    {
 13        /// <summary>
 14        /// Writes the <see cref="DateTimeOffset"/> value (as a JSON string) as an element of a JSON array.
 15        /// </summary>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="DateTimeOffset"/> using the round-trippable ('O') <see cref="StandardFormat"/> , for e
 22        /// </remarks>
 23        public void WriteStringValue(DateTimeOffset value)
 024        {
 025            if (!_options.SkipValidation)
 026            {
 027                ValidateWritingValue();
 028            }
 29
 030            if (_options.Indented)
 031            {
 032                WriteStringValueIndented(value);
 033            }
 34            else
 035            {
 036                WriteStringValueMinimized(value);
 037            }
 38
 039            SetFlagToAddListSeparatorBeforeNextItem();
 040            _tokenType = JsonTokenType.String;
 041        }
 42
 43        private void WriteStringValueMinimized(DateTimeOffset value)
 044        {
 045            int maxRequired = JsonConstants.MaximumFormatDateTimeOffsetLength + 3; // 2 quotes, and optionally, 1 list s
 46
 047            if (_memory.Length - BytesPending < maxRequired)
 048            {
 049                Grow(maxRequired);
 050            }
 51
 052            Span<byte> output = _memory.Span;
 53
 054            if (_currentDepth < 0)
 055            {
 056                output[BytesPending++] = JsonConstants.ListSeparator;
 057            }
 58
 059            output[BytesPending++] = JsonConstants.Quote;
 60
 061            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 062            BytesPending += bytesWritten;
 63
 064            output[BytesPending++] = JsonConstants.Quote;
 065        }
 66
 67        private void WriteStringValueIndented(DateTimeOffset value)
 068        {
 069            int indent = Indentation;
 070            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 71
 72            // 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
 073            int maxRequired = indent + JsonConstants.MaximumFormatDateTimeOffsetLength + 3 + _newLineLength;
 74
 075            if (_memory.Length - BytesPending < maxRequired)
 076            {
 077                Grow(maxRequired);
 078            }
 79
 080            Span<byte> output = _memory.Span;
 81
 082            if (_currentDepth < 0)
 083            {
 084                output[BytesPending++] = JsonConstants.ListSeparator;
 085            }
 86
 087            if (_tokenType != JsonTokenType.PropertyName)
 088            {
 089                if (_tokenType != JsonTokenType.None)
 090                {
 091                    WriteNewLine(output);
 092                }
 093                WriteIndentation(output.Slice(BytesPending), indent);
 094                BytesPending += indent;
 095            }
 96
 097            output[BytesPending++] = JsonConstants.Quote;
 98
 099            JsonWriterHelper.WriteDateTimeOffsetTrimmed(output.Slice(BytesPending), value, out int bytesWritten);
 0100            BytesPending += bytesWritten;
 101
 0102            output[BytesPending++] = JsonConstants.Quote;
 0103        }
 104    }
 105}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Decimal.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the <see cref="decimal"/> value (as a JSON number) as an element of a JSON array.
 14        /// </summary>
 15        /// <param name="value">The value 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        /// <remarks>
 20        /// Writes the <see cref="decimal"/> using the default <see cref="StandardFormat"/> (that is, 'G').
 21        /// </remarks>
 22        public void WriteNumberValue(decimal value)
 023        {
 024            if (!_options.SkipValidation)
 025            {
 026                ValidateWritingValue();
 027            }
 28
 029            if (_options.Indented)
 030            {
 031                WriteNumberValueIndented(value);
 032            }
 33            else
 034            {
 035                WriteNumberValueMinimized(value);
 036            }
 37
 038            SetFlagToAddListSeparatorBeforeNextItem();
 039            _tokenType = JsonTokenType.Number;
 040        }
 41
 42        private void WriteNumberValueMinimized(decimal value)
 043        {
 044            int maxRequired = JsonConstants.MaximumFormatDecimalLength + 1; // Optionally, 1 list separator
 45
 046            if (_memory.Length - BytesPending < maxRequired)
 047            {
 048                Grow(maxRequired);
 049            }
 50
 051            Span<byte> output = _memory.Span;
 52
 053            if (_currentDepth < 0)
 054            {
 055                output[BytesPending++] = JsonConstants.ListSeparator;
 056            }
 57
 058            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 059            Debug.Assert(result);
 060            BytesPending += bytesWritten;
 061        }
 62
 63        private void WriteNumberValueIndented(decimal value)
 064        {
 065            int indent = Indentation;
 066            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 67
 068            int maxRequired = indent + JsonConstants.MaximumFormatDecimalLength + 1 + _newLineLength; // Optionally, 1 l
 69
 070            if (_memory.Length - BytesPending < maxRequired)
 071            {
 072                Grow(maxRequired);
 073            }
 74
 075            Span<byte> output = _memory.Span;
 76
 077            if (_currentDepth < 0)
 078            {
 079                output[BytesPending++] = JsonConstants.ListSeparator;
 080            }
 81
 082            if (_tokenType != JsonTokenType.PropertyName)
 083            {
 084                if (_tokenType != JsonTokenType.None)
 085                {
 086                    WriteNewLine(output);
 087                }
 088                WriteIndentation(output.Slice(BytesPending), indent);
 089                BytesPending += indent;
 090            }
 91
 092            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 093            Debug.Assert(result);
 094            BytesPending += bytesWritten;
 095        }
 96
 97        internal void WriteNumberValueAsString(decimal value)
 098        {
 099            Span<byte> utf8Number = stackalloc byte[JsonConstants.MaximumFormatDecimalLength];
 0100            bool result = Utf8Formatter.TryFormat(value, utf8Number, out int bytesWritten);
 0101            Debug.Assert(result);
 0102            WriteNumberValueAsStringUnescaped(utf8Number.Slice(0, bytesWritten));
 0103        }
 104    }
 105}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Double.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7using System.Globalization;
 8
 9namespace System.Text.Json
 10{
 11    public sealed partial class Utf8JsonWriter
 12    {
 13        /// <summary>
 14        /// Writes the <see cref="double"/> value (as a JSON number) as an element of a JSON array.
 15        /// </summary>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="double"/> using the default <see cref="StandardFormat"/> on .NET Core 3 or higher
 22        /// and 'G17' on any other framework.
 23        /// </remarks>
 24        public void WriteNumberValue(double value)
 025        {
 026            JsonWriterHelper.ValidateDouble(value);
 27
 028            if (!_options.SkipValidation)
 029            {
 030                ValidateWritingValue();
 031            }
 32
 033            if (_options.Indented)
 034            {
 035                WriteNumberValueIndented(value);
 036            }
 37            else
 038            {
 039                WriteNumberValueMinimized(value);
 040            }
 41
 042            SetFlagToAddListSeparatorBeforeNextItem();
 043            _tokenType = JsonTokenType.Number;
 044        }
 45
 46        private void WriteNumberValueMinimized(double value)
 047        {
 048            int maxRequired = JsonConstants.MaximumFormatDoubleLength + 1; // Optionally, 1 list separator
 49
 050            if (_memory.Length - BytesPending < maxRequired)
 051            {
 052                Grow(maxRequired);
 053            }
 54
 055            Span<byte> output = _memory.Span;
 56
 057            if (_currentDepth < 0)
 058            {
 059                output[BytesPending++] = JsonConstants.ListSeparator;
 060            }
 61
 062            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 063            Debug.Assert(result);
 064            BytesPending += bytesWritten;
 065        }
 66
 67        private void WriteNumberValueIndented(double value)
 068        {
 069            int indent = Indentation;
 070            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 71
 072            int maxRequired = indent + JsonConstants.MaximumFormatDoubleLength + 1 + _newLineLength; // Optionally, 1 li
 73
 074            if (_memory.Length - BytesPending < maxRequired)
 075            {
 076                Grow(maxRequired);
 077            }
 78
 079            Span<byte> output = _memory.Span;
 80
 081            if (_currentDepth < 0)
 082            {
 083                output[BytesPending++] = JsonConstants.ListSeparator;
 084            }
 85
 086            if (_tokenType != JsonTokenType.PropertyName)
 087            {
 088                if (_tokenType != JsonTokenType.None)
 089                {
 090                    WriteNewLine(output);
 091                }
 092                WriteIndentation(output.Slice(BytesPending), indent);
 093                BytesPending += indent;
 094            }
 95
 096            bool result = TryFormatDouble(value, output.Slice(BytesPending), out int bytesWritten);
 097            Debug.Assert(result);
 098            BytesPending += bytesWritten;
 099        }
 100
 101        private static bool TryFormatDouble(double value, Span<byte> destination, out int bytesWritten)
 0102        {
 103            // Frameworks that are not .NET Core 3.0 or higher do not produce roundtrippable strings by
 104            // default. Further, the Utf8Formatter on older frameworks does not support taking a precision
 105            // specifier for 'G' nor does it represent other formats such as 'R'. As such, we duplicate
 106            // the .NET Core 3.0 logic of forwarding to the UTF16 formatter and transcoding it back to UTF8,
 107            // with some additional changes to remove dependencies on Span APIs which don't exist downlevel.
 108
 109#if NET
 0110            return Utf8Formatter.TryFormat(value, destination, out bytesWritten);
 111#else
 112            string utf16Text = value.ToString(JsonConstants.DoubleFormatString, CultureInfo.InvariantCulture);
 113
 114            // Copy the value to the destination, if it's large enough.
 115
 116            if (utf16Text.Length > destination.Length)
 117            {
 118                bytesWritten = 0;
 119                return false;
 120            }
 121
 122            try
 123            {
 124                byte[] bytes = Encoding.UTF8.GetBytes(utf16Text);
 125
 126                if (bytes.Length > destination.Length)
 127                {
 128                    bytesWritten = 0;
 129                    return false;
 130                }
 131
 132                bytes.CopyTo(destination);
 133                bytesWritten = bytes.Length;
 134
 135                return true;
 136            }
 137            catch
 138            {
 139                bytesWritten = 0;
 140                return false;
 141            }
 142#endif
 0143        }
 144
 145        internal void WriteNumberValueAsString(double value)
 0146        {
 0147            Span<byte> utf8Number = stackalloc byte[JsonConstants.MaximumFormatDoubleLength];
 0148            bool result = TryFormatDouble(value, utf8Number, out int bytesWritten);
 0149            Debug.Assert(result);
 0150            WriteNumberValueAsStringUnescaped(utf8Number.Slice(0, bytesWritten));
 0151        }
 152
 153        internal void WriteFloatingPointConstant(double value)
 0154        {
 0155            if (double.IsNaN(value))
 0156            {
 0157                WriteNumberValueAsStringUnescaped(JsonConstants.NaNValue);
 0158            }
 0159            else if (double.IsPositiveInfinity(value))
 0160            {
 0161                WriteNumberValueAsStringUnescaped(JsonConstants.PositiveInfinityValue);
 0162            }
 0163            else if (double.IsNegativeInfinity(value))
 0164            {
 0165                WriteNumberValueAsStringUnescaped(JsonConstants.NegativeInfinityValue);
 0166            }
 167            else
 0168            {
 0169                WriteNumberValue(value);
 0170            }
 0171        }
 172    }
 173}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Float.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7using System.Globalization;
 8
 9namespace System.Text.Json
 10{
 11    public sealed partial class Utf8JsonWriter
 12    {
 13        /// <summary>
 14        /// Writes the <see cref="float"/> value (as a JSON number) as an element of a JSON array.
 15        /// </summary>
 16        /// <param name="value">The value to write.</param>
 17        /// <exception cref="InvalidOperationException">
 18        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 19        /// </exception>
 20        /// <remarks>
 21        /// Writes the <see cref="float"/> using the default <see cref="StandardFormat"/> on .NET Core 3 or higher
 22        /// and 'G9' on any other framework.
 23        /// </remarks>
 24        public void WriteNumberValue(float value)
 025        {
 026            JsonWriterHelper.ValidateSingle(value);
 27
 028            if (!_options.SkipValidation)
 029            {
 030                ValidateWritingValue();
 031            }
 32
 033            if (_options.Indented)
 034            {
 035                WriteNumberValueIndented(value);
 036            }
 37            else
 038            {
 039                WriteNumberValueMinimized(value);
 040            }
 41
 042            SetFlagToAddListSeparatorBeforeNextItem();
 043            _tokenType = JsonTokenType.Number;
 044        }
 45
 46        private void WriteNumberValueMinimized(float value)
 047        {
 048            int maxRequired = JsonConstants.MaximumFormatSingleLength + 1; // Optionally, 1 list separator
 49
 050            if (_memory.Length - BytesPending < maxRequired)
 051            {
 052                Grow(maxRequired);
 053            }
 54
 055            Span<byte> output = _memory.Span;
 56
 057            if (_currentDepth < 0)
 058            {
 059                output[BytesPending++] = JsonConstants.ListSeparator;
 060            }
 61
 062            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 063            Debug.Assert(result);
 064            BytesPending += bytesWritten;
 065        }
 66
 67        private void WriteNumberValueIndented(float value)
 068        {
 069            int indent = Indentation;
 070            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 71
 072            int maxRequired = indent + JsonConstants.MaximumFormatSingleLength + 1 + _newLineLength; // Optionally, 1 li
 73
 074            if (_memory.Length - BytesPending < maxRequired)
 075            {
 076                Grow(maxRequired);
 077            }
 78
 079            Span<byte> output = _memory.Span;
 80
 081            if (_currentDepth < 0)
 082            {
 083                output[BytesPending++] = JsonConstants.ListSeparator;
 084            }
 85
 086            if (_tokenType != JsonTokenType.PropertyName)
 087            {
 088                if (_tokenType != JsonTokenType.None)
 089                {
 090                    WriteNewLine(output);
 091                }
 092                WriteIndentation(output.Slice(BytesPending), indent);
 093                BytesPending += indent;
 094            }
 95
 096            bool result = TryFormatSingle(value, output.Slice(BytesPending), out int bytesWritten);
 097            Debug.Assert(result);
 098            BytesPending += bytesWritten;
 099        }
 100
 101        private static bool TryFormatSingle(float value, Span<byte> destination, out int bytesWritten)
 0102        {
 103            // Frameworks that are not .NET Core 3.0 or higher do not produce roundtrippable strings by
 104            // default. Further, the Utf8Formatter on older frameworks does not support taking a precision
 105            // specifier for 'G' nor does it represent other formats such as 'R'. As such, we duplicate
 106            // the .NET Core 3.0 logic of forwarding to the UTF16 formatter and transcoding it back to UTF8,
 107            // with some additional changes to remove dependencies on Span APIs which don't exist downlevel.
 108
 109#if NET
 0110            return Utf8Formatter.TryFormat(value, destination, out bytesWritten);
 111#else
 112            string utf16Text = value.ToString(JsonConstants.SingleFormatString, CultureInfo.InvariantCulture);
 113
 114            // Copy the value to the destination, if it's large enough.
 115
 116            if (utf16Text.Length > destination.Length)
 117            {
 118                bytesWritten = 0;
 119                return false;
 120            }
 121
 122            try
 123            {
 124                byte[] bytes = Encoding.UTF8.GetBytes(utf16Text);
 125
 126                if (bytes.Length > destination.Length)
 127                {
 128                    bytesWritten = 0;
 129                    return false;
 130                }
 131
 132                bytes.CopyTo(destination);
 133                bytesWritten = bytes.Length;
 134
 135                return true;
 136            }
 137            catch
 138            {
 139                bytesWritten = 0;
 140                return false;
 141            }
 142#endif
 0143        }
 144
 145        internal void WriteNumberValueAsString(float value)
 0146        {
 0147            Span<byte> utf8Number = stackalloc byte[JsonConstants.MaximumFormatSingleLength];
 0148            bool result = TryFormatSingle(value, utf8Number, out int bytesWritten);
 0149            Debug.Assert(result);
 0150            WriteNumberValueAsStringUnescaped(utf8Number.Slice(0, bytesWritten));
 0151        }
 152
 153        internal void WriteFloatingPointConstant(float value)
 0154        {
 0155            if (float.IsNaN(value))
 0156            {
 0157                WriteNumberValueAsStringUnescaped(JsonConstants.NaNValue);
 0158            }
 0159            else if (float.IsPositiveInfinity(value))
 0160            {
 0161                WriteNumberValueAsStringUnescaped(JsonConstants.PositiveInfinityValue);
 0162            }
 0163            else if (float.IsNegativeInfinity(value))
 0164            {
 0165                WriteNumberValueAsStringUnescaped(JsonConstants.NegativeInfinityValue);
 0166            }
 167            else
 0168            {
 0169                WriteNumberValue(value);
 0170            }
 0171        }
 172    }
 173}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.FormattedNumber.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    public sealed partial class Utf8JsonWriter
 10    {
 11        /// <summary>
 12        /// Writes the value (as a JSON number) as an element of a JSON array.
 13        /// </summary>
 14        /// <param name="utf8FormattedNumber">The value to write.</param>
 15        /// <exception cref="ArgumentException">
 16        /// Thrown when <paramref name="utf8FormattedNumber"/> does not represent a valid JSON number.
 17        /// </exception>
 18        /// <exception cref="InvalidOperationException">
 19        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 20        /// </exception>
 21        /// <remarks>
 22        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 23        /// </remarks>
 24        internal void WriteNumberValue(ReadOnlySpan<byte> utf8FormattedNumber)
 2825        {
 2826            JsonWriterHelper.ValidateValue(utf8FormattedNumber);
 2827            JsonWriterHelper.ValidateNumber(utf8FormattedNumber);
 28
 2829            if (!_options.SkipValidation)
 2830            {
 2831                ValidateWritingValue();
 2832            }
 33
 2834            if (_options.Indented)
 035            {
 036                WriteNumberValueIndented(utf8FormattedNumber);
 037            }
 38            else
 2839            {
 2840                WriteNumberValueMinimized(utf8FormattedNumber);
 2841            }
 42
 2843            SetFlagToAddListSeparatorBeforeNextItem();
 2844            _tokenType = JsonTokenType.Number;
 2845        }
 46
 47        private void WriteNumberValueMinimized(ReadOnlySpan<byte> utf8Value)
 2848        {
 2849            int maxRequired = utf8Value.Length + 1; // Optionally, 1 list separator
 50
 2851            if (_memory.Length - BytesPending < maxRequired)
 2852            {
 2853                Grow(maxRequired);
 2854            }
 55
 2856            Span<byte> output = _memory.Span;
 57
 2858            if (_currentDepth < 0)
 059            {
 060                output[BytesPending++] = JsonConstants.ListSeparator;
 061            }
 62
 2863            utf8Value.CopyTo(output.Slice(BytesPending));
 2864            BytesPending += utf8Value.Length;
 2865        }
 66
 67        private void WriteNumberValueIndented(ReadOnlySpan<byte> utf8Value)
 068        {
 069            int indent = Indentation;
 070            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 71
 072            Debug.Assert(utf8Value.Length < int.MaxValue - indent - 1 - _newLineLength);
 73
 074            int maxRequired = indent + utf8Value.Length + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 by
 75
 076            if (_memory.Length - BytesPending < maxRequired)
 077            {
 078                Grow(maxRequired);
 079            }
 80
 081            Span<byte> output = _memory.Span;
 82
 083            if (_currentDepth < 0)
 084            {
 085                output[BytesPending++] = JsonConstants.ListSeparator;
 086            }
 87
 088            if (_tokenType != JsonTokenType.PropertyName)
 089            {
 090                if (_tokenType != JsonTokenType.None)
 091                {
 092                    WriteNewLine(output);
 093                }
 094                WriteIndentation(output.Slice(BytesPending), indent);
 095                BytesPending += indent;
 096            }
 97
 098            utf8Value.CopyTo(output.Slice(BytesPending));
 099            BytesPending += utf8Value.Length;
 0100        }
 101    }
 102}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Guid.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the <see cref="Guid"/> value (as a JSON string) as an element of a JSON array.
 14        /// </summary>
 15        /// <param name="value">The value 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        /// <remarks>
 20        /// Writes the <see cref="Guid"/> using the default <see cref="StandardFormat"/> (that is, 'D'), as the form: nn
 21        /// </remarks>
 22        public void WriteStringValue(Guid value)
 023        {
 024            if (!_options.SkipValidation)
 025            {
 026                ValidateWritingValue();
 027            }
 28
 029            if (_options.Indented)
 030            {
 031                WriteStringValueIndented(value);
 032            }
 33            else
 034            {
 035                WriteStringValueMinimized(value);
 036            }
 37
 038            SetFlagToAddListSeparatorBeforeNextItem();
 039            _tokenType = JsonTokenType.String;
 040        }
 41
 42        private void WriteStringValueMinimized(Guid value)
 043        {
 044            int maxRequired = JsonConstants.MaximumFormatGuidLength + 3; // 2 quotes, and optionally, 1 list separator
 45
 046            if (_memory.Length - BytesPending < maxRequired)
 047            {
 048                Grow(maxRequired);
 049            }
 50
 051            Span<byte> output = _memory.Span;
 52
 053            if (_currentDepth < 0)
 054            {
 055                output[BytesPending++] = JsonConstants.ListSeparator;
 056            }
 57
 058            output[BytesPending++] = JsonConstants.Quote;
 59
 060            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 061            Debug.Assert(result);
 062            BytesPending += bytesWritten;
 63
 064            output[BytesPending++] = JsonConstants.Quote;
 065        }
 66
 67        private void WriteStringValueIndented(Guid value)
 068        {
 069            int indent = Indentation;
 070            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 71
 72            // 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
 073            int maxRequired = indent + JsonConstants.MaximumFormatGuidLength + 3 + _newLineLength;
 74
 075            if (_memory.Length - BytesPending < maxRequired)
 076            {
 077                Grow(maxRequired);
 078            }
 79
 080            Span<byte> output = _memory.Span;
 81
 082            if (_currentDepth < 0)
 083            {
 084                output[BytesPending++] = JsonConstants.ListSeparator;
 085            }
 86
 087            if (_tokenType != JsonTokenType.PropertyName)
 088            {
 089                if (_tokenType != JsonTokenType.None)
 090                {
 091                    WriteNewLine(output);
 092                }
 093                WriteIndentation(output.Slice(BytesPending), indent);
 094                BytesPending += indent;
 095            }
 96
 097            output[BytesPending++] = JsonConstants.Quote;
 98
 099            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0100            Debug.Assert(result);
 0101            BytesPending += bytesWritten;
 102
 0103            output[BytesPending++] = JsonConstants.Quote;
 0104        }
 105    }
 106}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Helpers.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7using System.Diagnostics.CodeAnalysis;
 8using System.Runtime.CompilerServices;
 9using System.Runtime.InteropServices;
 10
 11namespace System.Text.Json
 12{
 13    public sealed partial class Utf8JsonWriter
 14    {
 15        /// <summary>
 16        /// Returns whether a JSON value can be written at the current position based on the current <see cref="_enclosi
 17        /// <list type="bullet">
 18        /// <item>
 19        /// <see cref="EnclosingContainerType.Array"/>: Writing a value is always allowed.
 20        /// </item>
 21        /// <item>
 22        /// <see cref="EnclosingContainerType.Object"/>: Writing a value is allowed only if <see cref="_tokenType"/> is 
 23        /// Because we designed <see cref="EnclosingContainerType.Object"/> == <see cref="JsonTokenType.PropertyName"/>,
 24        /// </item>
 25        /// <item>
 26        /// <see cref="EnclosingContainerType.None"/>: Writing a value is allowed only if <see cref="_tokenType"/> is No
 27        /// This case is identical to the previous case.
 28        /// </item>
 29        /// <item>
 30        /// <see cref="EnclosingContainerType.Utf8StringSequence"/>, <see cref="EnclosingContainerType.Utf16StringSequen
 31        /// Writing a value is never valid and <see cref="_enclosingContainer"/> does not equal any <see cref="JsonToken
 32        /// </item>
 33        /// </list>
 34        /// This method performs better without short circuiting (this often gets inlined so using simple branch free co
 35        /// </summary>
 2836        private bool CanWriteValue => _enclosingContainer == EnclosingContainerType.Array | (byte)_enclosingContainer ==
 37
 038        private bool HasPartialStringData => _partialStringDataLength != 0;
 39
 040        private void ClearPartialStringData() => _partialStringDataLength = 0;
 41
 42        private void ValidateWritingValue()
 2843        {
 2844            if (!CanWriteValue)
 045            {
 046                OnValidateWritingValueFailed();
 47            }
 2848        }
 49
 50        [DoesNotReturn]
 51        [MethodImpl(MethodImplOptions.NoInlining)]
 52        private void OnValidateWritingValueFailed()
 053        {
 054            Debug.Assert(!_options.SkipValidation);
 55
 056            if (IsWritingPartialString)
 057            {
 058                ThrowInvalidOperationException(ExceptionResource.CannotWriteWithinString);
 59            }
 60
 061            Debug.Assert(!HasPartialStringData);
 62
 063            if (_enclosingContainer == EnclosingContainerType.Object)
 064            {
 065                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 066                Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
 067                ThrowInvalidOperationException(ExceptionResource.CannotWriteValueWithinObject);
 68            }
 69            else
 070            {
 071                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 072                Debug.Assert(CurrentDepth == 0 && _tokenType != JsonTokenType.None);
 073                ThrowInvalidOperationException(ExceptionResource.CannotWriteValueAfterPrimitiveOrClose);
 74            }
 75        }
 76
 77        private void ValidateWritingSegment(EnclosingContainerType currentSegmentEncoding)
 078        {
 079            Debug.Assert(currentSegmentEncoding is EnclosingContainerType.Utf8StringSequence or EnclosingContainerType.U
 80
 81            // A string segment can be written if either:
 82            // 1) The writer is currently in a partial string of the same type. In this case the new segment
 83            // will continue the partial string.
 84            // - or -
 85            // 2) The writer can write a value at the current position, in which case a new string can be started.
 086            if (_enclosingContainer != currentSegmentEncoding && !CanWriteValue)
 087            {
 088                OnValidateWritingSegmentFailed(currentSegmentEncoding);
 89            }
 090        }
 91
 92        [DoesNotReturn]
 93        [MethodImpl(MethodImplOptions.NoInlining)]
 94        private void OnValidateWritingSegmentFailed(EnclosingContainerType currentSegmentEncoding)
 095        {
 096            if (IsWritingPartialString)
 097            {
 098                ThrowHelper.ThrowInvalidOperationException_CannotMixEncodings(_enclosingContainer, currentSegmentEncodin
 99            }
 100
 0101            Debug.Assert(!HasPartialStringData);
 102
 0103            if (_enclosingContainer == EnclosingContainerType.Object)
 0104            {
 0105                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 0106                Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray);
 0107                ThrowInvalidOperationException(ExceptionResource.CannotWriteValueWithinObject);
 108            }
 109            else
 0110            {
 0111                Debug.Assert(_tokenType != JsonTokenType.PropertyName);
 0112                Debug.Assert(CurrentDepth == 0 && _tokenType != JsonTokenType.None);
 0113                ThrowInvalidOperationException(ExceptionResource.CannotWriteValueAfterPrimitiveOrClose);
 114            }
 115        }
 116
 117        private void ValidateNotWithinUnfinalizedString()
 0118        {
 0119            if (IsWritingPartialString)
 0120            {
 0121                ThrowInvalidOperationException(ExceptionResource.CannotWriteWithinString);
 122            }
 123
 0124            Debug.Assert(!HasPartialStringData);
 0125        }
 126
 127        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 128        private void Base64EncodeAndWrite(ReadOnlySpan<byte> bytes, Span<byte> output)
 0129        {
 0130            Span<byte> destination = output.Slice(BytesPending);
 0131            OperationStatus status = Base64.EncodeToUtf8(bytes, destination, out int consumed, out int written);
 0132            Debug.Assert(status == OperationStatus.Done);
 0133            Debug.Assert(consumed == bytes.Length);
 0134            BytesPending += written;
 0135        }
 136    }
 137}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Literal.cs

#LineLine coverage
 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
 4using System.Diagnostics;
 5
 6namespace System.Text.Json
 7{
 8    public sealed partial class Utf8JsonWriter
 9    {
 10        /// <summary>
 11        /// Writes the JSON literal "null" as an element of a JSON array.
 12        /// </summary>
 13        /// <exception cref="InvalidOperationException">
 14        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 15        /// </exception>
 16        public void WriteNullValue()
 017        {
 018            WriteLiteralByOptions(JsonConstants.NullValue);
 19
 020            SetFlagToAddListSeparatorBeforeNextItem();
 021            _tokenType = JsonTokenType.Null;
 022        }
 23
 24        /// <summary>
 25        /// Writes the <see cref="bool"/> value (as a JSON literal "true" or "false") as an element of a JSON array.
 26        /// </summary>
 27        /// <param name="value">The value write.</param>
 28        /// <exception cref="InvalidOperationException">
 29        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 30        /// </exception>
 31        public void WriteBooleanValue(bool value)
 032        {
 033            if (value)
 034            {
 035                WriteLiteralByOptions(JsonConstants.TrueValue);
 036                _tokenType = JsonTokenType.True;
 037            }
 38            else
 039            {
 040                WriteLiteralByOptions(JsonConstants.FalseValue);
 041                _tokenType = JsonTokenType.False;
 042            }
 43
 044            SetFlagToAddListSeparatorBeforeNextItem();
 045        }
 46
 47        private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8Value)
 048        {
 049            if (!_options.SkipValidation)
 050            {
 051                ValidateWritingValue();
 052            }
 53
 054            if (_options.Indented)
 055            {
 056                WriteLiteralIndented(utf8Value);
 057            }
 58            else
 059            {
 060                WriteLiteralMinimized(utf8Value);
 061            }
 062        }
 63
 64        private void WriteLiteralMinimized(ReadOnlySpan<byte> utf8Value)
 065        {
 066            Debug.Assert(utf8Value.Length <= 5);
 67
 068            int maxRequired = utf8Value.Length + 1; // Optionally, 1 list separator
 69
 070            if (_memory.Length - BytesPending < maxRequired)
 071            {
 072                Grow(maxRequired);
 073            }
 74
 075            Span<byte> output = _memory.Span;
 76
 077            if (_currentDepth < 0)
 078            {
 079                output[BytesPending++] = JsonConstants.ListSeparator;
 080            }
 81
 082            utf8Value.CopyTo(output.Slice(BytesPending));
 083            BytesPending += utf8Value.Length;
 084        }
 85
 86        private void WriteLiteralIndented(ReadOnlySpan<byte> utf8Value)
 087        {
 088            int indent = Indentation;
 089            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 090            Debug.Assert(utf8Value.Length <= 5);
 91
 092            int maxRequired = indent + utf8Value.Length + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 by
 93
 094            if (_memory.Length - BytesPending < maxRequired)
 095            {
 096                Grow(maxRequired);
 097            }
 98
 099            Span<byte> output = _memory.Span;
 100
 0101            if (_currentDepth < 0)
 0102            {
 0103                output[BytesPending++] = JsonConstants.ListSeparator;
 0104            }
 105
 0106            if (_tokenType != JsonTokenType.PropertyName)
 0107            {
 0108                if (_tokenType != JsonTokenType.None)
 0109                {
 0110                    WriteNewLine(output);
 0111                }
 0112                WriteIndentation(output.Slice(BytesPending), indent);
 0113                BytesPending += indent;
 0114            }
 115
 0116            utf8Value.CopyTo(output.Slice(BytesPending));
 0117            BytesPending += utf8Value.Length;
 0118        }
 119    }
 120}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Raw.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Diagnostics.CodeAnalysis;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the input as JSON content. It is expected that the input content is a single complete JSON value.
 14        /// </summary>
 15        /// <param name="json">The raw JSON content to write.</param>
 16        /// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</p
 17        /// <exception cref="ArgumentNullException">Thrown if <paramref name="json"/> is <see langword="null"/>.</except
 18        /// <exception cref="ArgumentException">Thrown if the length of the input is zero or greater than 715,827,882 (<
 19        /// <exception cref="JsonException">
 20        /// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
 21        /// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
 22        /// or the input JSON exceeds a recursive depth of 64.
 23        /// </exception>
 24        /// <remarks>
 25        /// When writing untrused JSON values, do not set <paramref name="skipInputValidation"/> to <see langword="true"
 26        /// being written, and/or the overall payload being written to the writer instance being invalid.
 27        ///
 28        /// When using this method, the input content will be written to the writer destination as-is, unless validation
 29        ///
 30        /// The <see cref="JsonWriterOptions.SkipValidation"/> value for the writer instance is honored when using this 
 31        ///
 32        /// The <see cref="JsonWriterOptions.Indented"/> and <see cref="JsonWriterOptions.Encoder"/> values for the writ
 33        /// </remarks>
 34        public void WriteRawValue([StringSyntax(StringSyntaxAttribute.Json)] string json, bool skipInputValidation = fal
 035        {
 036            if (!_options.SkipValidation)
 037            {
 038                ValidateWritingValue();
 039            }
 40
 041            ArgumentNullException.ThrowIfNull(json);
 42
 043            TranscodeAndWriteRawValue(json.AsSpan(), skipInputValidation);
 044        }
 45
 46        /// <summary>
 47        /// Writes the input as JSON content. It is expected that the input content is a single complete JSON value.
 48        /// </summary>
 49        /// <param name="json">The raw JSON content to write.</param>
 50        /// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</p
 51        /// <exception cref="ArgumentException">Thrown if the length of the input is zero or greater than 715,827,882 (<
 52        /// <exception cref="JsonException">
 53        /// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
 54        /// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
 55        /// or the input JSON exceeds a recursive depth of 64.
 56        /// </exception>
 57        /// <remarks>
 58        /// When writing untrused JSON values, do not set <paramref name="skipInputValidation"/> to <see langword="true"
 59        /// being written, and/or the overall payload being written to the writer instance being invalid.
 60        ///
 61        /// When using this method, the input content will be written to the writer destination as-is, unless validation
 62        ///
 63        /// The <see cref="JsonWriterOptions.SkipValidation"/> value for the writer instance is honored when using this 
 64        ///
 65        /// The <see cref="JsonWriterOptions.Indented"/> and <see cref="JsonWriterOptions.Encoder"/> values for the writ
 66        /// </remarks>
 67        public void WriteRawValue([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, bool skipInputVali
 068        {
 069            if (!_options.SkipValidation)
 070            {
 071                ValidateWritingValue();
 072            }
 73
 074            TranscodeAndWriteRawValue(json, skipInputValidation);
 075        }
 76
 77        /// <summary>
 78        /// Writes the input as JSON content. It is expected that the input content is a single complete JSON value.
 79        /// </summary>
 80        /// <param name="utf8Json">The raw JSON content to write.</param>
 81        /// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</p
 82        /// <exception cref="ArgumentException">Thrown if the length of the input is zero or greater than or equal to <s
 83        /// <exception cref="JsonException">
 84        /// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
 85        /// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
 86        /// or the input JSON exceeds a recursive depth of 64.
 87        /// </exception>
 88        /// <remarks>
 89        /// When writing untrused JSON values, do not set <paramref name="skipInputValidation"/> to <see langword="true"
 90        /// being written, and/or the overall payload being written to the writer instance being invalid.
 91        ///
 92        /// When using this method, the input content will be written to the writer destination as-is, unless validation
 93        ///
 94        /// The <see cref="JsonWriterOptions.SkipValidation"/> value for the writer instance is honored when using this 
 95        ///
 96        /// The <see cref="JsonWriterOptions.Indented"/> and <see cref="JsonWriterOptions.Encoder"/> values for the writ
 97        /// </remarks>
 98        public void WriteRawValue(ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false)
 099        {
 0100            if (!_options.SkipValidation)
 0101            {
 0102                ValidateWritingValue();
 0103            }
 104
 0105            if (utf8Json.Length == int.MaxValue)
 0106            {
 0107                ThrowHelper.ThrowArgumentException_ValueTooLarge(int.MaxValue);
 108            }
 109
 0110            WriteRawValueCore(utf8Json, skipInputValidation);
 0111        }
 112
 113        /// <summary>
 114        /// Writes the input as JSON content. It is expected that the input content is a single complete JSON value.
 115        /// </summary>
 116        /// <param name="utf8Json">The raw JSON content to write.</param>
 117        /// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</p
 118        /// <exception cref="ArgumentException">Thrown if the length of the input is zero or equal to <see cref="int.Max
 119        /// <exception cref="JsonException">
 120        /// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
 121        /// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
 122        /// or the input JSON exceeds a recursive depth of 64.
 123        /// </exception>
 124        /// <remarks>
 125        /// When writing untrused JSON values, do not set <paramref name="skipInputValidation"/> to <see langword="true"
 126        /// being written, and/or the overall payload being written to the writer instance being invalid.
 127        ///
 128        /// When using this method, the input content will be written to the writer destination as-is, unless validation
 129        ///
 130        /// The <see cref="JsonWriterOptions.SkipValidation"/> value for the writer instance is honored when using this 
 131        ///
 132        /// The <see cref="JsonWriterOptions.Indented"/> and <see cref="JsonWriterOptions.Encoder"/> values for the writ
 133        /// </remarks>
 134        public void WriteRawValue(ReadOnlySequence<byte> utf8Json, bool skipInputValidation = false)
 0135        {
 0136            if (!_options.SkipValidation)
 0137            {
 0138                ValidateWritingValue();
 0139            }
 140
 0141            long utf8JsonLen = utf8Json.Length;
 142
 0143            if (utf8JsonLen == 0)
 0144            {
 0145                ThrowHelper.ThrowArgumentException(SR.ExpectedJsonTokens);
 146            }
 0147            if (utf8JsonLen >= int.MaxValue)
 0148            {
 0149                ThrowHelper.ThrowArgumentException_ValueTooLarge(utf8JsonLen);
 150            }
 151
 0152            if (skipInputValidation)
 0153            {
 154                // Treat all unvalidated raw JSON value writes as string. If the payload is valid, this approach does
 155                // not affect structural validation since a string token is equivalent to a complete object, array,
 156                // or other complete JSON tokens when considering structural validation on subsequent writer calls.
 157                // If the payload is not valid, then we make no guarantees about the structural validation of the final 
 0158                _tokenType = JsonTokenType.String;
 0159            }
 160            else
 0161            {
 162                // Utilize reader validation.
 0163                Utf8JsonReader reader = new(utf8Json);
 0164                while (reader.Read());
 0165                _tokenType = reader.TokenType;
 0166            }
 167
 0168            Debug.Assert(utf8JsonLen < int.MaxValue);
 0169            int len = (int)utf8JsonLen;
 170
 171            // TODO (https://github.com/dotnet/runtime/issues/29293):
 172            // investigate writing this in chunks, rather than requesting one potentially long, contiguous buffer.
 0173            int maxRequired = len + 1; // Optionally, 1 list separator. We've guarded against integer overflow earlier i
 174
 0175            if (_memory.Length - BytesPending < maxRequired)
 0176            {
 0177                Grow(maxRequired);
 0178            }
 179
 0180            Span<byte> output = _memory.Span;
 181
 0182            if (_currentDepth < 0)
 0183            {
 0184                output[BytesPending++] = JsonConstants.ListSeparator;
 0185            }
 186
 0187            utf8Json.CopyTo(output.Slice(BytesPending));
 0188            BytesPending += len;
 189
 0190            SetFlagToAddListSeparatorBeforeNextItem();
 0191        }
 192
 193        private void TranscodeAndWriteRawValue(ReadOnlySpan<char> json, bool skipInputValidation)
 0194        {
 0195            if (json.Length > JsonConstants.MaxUtf16RawValueLength)
 0196            {
 0197                ThrowHelper.ThrowArgumentException_ValueTooLarge(json.Length);
 198            }
 199
 0200            byte[]? tempArray = null;
 201
 202            // For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
 0203            Span<byte> utf8Json =
 0204                // Use stack memory
 0205                json.Length <= (JsonConstants.StackallocByteThreshold / JsonConstants.MaxExpansionFactorWhileTranscoding
 0206                // Use a pooled array
 0207                json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorW
 0208                // Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per curr
 0209                // and by using a normal alloc we can avoid the Clear().
 0210                new byte[JsonReaderHelper.GetUtf8ByteCount(json)];
 211
 212            try
 0213            {
 0214                int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8Json);
 0215                utf8Json = utf8Json.Slice(0, actualByteCount);
 0216                WriteRawValueCore(utf8Json, skipInputValidation);
 0217            }
 218            finally
 0219            {
 0220                if (tempArray != null)
 0221                {
 0222                    utf8Json.Clear();
 0223                    ArrayPool<byte>.Shared.Return(tempArray);
 0224                }
 0225            }
 0226        }
 227
 228        private void WriteRawValueCore(ReadOnlySpan<byte> utf8Json, bool skipInputValidation)
 0229        {
 0230            int len = utf8Json.Length;
 231
 0232            if (len == 0)
 0233            {
 0234                ThrowHelper.ThrowArgumentException(SR.ExpectedJsonTokens);
 235            }
 236
 237            // In the UTF-16-based entry point methods above, we validate that the payload length <= int.MaxValue /3.
 238            // The result of this division will be rounded down, so even if every input character needs to be transcoded
 239            // (with expansion factor of 3), the resulting payload would be less than int.MaxValue,
 240            // as (int.MaxValue/3) * 3 is less than int.MaxValue.
 0241            Debug.Assert(len < int.MaxValue);
 242
 0243            if (skipInputValidation)
 0244            {
 245                // Treat all unvalidated raw JSON value writes as string. If the payload is valid, this approach does
 246                // not affect structural validation since a string token is equivalent to a complete object, array,
 247                // or other complete JSON tokens when considering structural validation on subsequent writer calls.
 248                // If the payload is not valid, then we make no guarantees about the structural validation of the final 
 0249                _tokenType = JsonTokenType.String;
 0250            }
 251            else
 0252            {
 253                // Utilize reader validation.
 0254                Utf8JsonReader reader = new(utf8Json);
 0255                while (reader.Read());
 0256                _tokenType = reader.TokenType;
 0257            }
 258
 259            // TODO (https://github.com/dotnet/runtime/issues/29293):
 260            // investigate writing this in chunks, rather than requesting one potentially long, contiguous buffer.
 0261            int maxRequired = len + 1; // Optionally, 1 list separator. We've guarded against integer overflow earlier i
 262
 0263            if (_memory.Length - BytesPending < maxRequired)
 0264            {
 0265                Grow(maxRequired);
 0266            }
 267
 0268            Span<byte> output = _memory.Span;
 269
 0270            if (_currentDepth < 0)
 0271            {
 0272                output[BytesPending++] = JsonConstants.ListSeparator;
 0273            }
 274
 0275            utf8Json.CopyTo(output.Slice(BytesPending));
 0276            BytesPending += len;
 277
 0278            SetFlagToAddListSeparatorBeforeNextItem();
 0279        }
 280    }
 281}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the <see cref="int"/> value (as a JSON number) as an element of a JSON array.
 14        /// </summary>
 15        /// <param name="value">The value 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        /// <remarks>
 20        /// Writes the <see cref="int"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 327
 21        /// </remarks>
 22        public void WriteNumberValue(int value)
 023            => WriteNumberValue((long)value);
 24
 25        /// <summary>
 26        /// Writes the <see cref="long"/> value (as a JSON number) as an element of a JSON array.
 27        /// </summary>
 28        /// <param name="value">The value to write.</param>
 29        /// <exception cref="InvalidOperationException">
 30        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 31        /// </exception>
 32        /// <remarks>
 33        /// Writes the <see cref="long"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 34        /// </remarks>
 35        public void WriteNumberValue(long value)
 036        {
 037            if (!_options.SkipValidation)
 038            {
 039                ValidateWritingValue();
 040            }
 41
 042            if (_options.Indented)
 043            {
 044                WriteNumberValueIndented(value);
 045            }
 46            else
 047            {
 048                WriteNumberValueMinimized(value);
 049            }
 50
 051            SetFlagToAddListSeparatorBeforeNextItem();
 052            _tokenType = JsonTokenType.Number;
 053        }
 54
 55        private void WriteNumberValueMinimized(long value)
 056        {
 057            int maxRequired = JsonConstants.MaximumFormatInt64Length + 1; // Optionally, 1 list separator
 58
 059            if (_memory.Length - BytesPending < maxRequired)
 060            {
 061                Grow(maxRequired);
 062            }
 63
 064            Span<byte> output = _memory.Span;
 65
 066            if (_currentDepth < 0)
 067            {
 068                output[BytesPending++] = JsonConstants.ListSeparator;
 069            }
 70
 071            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 072            Debug.Assert(result);
 073            BytesPending += bytesWritten;
 074        }
 75
 76        private void WriteNumberValueIndented(long value)
 077        {
 078            int indent = Indentation;
 079            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 80
 081            int maxRequired = indent + JsonConstants.MaximumFormatInt64Length + 1 + _newLineLength; // Optionally, 1 lis
 82
 083            if (_memory.Length - BytesPending < maxRequired)
 084            {
 085                Grow(maxRequired);
 086            }
 87
 088            Span<byte> output = _memory.Span;
 89
 090            if (_currentDepth < 0)
 091            {
 092                output[BytesPending++] = JsonConstants.ListSeparator;
 093            }
 94
 095            if (_tokenType != JsonTokenType.PropertyName)
 096            {
 097                if (_tokenType != JsonTokenType.None)
 098                {
 099                    WriteNewLine(output);
 0100                }
 0101                WriteIndentation(output.Slice(BytesPending), indent);
 0102                BytesPending += indent;
 0103            }
 104
 0105            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0106            Debug.Assert(result);
 0107            BytesPending += bytesWritten;
 0108        }
 109
 110        internal void WriteNumberValueAsString(long value)
 0111        {
 0112            Span<byte> utf8Number = stackalloc byte[JsonConstants.MaximumFormatInt64Length];
 0113            bool result = Utf8Formatter.TryFormat(value, utf8Number, out int bytesWritten);
 0114            Debug.Assert(result);
 0115            WriteNumberValueAsStringUnescaped(utf8Number.Slice(0, bytesWritten));
 0116        }
 117    }
 118}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Diagnostics;
 6
 7namespace System.Text.Json
 8{
 9    public sealed partial class Utf8JsonWriter
 10    {
 11        /// <summary>
 12        /// Writes the pre-encoded text value (as a JSON string) as an element of a JSON array.
 13        /// </summary>
 14        /// <param name="value">The JSON-encoded value to write.</param>
 15        /// <exception cref="InvalidOperationException">
 16        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 17        /// </exception>
 18        public void WriteStringValue(JsonEncodedText value)
 019        {
 020            ReadOnlySpan<byte> utf8Value = value.EncodedUtf8Bytes;
 021            Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize);
 22
 023            WriteStringByOptions(utf8Value);
 24
 025            SetFlagToAddListSeparatorBeforeNextItem();
 026            _tokenType = JsonTokenType.String;
 027        }
 28
 29        /// <summary>
 30        /// Writes the string text value (as a JSON string) as an element of a JSON array.
 31        /// </summary>
 32        /// <param name="value">The value to write.</param>
 33        /// <exception cref="ArgumentException">
 34        /// Thrown when the specified value is too large.
 35        /// </exception>
 36        /// <exception cref="InvalidOperationException">
 37        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 38        /// </exception>
 39        /// <remarks>
 40        /// <para>
 41        /// The value is escaped before writing.</para>
 42        /// <para>
 43        /// If <paramref name="value"/> is <see langword="null"/> the JSON null value is written,
 44        /// as if <see cref="WriteNullValue"/> was called.
 45        /// </para>
 46        /// </remarks>
 47        public void WriteStringValue(string? value)
 048        {
 049            if (value == null)
 050            {
 051                WriteNullValue();
 052            }
 53            else
 054            {
 055                WriteStringValue(value.AsSpan());
 056            }
 057        }
 58
 59        /// <summary>
 60        /// Writes the text value (as a JSON string) as an element of a JSON array.
 61        /// </summary>
 62        /// <param name="value">The value to write.</param>
 63        /// <exception cref="ArgumentException">
 64        /// Thrown when the specified value is too large.
 65        /// </exception>
 66        /// <exception cref="InvalidOperationException">
 67        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 68        /// </exception>
 69        /// <remarks>
 70        /// The value is escaped before writing.
 71        /// </remarks>
 72        public void WriteStringValue(ReadOnlySpan<char> value)
 073        {
 074            JsonWriterHelper.ValidateValue(value);
 75
 076            WriteStringEscape(value);
 77
 078            SetFlagToAddListSeparatorBeforeNextItem();
 079            _tokenType = JsonTokenType.String;
 080        }
 81
 82        private void WriteStringEscape(ReadOnlySpan<char> value)
 083        {
 084            int valueIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
 85
 086            Debug.Assert(valueIdx >= -1 && valueIdx < value.Length);
 87
 088            if (valueIdx != -1)
 089            {
 090                WriteStringEscapeValue(value, valueIdx);
 091            }
 92            else
 093            {
 094                WriteStringByOptions(value);
 095            }
 096        }
 97
 98        private void WriteStringByOptions(ReadOnlySpan<char> value)
 099        {
 0100            if (!_options.SkipValidation)
 0101            {
 0102                ValidateWritingValue();
 0103            }
 104
 0105            if (_options.Indented)
 0106            {
 0107                WriteStringIndented(value);
 0108            }
 109            else
 0110            {
 0111                WriteStringMinimized(value);
 0112            }
 0113        }
 114
 115        // TODO: https://github.com/dotnet/runtime/issues/29293
 116        private void WriteStringMinimized(ReadOnlySpan<char> escapedValue)
 0117        {
 0118            Debug.Assert(escapedValue.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - 3);
 119
 120            // All ASCII, 2 quotes => escapedValue.Length + 2
 121            // Optionally, 1 list separator, and up to 3x growth when transcoding
 0122            int maxRequired = (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 3;
 123
 0124            if (_memory.Length - BytesPending < maxRequired)
 0125            {
 0126                Grow(maxRequired);
 0127            }
 128
 0129            Span<byte> output = _memory.Span;
 130
 0131            if (_currentDepth < 0)
 0132            {
 0133                output[BytesPending++] = JsonConstants.ListSeparator;
 0134            }
 0135            output[BytesPending++] = JsonConstants.Quote;
 136
 0137            TranscodeAndWrite(escapedValue, output);
 138
 0139            output[BytesPending++] = JsonConstants.Quote;
 0140        }
 141
 142        // TODO: https://github.com/dotnet/runtime/issues/29293
 143        private void WriteStringIndented(ReadOnlySpan<char> escapedValue)
 0144        {
 0145            int indent = Indentation;
 0146            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 147
 0148            Debug.Assert(escapedValue.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - inden
 149
 150            // All ASCII, 2 quotes => indent + escapedValue.Length + 2
 151            // Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
 0152            int maxRequired = indent + (escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + 3 + _n
 153
 0154            if (_memory.Length - BytesPending < maxRequired)
 0155            {
 0156                Grow(maxRequired);
 0157            }
 158
 0159            Span<byte> output = _memory.Span;
 160
 0161            if (_currentDepth < 0)
 0162            {
 0163                output[BytesPending++] = JsonConstants.ListSeparator;
 0164            }
 165
 0166            if (_tokenType != JsonTokenType.PropertyName)
 0167            {
 0168                if (_tokenType != JsonTokenType.None)
 0169                {
 0170                    WriteNewLine(output);
 0171                }
 0172                WriteIndentation(output.Slice(BytesPending), indent);
 0173                BytesPending += indent;
 0174            }
 175
 0176            output[BytesPending++] = JsonConstants.Quote;
 177
 0178            TranscodeAndWrite(escapedValue, output);
 179
 0180            output[BytesPending++] = JsonConstants.Quote;
 0181        }
 182
 183        private void WriteStringEscapeValue(ReadOnlySpan<char> value, int firstEscapeIndexVal)
 0184        {
 0185            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length);
 0186            Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < value.Length);
 187
 0188            char[]? valueArray = null;
 189
 0190            int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal);
 191
 0192            Span<char> escapedValue = length <= JsonConstants.StackallocCharThreshold ?
 0193                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0194                (valueArray = ArrayPool<char>.Shared.Rent(length));
 195
 0196            JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written);
 197
 0198            WriteStringByOptions(escapedValue.Slice(0, written));
 199
 0200            if (valueArray != null)
 0201            {
 0202                ArrayPool<char>.Shared.Return(valueArray);
 0203            }
 0204        }
 205
 206        /// <summary>
 207        /// Writes the UTF-8 text value (as a JSON string) as an element of a JSON array.
 208        /// </summary>
 209        /// <param name="utf8Value">The UTF-8 encoded value to be written as a JSON string element of a JSON array.</par
 210        /// <exception cref="ArgumentException">
 211        /// Thrown when the specified value is too large.
 212        /// </exception>
 213        /// <exception cref="InvalidOperationException">
 214        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 215        /// </exception>
 216        /// <remarks>
 217        /// The value is escaped before writing.
 218        /// </remarks>
 219        public void WriteStringValue(ReadOnlySpan<byte> utf8Value)
 0220        {
 0221            JsonWriterHelper.ValidateValue(utf8Value);
 222
 0223            WriteStringEscape(utf8Value);
 224
 0225            SetFlagToAddListSeparatorBeforeNextItem();
 0226            _tokenType = JsonTokenType.String;
 0227        }
 228
 229        private void WriteStringEscape(ReadOnlySpan<byte> utf8Value)
 0230        {
 0231            int valueIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
 232
 0233            Debug.Assert(valueIdx >= -1 && valueIdx < utf8Value.Length);
 234
 0235            if (valueIdx != -1)
 0236            {
 0237                WriteStringEscapeValue(utf8Value, valueIdx);
 0238            }
 239            else
 0240            {
 0241                WriteStringByOptions(utf8Value);
 0242            }
 0243        }
 244
 245        private void WriteStringByOptions(ReadOnlySpan<byte> utf8Value)
 0246        {
 0247            if (!_options.SkipValidation)
 0248            {
 0249                ValidateWritingValue();
 0250            }
 251
 0252            if (_options.Indented)
 0253            {
 0254                WriteStringIndented(utf8Value);
 0255            }
 256            else
 0257            {
 0258                WriteStringMinimized(utf8Value);
 0259            }
 0260        }
 261
 262        // TODO: https://github.com/dotnet/runtime/issues/29293
 263        private void WriteStringMinimized(ReadOnlySpan<byte> escapedValue)
 0264        {
 0265            Debug.Assert(escapedValue.Length < int.MaxValue - 3);
 266
 0267            int minRequired = escapedValue.Length + 2; // 2 quotes
 0268            int maxRequired = minRequired + 1; // Optionally, 1 list separator
 269
 0270            if (_memory.Length - BytesPending < maxRequired)
 0271            {
 0272                Grow(maxRequired);
 0273            }
 274
 0275            Span<byte> output = _memory.Span;
 276
 0277            if (_currentDepth < 0)
 0278            {
 0279                output[BytesPending++] = JsonConstants.ListSeparator;
 0280            }
 0281            output[BytesPending++] = JsonConstants.Quote;
 282
 0283            escapedValue.CopyTo(output.Slice(BytesPending));
 0284            BytesPending += escapedValue.Length;
 285
 0286            output[BytesPending++] = JsonConstants.Quote;
 0287        }
 288
 289        // TODO: https://github.com/dotnet/runtime/issues/29293
 290        private void WriteStringIndented(ReadOnlySpan<byte> escapedValue)
 0291        {
 0292            int indent = Indentation;
 0293            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 294
 0295            Debug.Assert(escapedValue.Length < int.MaxValue - indent - 3 - _newLineLength);
 296
 0297            int minRequired = indent + escapedValue.Length + 2; // 2 quotes
 0298            int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new li
 299
 0300            if (_memory.Length - BytesPending < maxRequired)
 0301            {
 0302                Grow(maxRequired);
 0303            }
 304
 0305            Span<byte> output = _memory.Span;
 306
 0307            if (_currentDepth < 0)
 0308            {
 0309                output[BytesPending++] = JsonConstants.ListSeparator;
 0310            }
 311
 0312            if (_tokenType != JsonTokenType.PropertyName)
 0313            {
 0314                if (_tokenType != JsonTokenType.None)
 0315                {
 0316                    WriteNewLine(output);
 0317                }
 0318                WriteIndentation(output.Slice(BytesPending), indent);
 0319                BytesPending += indent;
 0320            }
 321
 0322            output[BytesPending++] = JsonConstants.Quote;
 323
 0324            escapedValue.CopyTo(output.Slice(BytesPending));
 0325            BytesPending += escapedValue.Length;
 326
 0327            output[BytesPending++] = JsonConstants.Quote;
 0328        }
 329
 330        private void WriteStringEscapeValue(ReadOnlySpan<byte> utf8Value, int firstEscapeIndexVal)
 0331        {
 0332            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length);
 0333            Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length);
 334
 0335            byte[]? valueArray = null;
 336
 0337            int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
 338
 0339            Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
 0340                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0341                (valueArray = ArrayPool<byte>.Shared.Rent(length));
 342
 0343            JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int writte
 344
 0345            WriteStringByOptions(escapedValue.Slice(0, written));
 346
 0347            if (valueArray != null)
 0348            {
 0349                ArrayPool<byte>.Shared.Return(valueArray);
 0350            }
 0351        }
 352
 353        /// <summary>
 354        /// Writes a number as a JSON string. The string value is not escaped.
 355        /// </summary>
 356        /// <param name="utf8Value"></param>
 357        internal void WriteNumberValueAsStringUnescaped(ReadOnlySpan<byte> utf8Value)
 0358        {
 359            // The value has been validated prior to calling this method.
 360
 0361            WriteStringByOptions(utf8Value);
 362
 0363            SetFlagToAddListSeparatorBeforeNextItem();
 0364            _tokenType = JsonTokenType.String;
 0365        }
 366    }
 367}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.StringSegment.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the text value segment as a partial JSON string.
 14        /// </summary>
 15        /// <param name="value">The value to write.</param>
 16        /// <param name="isFinalSegment">Indicates that this is the final segment of the string.</param>
 17        /// <exception cref="ArgumentException">
 18        /// Thrown when the specified value is too large.
 19        /// </exception>
 20        /// <exception cref="InvalidOperationException">
 21        /// Thrown when validation is enabled if this would result in invalid JSON being written or
 22        /// if the previously written segment (if any) was not written with this same overload.
 23        /// </exception>
 24        /// <remarks>
 25        /// The value is escaped before writing.
 26        /// </remarks>
 27        public void WriteStringValueSegment(ReadOnlySpan<char> value, bool isFinalSegment)
 028        {
 029            JsonWriterHelper.ValidateValue(value);
 30
 031            if (!_options.SkipValidation)
 032            {
 033                ValidateWritingSegment(EnclosingContainerType.Utf16StringSequence);
 034            }
 35
 036            if (_enclosingContainer != EnclosingContainerType.Utf16StringSequence)
 037            {
 038                WriteStringSegmentPrologue();
 039                _enclosingContainer = EnclosingContainerType.Utf16StringSequence;
 040            }
 41
 42            // The steps to write a string segment are to complete the previous partial code point
 43            // and escape either of which might not be required so there is a fast path for each of these steps.
 044            if (HasPartialStringData)
 045            {
 046                WriteStringSegmentWithLeftover(value, isFinalSegment);
 047            }
 48            else
 049            {
 050                WriteStringSegmentEscape(value, isFinalSegment);
 051            }
 52
 053            if (isFinalSegment)
 054            {
 055                WriteStringSegmentEpilogue();
 56
 057                SetFlagToAddListSeparatorBeforeNextItem();
 058                EnclosingContainerType container = _bitStack.Peek() ? EnclosingContainerType.Object : EnclosingContainer
 059                _enclosingContainer = _bitStack.CurrentDepth == 0 ? EnclosingContainerType.None : container;
 060                _tokenType = JsonTokenType.String;
 061            }
 062        }
 63
 64        private void WriteStringSegmentWithLeftover(scoped ReadOnlySpan<char> value, bool isFinalSegment)
 065        {
 066            Debug.Assert(HasPartialStringData);
 067            Debug.Assert(_enclosingContainer == EnclosingContainerType.Utf16StringSequence);
 68
 069            scoped ReadOnlySpan<char> partialStringDataBuffer = PartialUtf16StringData;
 70
 071            Span<char> combinedBuffer = stackalloc char[2];
 072            combinedBuffer = combinedBuffer.Slice(0, ConcatInto(partialStringDataBuffer, value, combinedBuffer));
 73
 074            switch (Rune.DecodeFromUtf16(combinedBuffer, out _, out int charsConsumed))
 75            {
 76                case OperationStatus.NeedMoreData:
 077                    Debug.Assert(value.Length + partialStringDataBuffer.Length < 2);
 078                    Debug.Assert(charsConsumed == value.Length + partialStringDataBuffer.Length);
 79                    // Let the encoder deal with the error if this is a final buffer.
 080                    value = combinedBuffer.Slice(0, charsConsumed);
 081                    partialStringDataBuffer = [];
 082                    break;
 83                case OperationStatus.Done:
 084                    Debug.Assert(charsConsumed > partialStringDataBuffer.Length);
 085                    Debug.Assert(charsConsumed <= 2);
 86                    // Divide up the code point chars into its own buffer and the remainder of the input buffer.
 087                    value = value.Slice(charsConsumed - partialStringDataBuffer.Length);
 088                    partialStringDataBuffer = combinedBuffer.Slice(0, charsConsumed);
 089                    break;
 90                case OperationStatus.InvalidData:
 091                    Debug.Assert(charsConsumed >= partialStringDataBuffer.Length);
 092                    Debug.Assert(charsConsumed <= 2);
 093                    value = value.Slice(charsConsumed - partialStringDataBuffer.Length);
 094                    partialStringDataBuffer = combinedBuffer.Slice(0, charsConsumed);
 095                    break;
 96                case OperationStatus.DestinationTooSmall:
 97                default:
 098                    Debug.Fail("Unexpected OperationStatus return value.");
 99                    break;
 100            }
 101
 102            // The "isFinalSegment" argument indicates whether input that NeedsMoreData should be consumed as an error o
 103            // Because we have validated above that partialStringDataBuffer will be the next consumed chars during Rune 
 104            // (even if this is because it is invalid), we should pass isFinalSegment = true to indicate to the decoder 
 105            // parse the code units without extra data.
 106            //
 107            // This is relevant in the case of having ['\uD800', 'C'], where the validation above would have needed both
 108            // to determine that only the first unit should be consumed (as invalid). So this method will get only ['\uD
 109            // Because we know more data will not be able to complete this code point, we need to pass isFinalSegment = 
 110            // to ensure that the encoder consumes this data eagerly instead of leaving it and returning NeedsMoreData.
 0111            WriteStringSegmentEscape(partialStringDataBuffer, true);
 112
 0113            ClearPartialStringData();
 114
 0115            WriteStringSegmentEscape(value, isFinalSegment);
 0116        }
 117
 118        private void WriteStringSegmentEscape(ReadOnlySpan<char> value, bool isFinalSegment)
 0119        {
 0120            if (value.IsEmpty) return;
 121
 0122            int escapeIdx = JsonWriterHelper.NeedsEscaping(value, _options.Encoder);
 0123            if (escapeIdx != -1)
 0124            {
 0125                WriteStringSegmentEscapeValue(value, escapeIdx, isFinalSegment);
 0126            }
 127            else
 0128            {
 0129                WriteStringSegmentData(value);
 0130            }
 0131        }
 132
 133        private void WriteStringSegmentEscapeValue(ReadOnlySpan<char> value, int firstEscapeIndexVal, bool isFinalSegmen
 0134        {
 0135            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= value.Length);
 0136            Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < value.Length);
 137
 0138            char[]? valueArray = null;
 139
 0140            int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal);
 141
 0142            Span<char> escapedValue = length <= JsonConstants.StackallocCharThreshold ?
 0143                stackalloc char[JsonConstants.StackallocCharThreshold] :
 0144                (valueArray = ArrayPool<char>.Shared.Rent(length));
 145
 0146            JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int consumed, 
 147
 0148            WriteStringSegmentData(escapedValue.Slice(0, written));
 149
 0150            Debug.Assert(consumed == value.Length || !isFinalSegment);
 0151            if (value.Length != consumed)
 0152            {
 0153                Debug.Assert(!isFinalSegment);
 0154                Debug.Assert(value.Length - consumed < 2);
 0155                PartialUtf16StringData = value.Slice(consumed);
 0156            }
 157
 0158            if (valueArray != null)
 0159            {
 0160                ArrayPool<char>.Shared.Return(valueArray);
 0161            }
 0162        }
 163
 164        private void WriteStringSegmentData(ReadOnlySpan<char> escapedValue)
 0165        {
 0166            Debug.Assert(escapedValue.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding));
 167
 0168            int requiredBytes = escapedValue.Length * JsonConstants.MaxExpansionFactorWhileTranscoding;
 169
 0170            if (_memory.Length - BytesPending < requiredBytes)
 0171            {
 0172                Grow(requiredBytes);
 0173            }
 174
 0175            Span<byte> output = _memory.Span;
 176
 0177            TranscodeAndWrite(escapedValue, output);
 0178        }
 179
 180        /// <summary>
 181        /// Writes the UTF-8 text value segment as a partial JSON string.
 182        /// </summary>
 183        /// <param name="value">The UTF-8 encoded value to be written as a JSON string element of a JSON array.</param>
 184        /// <param name="isFinalSegment">Indicates that this is the final segment of the string.</param>
 185        /// <exception cref="ArgumentException">
 186        /// Thrown when the specified value is too large.
 187        /// </exception>
 188        /// <exception cref="InvalidOperationException">
 189        /// Thrown when validation is enabled if this would result in invalid JSON being written or
 190        /// if the previously written segment (if any) was not written with this same overload.
 191        /// </exception>
 192        /// <remarks>
 193        /// The value is escaped before writing.
 194        /// </remarks>
 195        public void WriteStringValueSegment(ReadOnlySpan<byte> value, bool isFinalSegment)
 0196        {
 0197            JsonWriterHelper.ValidateValue(value);
 198
 0199            if (!_options.SkipValidation)
 0200            {
 0201                ValidateWritingSegment(EnclosingContainerType.Utf8StringSequence);
 0202            }
 203
 0204            if (_enclosingContainer != EnclosingContainerType.Utf8StringSequence)
 0205            {
 0206                WriteStringSegmentPrologue();
 0207                _enclosingContainer = EnclosingContainerType.Utf8StringSequence;
 0208            }
 209
 210            // The steps to write a string segment are to complete the previous partial code point
 211            // and escape either of which might not be required so there is a fast path for each of these steps.
 0212            if (HasPartialStringData)
 0213            {
 0214                WriteStringSegmentWithLeftover(value, isFinalSegment);
 0215            }
 216            else
 0217            {
 0218                WriteStringSegmentEscape(value, isFinalSegment);
 0219            }
 220
 0221            if (isFinalSegment)
 0222            {
 0223                WriteStringSegmentEpilogue();
 224
 0225                SetFlagToAddListSeparatorBeforeNextItem();
 0226                EnclosingContainerType container = _bitStack.Peek() ? EnclosingContainerType.Object : EnclosingContainer
 0227                _enclosingContainer = _bitStack.CurrentDepth == 0 ? EnclosingContainerType.None : container;
 0228                _tokenType = JsonTokenType.String;
 0229            }
 0230        }
 231
 232        private void WriteStringSegmentWithLeftover(scoped ReadOnlySpan<byte> utf8Value, bool isFinalSegment)
 0233        {
 0234            Debug.Assert(HasPartialStringData);
 0235            Debug.Assert(_enclosingContainer == EnclosingContainerType.Utf8StringSequence);
 236
 0237            scoped ReadOnlySpan<byte> partialStringDataBuffer = PartialUtf8StringData;
 238
 0239            Span<byte> combinedBuffer = stackalloc byte[4];
 0240            combinedBuffer = combinedBuffer.Slice(0, ConcatInto(partialStringDataBuffer, utf8Value, combinedBuffer));
 241
 0242            switch (Rune.DecodeFromUtf8(combinedBuffer, out _, out int bytesConsumed))
 243            {
 244                case OperationStatus.NeedMoreData:
 0245                    Debug.Assert(utf8Value.Length + partialStringDataBuffer.Length < 4);
 0246                    Debug.Assert(bytesConsumed == utf8Value.Length + partialStringDataBuffer.Length);
 247                    // Let the encoder deal with the error if this is a final buffer.
 0248                    utf8Value = combinedBuffer.Slice(0, bytesConsumed);
 0249                    partialStringDataBuffer = [];
 0250                    break;
 251                case OperationStatus.Done:
 0252                    Debug.Assert(bytesConsumed > partialStringDataBuffer.Length);
 0253                    Debug.Assert(bytesConsumed <= 4);
 254                    // Divide up the code point bytes into its own buffer and the remainder of the input buffer.
 0255                    utf8Value = utf8Value.Slice(bytesConsumed - partialStringDataBuffer.Length);
 0256                    partialStringDataBuffer = combinedBuffer.Slice(0, bytesConsumed);
 0257                    break;
 258                case OperationStatus.InvalidData:
 0259                    Debug.Assert(bytesConsumed >= partialStringDataBuffer.Length);
 0260                    Debug.Assert(bytesConsumed <= 4);
 0261                    utf8Value = utf8Value.Slice(bytesConsumed - partialStringDataBuffer.Length);
 0262                    partialStringDataBuffer = combinedBuffer.Slice(0, bytesConsumed);
 0263                    break;
 264                case OperationStatus.DestinationTooSmall:
 265                default:
 0266                    Debug.Fail("Unexpected OperationStatus return value.");
 267                    break;
 268            }
 269
 270            // The "isFinalSegment" argument indicates whether input that NeedsMoreData should be consumed as an error o
 271            // Because we have validated above that partialStringDataBuffer will be the next consumed bytes during Rune 
 272            // (even if this is because it is invalid), we should pass isFinalSegment = true to indicate to the decoder 
 273            // parse the code units without extra data.
 274            //
 275            // This is relevant in the case of having [<3-length prefix code unit>, <continuation>, <ascii>], where the 
 276            // above would have needed all 3 code units to determine that only the first 2 units should be consumed (as 
 277            // So this method will get only <3-size prefix code unit><continuation>. Because we know more data will not 
 278            // to complete this code point, we need to pass isFinalSegment = true to ensure that the encoder consumes th
 279            // instead of leaving it and returning NeedsMoreData.
 0280            WriteStringSegmentEscape(partialStringDataBuffer, true);
 281
 0282            ClearPartialStringData();
 283
 0284            WriteStringSegmentEscape(utf8Value, isFinalSegment);
 0285        }
 286
 287        private void WriteStringSegmentEscape(ReadOnlySpan<byte> utf8Value, bool isFinalSegment)
 0288        {
 0289            if (utf8Value.IsEmpty) return;
 290
 0291            int escapeIdx = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder);
 0292            if (escapeIdx != -1)
 0293            {
 0294                WriteStringSegmentEscapeValue(utf8Value, escapeIdx, isFinalSegment);
 0295            }
 296            else
 0297            {
 0298                WriteStringSegmentData(utf8Value);
 0299            }
 0300        }
 301
 302        private void WriteStringSegmentEscapeValue(ReadOnlySpan<byte> utf8Value, int firstEscapeIndexVal, bool isFinalSe
 0303        {
 0304            Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8Value.Length);
 0305            Debug.Assert(firstEscapeIndexVal >= 0 && firstEscapeIndexVal < utf8Value.Length);
 0306            byte[]? valueArray = null;
 0307            int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal);
 0308            Span<byte> escapedValue = length <= JsonConstants.StackallocByteThreshold ?
 0309                stackalloc byte[JsonConstants.StackallocByteThreshold] :
 0310                (valueArray = ArrayPool<byte>.Shared.Rent(length));
 311
 0312            JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int consum
 313
 0314            WriteStringSegmentData(escapedValue.Slice(0, written));
 315
 0316            Debug.Assert(consumed == utf8Value.Length || !isFinalSegment);
 0317            if (utf8Value.Length != consumed)
 0318            {
 0319                Debug.Assert(!isFinalSegment);
 0320                Debug.Assert(utf8Value.Length - consumed < 4);
 0321                PartialUtf8StringData = utf8Value.Slice(consumed);
 0322            }
 323
 0324            if (valueArray != null)
 0325            {
 0326                ArrayPool<byte>.Shared.Return(valueArray);
 0327            }
 0328        }
 329
 330        private void WriteStringSegmentData(ReadOnlySpan<byte> escapedValue)
 0331        {
 0332            Debug.Assert(escapedValue.Length < int.MaxValue - 3);
 333
 0334            int requiredBytes = escapedValue.Length;
 335
 0336            if (_memory.Length - BytesPending < requiredBytes)
 0337            {
 0338                Grow(requiredBytes);
 0339            }
 340
 0341            Span<byte> output = _memory.Span;
 342
 0343            escapedValue.CopyTo(output.Slice(BytesPending));
 0344            BytesPending += escapedValue.Length;
 0345        }
 346
 347        /// <summary>
 348        /// Writes the input bytes as a partial JSON string.
 349        /// </summary>
 350        /// <param name="value">The bytes to be written as a JSON string element of a JSON array.</param>
 351        /// <param name="isFinalSegment">Indicates that this is the final segment of the string.</param>
 352        /// <exception cref="ArgumentException">
 353        /// Thrown when the specified value is too large.
 354        /// </exception>
 355        /// <exception cref="InvalidOperationException">
 356        /// Thrown when validation is enabled if this would result in invalid JSON being written or
 357        /// if the previously written segment (if any) was not written with this same overload.
 358        /// </exception>
 359        public void WriteBase64StringSegment(ReadOnlySpan<byte> value, bool isFinalSegment)
 0360        {
 0361            if (value.Length > Base64.GetMaxDecodedFromUtf8Length(int.MaxValue))
 0362            {
 0363                ThrowHelper.ThrowArgumentException_ValueTooLarge(value.Length);
 364            }
 365
 0366            if (!_options.SkipValidation)
 0367            {
 0368                ValidateWritingSegment(EnclosingContainerType.Base64StringSequence);
 0369            }
 370
 0371            if (_enclosingContainer != EnclosingContainerType.Base64StringSequence)
 0372            {
 0373                WriteStringSegmentPrologue();
 0374                _enclosingContainer = EnclosingContainerType.Base64StringSequence;
 0375            }
 376
 377            // The steps to write a string segment are to complete the previous partial string data
 378            // and escape either of which might not be required so there is a fast path for each of these steps.
 0379            if (HasPartialStringData)
 0380            {
 0381                WriteBase64StringSegmentWithLeftover(value, isFinalSegment);
 0382            }
 383            else
 0384            {
 0385                WriteBase64StringSegmentData(value, isFinalSegment);
 0386            }
 387
 0388            if (isFinalSegment)
 0389            {
 0390                WriteStringSegmentEpilogue();
 391
 0392                SetFlagToAddListSeparatorBeforeNextItem();
 0393                EnclosingContainerType container = _bitStack.Peek() ? EnclosingContainerType.Object : EnclosingContainer
 0394                _enclosingContainer = _bitStack.CurrentDepth == 0 ? EnclosingContainerType.None : container;
 0395                _tokenType = JsonTokenType.String;
 0396            }
 0397        }
 398
 399        private void WriteBase64StringSegmentWithLeftover(scoped ReadOnlySpan<byte> bytes, bool isFinalSegment)
 0400        {
 0401            Debug.Assert(HasPartialStringData);
 0402            Debug.Assert(_enclosingContainer == EnclosingContainerType.Base64StringSequence);
 403
 0404            scoped ReadOnlySpan<byte> partialStringDataBuffer = PartialBase64StringData;
 405
 0406            Span<byte> combinedBuffer = stackalloc byte[3];
 0407            combinedBuffer = combinedBuffer.Slice(0, ConcatInto(partialStringDataBuffer, bytes, combinedBuffer));
 0408            if (combinedBuffer.Length is 3)
 0409            {
 410                // Divide up the partial bytes into its own buffer and the remainder of the input buffer.
 0411                bytes = bytes.Slice(3 - partialStringDataBuffer.Length);
 0412                partialStringDataBuffer = combinedBuffer.Slice(0, 3);
 0413            }
 414            else
 0415            {
 0416                Debug.Assert(combinedBuffer.Length is 1 or 2);
 417                // Need more data. If this is a final segment, then the encoder will append '=' as needed.
 0418                Debug.Assert(bytes.Length + partialStringDataBuffer.Length < 3);
 0419                bytes = combinedBuffer;
 0420                partialStringDataBuffer = [];
 0421            }
 422
 423            // It doesn't matter if we pass true or false for isFinalSegment since we are guaranteed to not have partial
 424            // here (it is either empty or completed using the combined buffer above).
 0425            WriteBase64StringSegmentData(partialStringDataBuffer, false);
 426
 0427            ClearPartialStringData();
 428
 0429            WriteBase64StringSegmentData(bytes, isFinalSegment);
 0430        }
 431
 432        private void WriteBase64StringSegmentData(ReadOnlySpan<byte> bytes, bool isFinalSegment)
 0433        {
 434            int leftoverSize;
 0435            if (!isFinalSegment && (leftoverSize = bytes.Length % 3) != 0)
 0436            {
 437                // If this is not the final segment, we need to wait for more data to come in.
 0438                PartialBase64StringData = bytes.Slice(bytes.Length - leftoverSize);
 0439                bytes = bytes.Slice(0, bytes.Length - leftoverSize);
 0440            }
 441
 0442            if (bytes.Length == 0)
 0443            {
 0444                return;
 445            }
 446
 0447            int requiredBytes = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
 448
 0449            if (_memory.Length - BytesPending < requiredBytes)
 0450            {
 0451                Grow(requiredBytes);
 0452            }
 453
 0454            Span<byte> output = _memory.Span;
 455
 456            // For non-final segments, the input is sliced to be a multiple of 3 bytes above which guarantees
 457            // that the base64 encoding will never end with padding since 3x input bytes turn into exactly 4x base64 byt
 0458            Base64EncodeAndWrite(bytes, output);
 0459        }
 460
 461        private void WriteStringSegmentPrologue()
 0462        {
 0463            if (_options.Indented)
 0464            {
 0465                WriteStringSegmentIndentedPrologue();
 0466            }
 467            else
 0468            {
 0469                WriteStringSegmentMinimizedPrologue();
 0470            }
 0471        }
 472
 473        private void WriteStringSegmentIndentedPrologue()
 0474        {
 0475            int indent = Indentation;
 0476            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 477
 478            // One quote and optionally 1 indent, 1 list separator and 1-2 bytes for new line
 0479            int bytesRequired = 1 + indent + 1 + _newLineLength;
 0480            if (_memory.Length - BytesPending < bytesRequired)
 0481            {
 0482                Grow(bytesRequired);
 0483            }
 484
 0485            Span<byte> output = _memory.Span;
 486
 0487            if (_currentDepth < 0)
 0488            {
 0489                output[BytesPending++] = JsonConstants.ListSeparator;
 0490            }
 491
 0492            if (_tokenType != JsonTokenType.PropertyName)
 0493            {
 0494                if (_tokenType != JsonTokenType.None)
 0495                {
 0496                    WriteNewLine(output);
 0497                }
 0498                WriteIndentation(output.Slice(BytesPending), indent);
 0499                BytesPending += indent;
 0500            }
 501
 0502            output[BytesPending++] = JsonConstants.Quote;
 0503        }
 504
 505        private void WriteStringSegmentMinimizedPrologue()
 0506        {
 507            // One quote and optionally 1 list separator
 0508            int bytesRequired = 2;
 0509            if (_memory.Length - BytesPending < bytesRequired)
 0510            {
 0511                Grow(bytesRequired);
 0512            }
 513
 0514            Span<byte> output = _memory.Span;
 515
 0516            if (_currentDepth < 0)
 0517            {
 0518                output[BytesPending++] = JsonConstants.ListSeparator;
 0519            }
 520
 0521            output[BytesPending++] = JsonConstants.Quote;
 0522        }
 523
 524        private void WriteStringSegmentEpilogue()
 0525        {
 0526            if (_memory.Length == BytesPending)
 0527            {
 0528                Grow(1);
 0529            }
 530
 0531            _memory.Span[BytesPending++] = JsonConstants.Quote;
 0532        }
 533
 534        /// <summary>
 535        /// Given a byte buffer <paramref name="dest"/>, concatenates as much of <paramref name="srcLeft"/> followed
 536        /// by <paramref name="srcRight"/> into it as will fit, then returns the total number of bytes copied.
 537        /// </summary>
 538        private static int ConcatInto<T>(ReadOnlySpan<T> srcLeft, ReadOnlySpan<T> srcRight, Span<T> dest)
 0539        {
 0540            int total = 0;
 0541            for (int i = 0; i < srcLeft.Length; i++)
 0542            {
 0543                if ((uint)total >= (uint)dest.Length)
 0544                {
 0545                    goto Finish;
 546                }
 547                else
 0548                {
 0549                    dest[total++] = srcLeft[i];
 0550                }
 0551            }
 0552            for (int i = 0; i < srcRight.Length; i++)
 0553            {
 0554                if ((uint)total >= (uint)dest.Length)
 0555                {
 0556                    goto Finish;
 557                }
 558                else
 0559                {
 0560                    dest[total++] = srcRight[i];
 0561                }
 0562            }
 0563        Finish:
 0564            return total;
 0565        }
 566    }
 567}

C:\h\w\B31A098C\w\BB5A0A33\e\runtime-utils\Runner\runtime\src\libraries\System.Text.Json\src\System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs

#LineLine coverage
 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
 4using System.Buffers;
 5using System.Buffers.Text;
 6using System.Diagnostics;
 7
 8namespace System.Text.Json
 9{
 10    public sealed partial class Utf8JsonWriter
 11    {
 12        /// <summary>
 13        /// Writes the <see cref="uint"/> value (as a JSON number) as an element of a JSON array.
 14        /// </summary>
 15        /// <param name="value">The value 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        /// <remarks>
 20        /// Writes the <see cref="uint"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 32
 21        /// </remarks>
 22        [CLSCompliant(false)]
 23        public void WriteNumberValue(uint value)
 024            => WriteNumberValue((ulong)value);
 25
 26        /// <summary>
 27        /// Writes the <see cref="ulong"/> value (as a JSON number) as an element of a JSON array.
 28        /// </summary>
 29        /// <param name="value">The value to write.</param>
 30        /// <exception cref="InvalidOperationException">
 31        /// Thrown if this would result in invalid JSON being written (while validation is enabled).
 32        /// </exception>
 33        /// <remarks>
 34        /// Writes the <see cref="ulong"/> using the default <see cref="StandardFormat"/> (that is, 'G'), for example: 3
 35        /// </remarks>
 36        [CLSCompliant(false)]
 37        public void WriteNumberValue(ulong value)
 038        {
 039            if (!_options.SkipValidation)
 040            {
 041                ValidateWritingValue();
 042            }
 43
 044            if (_options.Indented)
 045            {
 046                WriteNumberValueIndented(value);
 047            }
 48            else
 049            {
 050                WriteNumberValueMinimized(value);
 051            }
 52
 053            SetFlagToAddListSeparatorBeforeNextItem();
 054            _tokenType = JsonTokenType.Number;
 055        }
 56
 57        private void WriteNumberValueMinimized(ulong value)
 058        {
 059            int maxRequired = JsonConstants.MaximumFormatUInt64Length + 1; // Optionally, 1 list separator
 60
 061            if (_memory.Length - BytesPending < maxRequired)
 062            {
 063                Grow(maxRequired);
 064            }
 65
 066            Span<byte> output = _memory.Span;
 67
 068            if (_currentDepth < 0)
 069            {
 070                output[BytesPending++] = JsonConstants.ListSeparator;
 071            }
 72
 073            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 074            Debug.Assert(result);
 075            BytesPending += bytesWritten;
 076        }
 77
 78        private void WriteNumberValueIndented(ulong value)
 079        {
 080            int indent = Indentation;
 081            Debug.Assert(indent <= _indentLength * _options.MaxDepth);
 82
 083            int maxRequired = indent + JsonConstants.MaximumFormatUInt64Length + 1 + _newLineLength; // Optionally, 1 li
 84
 085            if (_memory.Length - BytesPending < maxRequired)
 086            {
 087                Grow(maxRequired);
 088            }
 89
 090            Span<byte> output = _memory.Span;
 91
 092            if (_currentDepth < 0)
 093            {
 094                output[BytesPending++] = JsonConstants.ListSeparator;
 095            }
 96
 097            if (_tokenType != JsonTokenType.PropertyName)
 098            {
 099                if (_tokenType != JsonTokenType.None)
 0100                {
 0101                    WriteNewLine(output);
 0102                }
 0103                WriteIndentation(output.Slice(BytesPending), indent);
 0104                BytesPending += indent;
 0105            }
 106
 0107            bool result = Utf8Formatter.TryFormat(value, output.Slice(BytesPending), out int bytesWritten);
 0108            Debug.Assert(result);
 0109            BytesPending += bytesWritten;
 0110        }
 111
 112        internal void WriteNumberValueAsString(ulong value)
 0113        {
 0114            Span<byte> utf8Number = stackalloc byte[JsonConstants.MaximumFormatUInt64Length];
 0115            bool result = Utf8Formatter.TryFormat(value, utf8Number, out int bytesWritten);
 0116            Debug.Assert(result);
 0117            WriteNumberValueAsStringUnescaped(utf8Number.Slice(0, bytesWritten));
 0118        }
 119    }
 120}

Methods/Properties

PartialStringDataRaw()
BytesPending()
BytesCommitted()
Options()
Indentation()
TokenType()
CurrentDepth()
PartialUtf8StringData()
PartialUtf8StringData(System.ReadOnlySpan`1<System.Byte>)
PartialUtf16StringData()
PartialUtf16StringData(System.ReadOnlySpan`1<System.Char>)
PartialBase64StringData()
PartialBase64StringData(System.ReadOnlySpan`1<System.Byte>)
.ctor()
.ctor(System.Buffers.IBufferWriter`1<System.Byte>,System.Text.Json.JsonWriterOptions)
.ctor(System.IO.Stream,System.Text.Json.JsonWriterOptions)
SetOptions(System.Text.Json.JsonWriterOptions)
Reset()
Reset(System.IO.Stream)
Reset(System.Buffers.IBufferWriter`1<System.Byte>)
ResetAllStateForCacheReuse()
Reset(System.Buffers.IBufferWriter`1<System.Byte>,System.Text.Json.JsonWriterOptions)
CreateEmptyInstanceForCaching()
ResetHelper()
CheckNotDisposed()
Flush()
Dispose()
DisposeAsync()
FlushAsync()
WriteStartArray()
WriteStartObject()
WriteStart(System.Byte)
WriteStartMinimized(System.Byte)
WriteStartSlow(System.Byte)
ValidateStart()
OnValidateStartFailed()
WriteStartIndented(System.Byte)
WriteStartArray(System.Text.Json.JsonEncodedText)
WriteStartObject(System.Text.Json.JsonEncodedText)
WriteStartHelper(System.ReadOnlySpan`1<System.Byte>,System.Byte)
WriteStartArray(System.ReadOnlySpan`1<System.Byte>)
WriteStartObject(System.ReadOnlySpan`1<System.Byte>)
WriteStartEscape(System.ReadOnlySpan`1<System.Byte>,System.Byte)
WriteStartByOptions(System.ReadOnlySpan`1<System.Byte>,System.Byte)
WriteStartEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Byte,System.Int32)
WriteStartArray(System.String)
WriteStartObject(System.String)
WriteStartArray(System.ReadOnlySpan`1<System.Char>)
WriteStartObject(System.ReadOnlySpan`1<System.Char>)
WriteStartEscape(System.ReadOnlySpan`1<System.Char>,System.Byte)
WriteStartByOptions(System.ReadOnlySpan`1<System.Char>,System.Byte)
WriteStartEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Byte,System.Int32)
WriteEndArray()
WriteEndObject()
WriteEnd(System.Byte)
WriteEndMinimized(System.Byte)
WriteEndSlow(System.Byte)
ValidateEnd(System.Byte)
WriteEndIndented(System.Byte)
WriteNewLine(System.Span`1<System.Byte>)
WriteIndentation(System.Span`1<System.Byte>,System.Int32)
UpdateBitStackOnStart(System.Byte)
Grow(System.Int32)
FirstCallToGetMemory(System.Int32)
SetFlagToAddListSeparatorBeforeNextItem()
ThrowInvalidOperationException(System.Text.Json.ExceptionResource)
ThrowInvalidOperationException_MismatchedObjectArray(System.Byte)
ThrowInvalidOperationException_DepthTooLarge()
DebuggerDisplay()
IsWritingPartialString()
WriteBase64String(System.Text.Json.JsonEncodedText,System.ReadOnlySpan`1<System.Byte>)
WriteBase64String(System.String,System.ReadOnlySpan`1<System.Byte>)
WriteBase64String(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64String(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Escape(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Escape(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64EscapeProperty(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteBase64EscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteBase64ByOptions(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64ByOptions(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Minimized(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Minimized(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Indented(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteBase64Indented(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.Text.Json.JsonEncodedText,System.DateTime)
WriteString(System.String,System.DateTime)
WriteString(System.ReadOnlySpan`1<System.Char>,System.DateTime)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.DateTime)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>,System.DateTime)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>,System.DateTime)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.DateTime,System.Int32)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.DateTime,System.Int32)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>,System.DateTime)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>,System.DateTime)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>,System.DateTime)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>,System.DateTime)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>,System.DateTime)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>,System.DateTime)
WritePropertyName(System.DateTime)
WriteString(System.Text.Json.JsonEncodedText,System.DateTimeOffset)
WriteString(System.String,System.DateTimeOffset)
WriteString(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset,System.Int32)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset,System.Int32)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>,System.DateTimeOffset)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>,System.DateTimeOffset)
WritePropertyName(System.DateTimeOffset)
WriteNumber(System.Text.Json.JsonEncodedText,System.Decimal)
WriteNumber(System.String,System.Decimal)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.Decimal)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.Decimal)
WriteNumberEscape(System.ReadOnlySpan`1<System.Char>,System.Decimal)
WriteNumberEscape(System.ReadOnlySpan`1<System.Byte>,System.Decimal)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Decimal,System.Int32)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Decimal,System.Int32)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Char>,System.Decimal)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Byte>,System.Decimal)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Char>,System.Decimal)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Byte>,System.Decimal)
WriteNumberIndented(System.ReadOnlySpan`1<System.Char>,System.Decimal)
WriteNumberIndented(System.ReadOnlySpan`1<System.Byte>,System.Decimal)
WritePropertyName(System.Decimal)
WriteNumber(System.Text.Json.JsonEncodedText,System.Double)
WriteNumber(System.String,System.Double)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.Double)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.Double)
WriteNumberEscape(System.ReadOnlySpan`1<System.Char>,System.Double)
WriteNumberEscape(System.ReadOnlySpan`1<System.Byte>,System.Double)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Double,System.Int32)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Double,System.Int32)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Char>,System.Double)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Byte>,System.Double)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Char>,System.Double)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Byte>,System.Double)
WriteNumberIndented(System.ReadOnlySpan`1<System.Char>,System.Double)
WriteNumberIndented(System.ReadOnlySpan`1<System.Byte>,System.Double)
WritePropertyName(System.Double)
WriteNumber(System.Text.Json.JsonEncodedText,System.Single)
WriteNumber(System.String,System.Single)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.Single)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.Single)
WriteNumberEscape(System.ReadOnlySpan`1<System.Char>,System.Single)
WriteNumberEscape(System.ReadOnlySpan`1<System.Byte>,System.Single)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Single,System.Int32)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Single,System.Int32)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Char>,System.Single)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Byte>,System.Single)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Char>,System.Single)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Byte>,System.Single)
WriteNumberIndented(System.ReadOnlySpan`1<System.Char>,System.Single)
WriteNumberIndented(System.ReadOnlySpan`1<System.Byte>,System.Single)
WritePropertyName(System.Single)
WriteString(System.Text.Json.JsonEncodedText,System.Guid)
WriteString(System.String,System.Guid)
WriteString(System.ReadOnlySpan`1<System.Char>,System.Guid)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.Guid)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>,System.Guid)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>,System.Guid)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Guid,System.Int32)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Guid,System.Int32)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>,System.Guid)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>,System.Guid)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>,System.Guid)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>,System.Guid)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>,System.Guid)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>,System.Guid)
WritePropertyName(System.Guid)
ValidatePropertyNameAndDepth(System.ReadOnlySpan`1<System.Char>)
ValidatePropertyNameAndDepth(System.ReadOnlySpan`1<System.Byte>)
ValidateDepth()
ValidateWritingProperty()
ValidateWritingProperty(System.Byte)
OnValidateWritingPropertyFailed()
WritePropertyNameMinimized(System.ReadOnlySpan`1<System.Byte>,System.Byte)
WritePropertyNameIndented(System.ReadOnlySpan`1<System.Byte>,System.Byte)
WritePropertyNameMinimized(System.ReadOnlySpan`1<System.Char>,System.Byte)
WritePropertyNameIndented(System.ReadOnlySpan`1<System.Char>,System.Byte)
TranscodeAndWrite(System.ReadOnlySpan`1<System.Char>,System.Span`1<System.Byte>)
WriteNull(System.Text.Json.JsonEncodedText)
WriteNullSection(System.ReadOnlySpan`1<System.Byte>)
WriteLiteralHelper(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteNull(System.String)
WriteNull(System.ReadOnlySpan`1<System.Char>)
WriteNull(System.ReadOnlySpan`1<System.Byte>)
WriteBoolean(System.Text.Json.JsonEncodedText,System.Boolean)
WriteBoolean(System.String,System.Boolean)
WriteBoolean(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteBoolean(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteLiteralEscape(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralEscape(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteLiteralEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteLiteralByOptions(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralByOptions(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralMinimized(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralMinimized(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralSection(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralIndented(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteLiteralIndented(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WritePropertyName(System.Boolean)
WriteNumber(System.Text.Json.JsonEncodedText,System.Int64)
WriteNumber(System.String,System.Int64)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.Int64)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.Int64)
WriteNumber(System.Text.Json.JsonEncodedText,System.Int32)
WriteNumber(System.String,System.Int32)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.Int32)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteNumberEscape(System.ReadOnlySpan`1<System.Char>,System.Int64)
WriteNumberEscape(System.ReadOnlySpan`1<System.Byte>,System.Int64)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Int64,System.Int32)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Int64,System.Int32)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Char>,System.Int64)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Byte>,System.Int64)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Char>,System.Int64)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Byte>,System.Int64)
WriteNumberIndented(System.ReadOnlySpan`1<System.Char>,System.Int64)
WriteNumberIndented(System.ReadOnlySpan`1<System.Byte>,System.Int64)
WritePropertyName(System.Int32)
WritePropertyName(System.Int64)
WritePropertyName(System.Text.Json.JsonEncodedText)
WritePropertyNameSection(System.ReadOnlySpan`1<System.Byte>)
WritePropertyNameHelper(System.ReadOnlySpan`1<System.Byte>)
WritePropertyName(System.String)
WritePropertyName(System.ReadOnlySpan`1<System.Char>)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.Int32)
WriteStringByOptionsPropertyName(System.ReadOnlySpan`1<System.Char>)
WriteStringMinimizedPropertyName(System.ReadOnlySpan`1<System.Char>)
WriteStringIndentedPropertyName(System.ReadOnlySpan`1<System.Char>)
WritePropertyName(System.ReadOnlySpan`1<System.Byte>)
WritePropertyNameUnescaped(System.ReadOnlySpan`1<System.Byte>)
WriteStringEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteStringByOptionsPropertyName(System.ReadOnlySpan`1<System.Byte>)
WriteStringMinimizedPropertyName(System.ReadOnlySpan`1<System.Byte>)
WriteStringPropertyNameSection(System.ReadOnlySpan`1<System.Byte>)
WriteStringIndentedPropertyName(System.ReadOnlySpan`1<System.Byte>)
WriteString(System.Text.Json.JsonEncodedText,System.Text.Json.JsonEncodedText)
WriteStringHelper(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.String,System.Text.Json.JsonEncodedText)
WriteString(System.String,System.String)
WriteString(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.Text.Json.JsonEncodedText,System.String)
WriteString(System.Text.Json.JsonEncodedText,System.ReadOnlySpan`1<System.Char>)
WriteStringHelperEscapeValue(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteString(System.String,System.ReadOnlySpan`1<System.Char>)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteString(System.Text.Json.JsonEncodedText,System.ReadOnlySpan`1<System.Byte>)
WriteStringHelperEscapeValue(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.String,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.ReadOnlySpan`1<System.Char>,System.Text.Json.JsonEncodedText)
WriteStringHelperEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.ReadOnlySpan`1<System.Char>,System.String)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.Text.Json.JsonEncodedText)
WriteStringHelperEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteString(System.ReadOnlySpan`1<System.Byte>,System.String)
WriteStringEscapeValueOnly(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteStringEscapeValueOnly(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>,System.Int32)
WriteStringEscapePropertyOnly(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteStringEscapePropertyOnly(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteStringEscapePropertyOrValue(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>,System.Int32,System.Int32)
WriteStringEscapePropertyOrValue(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>,System.Int32,System.Int32)
WriteStringEscapePropertyOrValue(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>,System.Int32,System.Int32)
WriteStringEscapePropertyOrValue(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>,System.Int32,System.Int32)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Char>)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Byte>)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>,System.ReadOnlySpan`1<System.Byte>)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>,System.ReadOnlySpan`1<System.Char>)
WriteNumber(System.Text.Json.JsonEncodedText,System.UInt64)
WriteNumber(System.String,System.UInt64)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.UInt64)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.UInt64)
WriteNumber(System.Text.Json.JsonEncodedText,System.UInt32)
WriteNumber(System.String,System.UInt32)
WriteNumber(System.ReadOnlySpan`1<System.Char>,System.UInt32)
WriteNumber(System.ReadOnlySpan`1<System.Byte>,System.UInt32)
WriteNumberEscape(System.ReadOnlySpan`1<System.Char>,System.UInt64)
WriteNumberEscape(System.ReadOnlySpan`1<System.Byte>,System.UInt64)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Char>,System.UInt64,System.Int32)
WriteNumberEscapeProperty(System.ReadOnlySpan`1<System.Byte>,System.UInt64,System.Int32)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Char>,System.UInt64)
WriteNumberByOptions(System.ReadOnlySpan`1<System.Byte>,System.UInt64)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Char>,System.UInt64)
WriteNumberMinimized(System.ReadOnlySpan`1<System.Byte>,System.UInt64)
WriteNumberIndented(System.ReadOnlySpan`1<System.Char>,System.UInt64)
WriteNumberIndented(System.ReadOnlySpan`1<System.Byte>,System.UInt64)
WritePropertyName(System.UInt32)
WritePropertyName(System.UInt64)
WriteBase64StringValue(System.ReadOnlySpan`1<System.Byte>)
WriteBase64ByOptions(System.ReadOnlySpan`1<System.Byte>)
WriteBase64Minimized(System.ReadOnlySpan`1<System.Byte>)
WriteBase64Indented(System.ReadOnlySpan`1<System.Byte>)
.cctor()
SingleLineCommentDelimiterUtf8()
WriteCommentValue(System.String)
WriteCommentValue(System.ReadOnlySpan`1<System.Char>)
WriteCommentByOptions(System.ReadOnlySpan`1<System.Char>)
WriteCommentMinimized(System.ReadOnlySpan`1<System.Char>)
WriteCommentIndented(System.ReadOnlySpan`1<System.Char>)
WriteCommentValue(System.ReadOnlySpan`1<System.Byte>)
WriteCommentByOptions(System.ReadOnlySpan`1<System.Byte>)
WriteCommentMinimized(System.ReadOnlySpan`1<System.Byte>)
WriteCommentIndented(System.ReadOnlySpan`1<System.Byte>)
WriteStringValue(System.DateTime)
WriteStringValueMinimized(System.DateTime)
WriteStringValueIndented(System.DateTime)
WriteStringValue(System.DateTimeOffset)
WriteStringValueMinimized(System.DateTimeOffset)
WriteStringValueIndented(System.DateTimeOffset)
WriteNumberValue(System.Decimal)
WriteNumberValueMinimized(System.Decimal)
WriteNumberValueIndented(System.Decimal)
WriteNumberValueAsString(System.Decimal)
WriteNumberValue(System.Double)
WriteNumberValueMinimized(System.Double)
WriteNumberValueIndented(System.Double)
TryFormatDouble(System.Double,System.Span`1<System.Byte>,System.Int32&)
WriteNumberValueAsString(System.Double)
WriteFloatingPointConstant(System.Double)
WriteNumberValue(System.Single)
WriteNumberValueMinimized(System.Single)
WriteNumberValueIndented(System.Single)
TryFormatSingle(System.Single,System.Span`1<System.Byte>,System.Int32&)
WriteNumberValueAsString(System.Single)
WriteFloatingPointConstant(System.Single)
WriteNumberValue(System.ReadOnlySpan`1<System.Byte>)
WriteNumberValueMinimized(System.ReadOnlySpan`1<System.Byte>)
WriteNumberValueIndented(System.ReadOnlySpan`1<System.Byte>)
WriteStringValue(System.Guid)
WriteStringValueMinimized(System.Guid)
WriteStringValueIndented(System.Guid)
CanWriteValue()
HasPartialStringData()
ClearPartialStringData()
ValidateWritingValue()
OnValidateWritingValueFailed()
ValidateWritingSegment(System.Text.Json.Utf8JsonWriter/EnclosingContainerType)
OnValidateWritingSegmentFailed(System.Text.Json.Utf8JsonWriter/EnclosingContainerType)
ValidateNotWithinUnfinalizedString()
Base64EncodeAndWrite(System.ReadOnlySpan`1<System.Byte>,System.Span`1<System.Byte>)
WriteNullValue()
WriteBooleanValue(System.Boolean)
WriteLiteralByOptions(System.ReadOnlySpan`1<System.Byte>)
WriteLiteralMinimized(System.ReadOnlySpan`1<System.Byte>)
WriteLiteralIndented(System.ReadOnlySpan`1<System.Byte>)
WriteRawValue(System.String,System.Boolean)
WriteRawValue(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteRawValue(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteRawValue(System.Buffers.ReadOnlySequence`1<System.Byte>,System.Boolean)
TranscodeAndWriteRawValue(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteRawValueCore(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteNumberValue(System.Int32)
WriteNumberValue(System.Int64)
WriteNumberValueMinimized(System.Int64)
WriteNumberValueIndented(System.Int64)
WriteNumberValueAsString(System.Int64)
WriteStringValue(System.Text.Json.JsonEncodedText)
WriteStringValue(System.String)
WriteStringValue(System.ReadOnlySpan`1<System.Char>)
WriteStringEscape(System.ReadOnlySpan`1<System.Char>)
WriteStringByOptions(System.ReadOnlySpan`1<System.Char>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Char>)
WriteStringIndented(System.ReadOnlySpan`1<System.Char>)
WriteStringEscapeValue(System.ReadOnlySpan`1<System.Char>,System.Int32)
WriteStringValue(System.ReadOnlySpan`1<System.Byte>)
WriteStringEscape(System.ReadOnlySpan`1<System.Byte>)
WriteStringByOptions(System.ReadOnlySpan`1<System.Byte>)
WriteStringMinimized(System.ReadOnlySpan`1<System.Byte>)
WriteStringIndented(System.ReadOnlySpan`1<System.Byte>)
WriteStringEscapeValue(System.ReadOnlySpan`1<System.Byte>,System.Int32)
WriteNumberValueAsStringUnescaped(System.ReadOnlySpan`1<System.Byte>)
WriteStringValueSegment(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteStringSegmentWithLeftover(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteStringSegmentEscape(System.ReadOnlySpan`1<System.Char>,System.Boolean)
WriteStringSegmentEscapeValue(System.ReadOnlySpan`1<System.Char>,System.Int32,System.Boolean)
WriteStringSegmentData(System.ReadOnlySpan`1<System.Char>)
WriteStringValueSegment(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteStringSegmentWithLeftover(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteStringSegmentEscape(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteStringSegmentEscapeValue(System.ReadOnlySpan`1<System.Byte>,System.Int32,System.Boolean)
WriteStringSegmentData(System.ReadOnlySpan`1<System.Byte>)
WriteBase64StringSegment(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteBase64StringSegmentWithLeftover(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteBase64StringSegmentData(System.ReadOnlySpan`1<System.Byte>,System.Boolean)
WriteStringSegmentPrologue()
WriteStringSegmentIndentedPrologue()
WriteStringSegmentMinimizedPrologue()
WriteStringSegmentEpilogue()
ConcatInto(System.ReadOnlySpan`1<T>,System.ReadOnlySpan`1<T>,System.Span`1<T>)
WriteNumberValue(System.UInt32)
WriteNumberValue(System.UInt64)
WriteNumberValueMinimized(System.UInt64)
WriteNumberValueIndented(System.UInt64)
WriteNumberValueAsString(System.UInt64)