From f2a55e228b83df6aa6dc215e295bf3da5ab6fc17 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Sun, 18 Jul 2021 12:04:17 -0700 Subject: [PATCH] System.Text.Json: stackalloc constants + misc PR feedback (#55350) * Follow-up feedback from #54186. * Code review. * Code review. * Dedicated constant for the max number of chars permitted to be allocated on the stack. --- .../Document/JsonDocument.TryGetProperty.cs | 6 +- .../System/Text/Json/Document/JsonDocument.cs | 4 +- .../src/System/Text/Json/JsonConstants.cs | 7 +- .../src/System/Text/Json/JsonHelpers.Date.cs | 8 +- .../System/Text/Json/JsonHelpers.Escaping.cs | 8 +- .../Reader/JsonReaderHelper.Unescaping.cs | 28 +++---- .../Text/Json/Reader/JsonReaderHelper.cs | 6 +- .../Reader/Utf8JsonReader.MultiSegment.cs | 4 +- .../System/Text/Json/Reader/Utf8JsonReader.cs | 6 +- .../Converters/Value/TimeSpanConverter.cs | 2 +- .../Utf8JsonWriter.WriteProperties.Bytes.cs | 8 +- ...Utf8JsonWriter.WriteProperties.DateTime.cs | 8 +- ...onWriter.WriteProperties.DateTimeOffset.cs | 8 +- .../Utf8JsonWriter.WriteProperties.Decimal.cs | 8 +- .../Utf8JsonWriter.WriteProperties.Double.cs | 8 +- .../Utf8JsonWriter.WriteProperties.Float.cs | 8 +- ...nWriter.WriteProperties.FormattedNumber.cs | 8 +- .../Utf8JsonWriter.WriteProperties.Guid.cs | 8 +- .../Utf8JsonWriter.WriteProperties.Literal.cs | 8 +- ...JsonWriter.WriteProperties.SignedNumber.cs | 8 +- .../Utf8JsonWriter.WriteProperties.String.cs | 76 +++++++++---------- ...onWriter.WriteProperties.UnsignedNumber.cs | 8 +- .../Utf8JsonWriter.WriteValues.Helpers.cs | 4 +- .../Utf8JsonWriter.WriteValues.String.cs | 8 +- .../System/Text/Json/Writer/Utf8JsonWriter.cs | 8 +- .../CollectionTests.Dictionary.cs | 2 +- .../JsonDateTimeTestData.cs | 5 ++ .../Serialization/PropertyNameTests.cs | 2 +- .../Serialization/ReferenceHandlerTests.cs | 4 +- .../Serialization/Value.ReadTests.cs | 14 ++++ .../Utf8JsonReaderTests.TryGet.Date.cs | 12 +-- .../Utf8JsonReaderTests.TryGet.cs | 12 +-- .../Utf8JsonReaderTests.cs | 1 + 33 files changed, 169 insertions(+), 146 deletions(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.TryGetProperty.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.TryGetProperty.cs index 8d3dc6174fc06..4653acd71c474 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.TryGetProperty.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.TryGetProperty.cs @@ -27,9 +27,9 @@ internal bool TryGetNamedPropertyValue(int index, ReadOnlySpan propertyNam int startIndex = index + DbRow.Size; int endIndex = checked(row.NumberOfRows * DbRow.Size + index); - if (maxBytes < JsonConstants.StackallocThreshold) + if (maxBytes < JsonConstants.StackallocByteThreshold) { - Span utf8Name = stackalloc byte[JsonConstants.StackallocThreshold]; + Span utf8Name = stackalloc byte[JsonConstants.StackallocByteThreshold]; int len = JsonReaderHelper.GetUtf8FromText(propertyName, utf8Name); utf8Name = utf8Name.Slice(0, len); @@ -139,7 +139,7 @@ private bool TryGetNamedPropertyValue( out JsonElement value) { ReadOnlySpan documentSpan = _utf8Json.Span; - Span utf8UnescapedStack = stackalloc byte[JsonConstants.StackallocThreshold]; + Span utf8UnescapedStack = stackalloc byte[JsonConstants.StackallocByteThreshold]; // Move to the row before the EndObject int index = endIndex - DbRow.Size; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs index fb120a49ae72d..26eabb07c1142 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs @@ -298,8 +298,8 @@ internal bool TextEquals(int index, ReadOnlySpan otherText, bool isPropert byte[]? otherUtf8TextArray = null; int length = checked(otherText.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); - Span otherUtf8Text = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[JsonConstants.StackallocThreshold] : + Span otherUtf8Text = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (otherUtf8TextArray = ArrayPool.Shared.Rent(length)); ReadOnlySpan utf16Text = MemoryMarshal.AsBytes(otherText); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs index a2e91cd3b08fc..6a9969e5d8f27 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonConstants.cs @@ -51,7 +51,8 @@ internal static class JsonConstants public const int MaxWriterDepth = 1_000; public const int RemoveFlagsBitMask = 0x7FFFFFFF; - public const int StackallocThreshold = 256; + public const int StackallocByteThreshold = 256; + public const int StackallocCharThreshold = StackallocByteThreshold / 2; // In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped. // For example: '+' becomes '\u0043' @@ -60,7 +61,7 @@ internal static class JsonConstants public const int MaxExpansionFactorWhileEscaping = 6; // In the worst case, a single UTF-16 character could be expanded to 3 UTF-8 bytes. - // Only surrogate pairs expand to 4 UTF-8 bytes but that is a transformation of 2 UTF-16 characters goign to 4 UTF-8 bytes (factor of 2). + // Only surrogate pairs expand to 4 UTF-8 bytes but that is a transformation of 2 UTF-16 characters going to 4 UTF-8 bytes (factor of 2). // All other UTF-16 characters can be represented by either 1 or 2 UTF-8 bytes. public const int MaxExpansionFactorWhileTranscoding = 3; @@ -95,6 +96,8 @@ internal static class JsonConstants public const int MinimumDateTimeParseLength = 10; // YYYY-MM-DD public const int MaximumEscapedDateTimeOffsetParseLength = MaxExpansionFactorWhileEscaping * MaximumDateTimeOffsetParseLength; + public const int MaximumLiteralLength = 5; // Must be able to fit null, true, & false. + // Encoding Helpers public const char HighSurrogateStart = '\ud800'; public const char HighSurrogateEnd = '\udbff'; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs index 7f05dfafc5173..80d4bf1a1d3fd 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Date.cs @@ -51,8 +51,8 @@ public static bool TryParseAsISO(ReadOnlySpan source, out DateTime value) int maxLength = checked(source.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); - Span bytes = maxLength <= JsonConstants.StackallocThreshold - ? stackalloc byte[JsonConstants.StackallocThreshold] + Span bytes = maxLength <= JsonConstants.StackallocByteThreshold + ? stackalloc byte[JsonConstants.StackallocByteThreshold] : new byte[maxLength]; int length = JsonReaderHelper.GetUtf8FromText(source, bytes); @@ -86,8 +86,8 @@ public static bool TryParseAsISO(ReadOnlySpan source, out DateTimeOffset v int maxLength = checked(source.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); - Span bytes = maxLength <= JsonConstants.StackallocThreshold - ? stackalloc byte[JsonConstants.StackallocThreshold] + Span bytes = maxLength <= JsonConstants.StackallocByteThreshold + ? stackalloc byte[JsonConstants.StackallocByteThreshold] : new byte[maxLength]; int length = JsonReaderHelper.GetUtf8FromText(source, bytes); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs b/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs index a248e32735c7c..d8f74ddc09130 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/JsonHelpers.Escaping.cs @@ -37,8 +37,8 @@ public static byte[] EscapeValue( int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedValue = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written); @@ -65,8 +65,8 @@ private static byte[] GetEscapedPropertyNameSection( int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedValue = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs index 0d739225d3499..b539d6d82bb74 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.Unescaping.cs @@ -14,8 +14,8 @@ public static bool TryGetUnescapedBase64Bytes(ReadOnlySpan utf8Source, int { byte[]? unescapedArray = null; - Span utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ? - stackalloc byte[utf8Source.Length] : + Span utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (unescapedArray = ArrayPool.Shared.Rent(utf8Source.Length)); Unescape(utf8Source, utf8Unescaped, idx, out int written); @@ -44,8 +44,8 @@ public static string GetUnescapedString(ReadOnlySpan utf8Source, int idx) int length = utf8Source.Length; byte[]? pooledName = null; - Span utf8Unescaped = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (pooledName = ArrayPool.Shared.Rent(length)); Unescape(utf8Source, utf8Unescaped, idx, out int written); @@ -71,8 +71,8 @@ public static ReadOnlySpan GetUnescapedSpan(ReadOnlySpan utf8Source, int length = utf8Source.Length; byte[]? pooledName = null; - Span utf8Unescaped = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (pooledName = ArrayPool.Shared.Rent(length)); Unescape(utf8Source, utf8Unescaped, idx, out int written); @@ -96,8 +96,8 @@ public static bool UnescapeAndCompare(ReadOnlySpan utf8Source, ReadOnlySpa byte[]? unescapedArray = null; - Span utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocThreshold ? - stackalloc byte[utf8Source.Length] : + Span utf8Unescaped = utf8Source.Length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (unescapedArray = ArrayPool.Shared.Rent(utf8Source.Length)); Unescape(utf8Source, utf8Unescaped, 0, out int written); @@ -127,12 +127,12 @@ public static bool UnescapeAndCompare(ReadOnlySequence utf8Source, ReadOnl int length = checked((int)utf8Source.Length); - Span utf8Unescaped = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span utf8Unescaped = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (unescapedArray = ArrayPool.Shared.Rent(length)); - Span utf8Escaped = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span utf8Escaped = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (escapedArray = ArrayPool.Shared.Rent(length)); utf8Source.CopyTo(utf8Escaped); @@ -174,8 +174,8 @@ public static bool TryDecodeBase64(ReadOnlySpan utf8Unescaped, [NotNullWhe { byte[]? pooledArray = null; - Span byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocThreshold ? - stackalloc byte[utf8Unescaped.Length] : + Span byteSpan = utf8Unescaped.Length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (pooledArray = ArrayPool.Shared.Rent(utf8Unescaped.Length)); OperationStatus status = Base64.DecodeFromUtf8(utf8Unescaped, byteSpan, out int bytesConsumed, out int bytesWritten); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs index 4ba1bff616b27..b9d51eff8dae6 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs @@ -252,7 +252,7 @@ public static bool TryGetEscapedDateTime(ReadOnlySpan source, out DateTime Debug.Assert(backslash != -1); Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); - Span sourceUnescaped = stackalloc byte[source.Length]; + Span sourceUnescaped = stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]; Unescape(source, sourceUnescaped, backslash, out int written); Debug.Assert(written > 0); @@ -277,7 +277,7 @@ public static bool TryGetEscapedDateTimeOffset(ReadOnlySpan source, out Da Debug.Assert(backslash != -1); Debug.Assert(source.Length <= JsonConstants.MaximumEscapedDateTimeOffsetParseLength); - Span sourceUnescaped = stackalloc byte[source.Length]; + Span sourceUnescaped = stackalloc byte[JsonConstants.MaximumEscapedDateTimeOffsetParseLength]; Unescape(source, sourceUnescaped, backslash, out int written); Debug.Assert(written > 0); @@ -303,7 +303,7 @@ public static bool TryGetEscapedGuid(ReadOnlySpan source, out Guid value) int idx = source.IndexOf(JsonConstants.BackSlash); Debug.Assert(idx != -1); - Span utf8Unescaped = stackalloc byte[source.Length]; + Span utf8Unescaped = stackalloc byte[JsonConstants.MaximumEscapedGuidLength]; Unescape(source, utf8Unescaped, idx, out int written); Debug.Assert(written > 0); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs index e639610cc0b39..89f269bdb870a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.MultiSegment.cs @@ -540,9 +540,9 @@ private bool ConsumeLiteralMultiSegment(ReadOnlySpan literal, JsonTokenTyp private bool CheckLiteralMultiSegment(ReadOnlySpan span, ReadOnlySpan literal, out int consumed) { - Debug.Assert(span.Length > 0 && span[0] == literal[0]); + Debug.Assert(span.Length > 0 && span[0] == literal[0] && literal.Length <= JsonConstants.MaximumLiteralLength); - Span readSoFar = stackalloc byte[literal.Length]; + Span readSoFar = stackalloc byte[JsonConstants.MaximumLiteralLength]; int written = 0; long prevTotalConsumed = _totalConsumed; diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs index 5ab5fd9ce8949..efafcf92b4ed2 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs @@ -513,7 +513,7 @@ public bool ValueTextEquals(ReadOnlySpan text) int length = checked(text.Length * JsonConstants.MaxExpansionFactorWhileTranscoding); - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { otherUtf8TextArray = ArrayPool.Shared.Rent(length); otherUtf8Text = otherUtf8TextArray; @@ -523,8 +523,8 @@ public bool ValueTextEquals(ReadOnlySpan text) // Cannot create a span directly since it gets passed to instance methods on a ref struct. unsafe { - byte* ptr = stackalloc byte[JsonConstants.StackallocThreshold]; - otherUtf8Text = new Span(ptr, JsonConstants.StackallocThreshold); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + otherUtf8Text = new Span(ptr, JsonConstants.StackallocByteThreshold); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs index 3767b7ff6d451..ddb3252c9be43 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/TimeSpanConverter.cs @@ -54,7 +54,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso int backslash = source.IndexOf(JsonConstants.BackSlash); Debug.Assert(backslash != -1); - Span sourceUnescaped = stackalloc byte[source.Length]; + Span sourceUnescaped = stackalloc byte[MaximumEscapedTimeSpanFormatLength]; JsonReaderHelper.Unescape(source, sourceUnescaped, backslash, out int written); Debug.Assert(written > 0); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs index 7852c0052001c..55d8aaa515dee 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Bytes.cs @@ -139,8 +139,8 @@ private void WriteBase64EscapeProperty(ReadOnlySpan propertyName, ReadOnly int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -162,8 +162,8 @@ private void WriteBase64EscapeProperty(ReadOnlySpan utf8PropertyName, Read int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs index fa96ffc6e8dc2..e41427cdcdd8c 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTime.cs @@ -144,8 +144,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan propertyName, DateTime int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -167,8 +167,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan utf8PropertyName, Date int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs index 3cbfef7cf4fb5..313496df38c9b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.DateTimeOffset.cs @@ -143,8 +143,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan propertyName, DateTime int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -166,8 +166,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan utf8PropertyName, Date int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs index 861ce7e00599a..ab98e246fab68 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Decimal.cs @@ -143,8 +143,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, decimal int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -166,8 +166,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, deci int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Double.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Double.cs index 359cfd4303676..1fb6832ce6ad3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Double.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Double.cs @@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, double v int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -170,8 +170,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, doub int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Float.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Float.cs index 6e46a4f6efbf8..3832dd4c0cebe 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Float.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Float.cs @@ -147,8 +147,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, float va int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -170,8 +170,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, floa int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.FormattedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.FormattedNumber.cs index e654b5270f16c..44f3c2f6c17cc 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.FormattedNumber.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.FormattedNumber.cs @@ -120,8 +120,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, ReadOnly int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -143,8 +143,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, Read int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Guid.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Guid.cs index 899ff6febf347..07ffde8f49278 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Guid.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Guid.cs @@ -143,8 +143,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan propertyName, Guid val int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -166,8 +166,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan utf8PropertyName, Guid int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Literal.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Literal.cs index ed8e8107145c9..9ab0b654cf97d 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Literal.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Literal.cs @@ -259,8 +259,8 @@ private void WriteLiteralEscapeProperty(ReadOnlySpan propertyName, ReadOnl int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -282,8 +282,8 @@ private void WriteLiteralEscapeProperty(ReadOnlySpan utf8PropertyName, Rea int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.SignedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.SignedNumber.cs index b231446cd6804..05d45181da035 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.SignedNumber.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.SignedNumber.cs @@ -213,8 +213,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, long val int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -236,8 +236,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, long int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs index f838d3fa1936d..38c64b4a400f3 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.String.cs @@ -112,7 +112,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan propertyName, int firs int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocCharThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -122,8 +122,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan propertyName, int firs // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - char* ptr = stackalloc char[length]; - escapedPropertyName = new Span(ptr, length); + char* ptr = stackalloc char[JsonConstants.StackallocCharThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocCharThreshold); } } @@ -274,7 +274,7 @@ private void WriteStringEscapeProperty(ReadOnlySpan utf8PropertyName, int int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -284,8 +284,8 @@ private void WriteStringEscapeProperty(ReadOnlySpan utf8PropertyName, int // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - byte* ptr = stackalloc byte[length]; - escapedPropertyName = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocByteThreshold); } } @@ -896,8 +896,8 @@ private void WriteStringEscapeValueOnly(ReadOnlySpan escapedPropertyName, int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndex); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedValue = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndex, _options.Encoder, out int written); @@ -919,8 +919,8 @@ private void WriteStringEscapeValueOnly(ReadOnlySpan escapedPropertyName, int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndex); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedValue = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndex, _options.Encoder, out int written); @@ -942,8 +942,8 @@ private void WriteStringEscapePropertyOnly(ReadOnlySpan propertyName, Read int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndex); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int written); @@ -965,8 +965,8 @@ private void WriteStringEscapePropertyOnly(ReadOnlySpan utf8PropertyName, int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndex); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndex, _options.Encoder, out int written); @@ -1068,7 +1068,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span escapedValue; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocCharThreshold) { valueArray = ArrayPool.Shared.Rent(length); escapedValue = valueArray; @@ -1078,8 +1078,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - char* ptr = stackalloc char[length]; - escapedValue = new Span(ptr, length); + char* ptr = stackalloc char[JsonConstants.StackallocCharThreshold]; + escapedValue = new Span(ptr, JsonConstants.StackallocCharThreshold); } } @@ -1092,7 +1092,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocCharThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -1102,8 +1102,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - char* ptr = stackalloc char[length]; - escapedPropertyName = new Span(ptr, length); + char* ptr = stackalloc char[JsonConstants.StackallocCharThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocCharThreshold); } } @@ -1137,7 +1137,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span escapedValue; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { valueArray = ArrayPool.Shared.Rent(length); escapedValue = valueArray; @@ -1147,8 +1147,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - byte* ptr = stackalloc byte[length]; - escapedValue = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + escapedValue = new Span(ptr, JsonConstants.StackallocByteThreshold); } } @@ -1161,7 +1161,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -1171,8 +1171,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - byte* ptr = stackalloc byte[length]; - escapedPropertyName = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocByteThreshold); } } @@ -1206,7 +1206,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span escapedValue; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { valueArray = ArrayPool.Shared.Rent(length); escapedValue = valueArray; @@ -1216,8 +1216,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - byte* ptr = stackalloc byte[length]; - escapedValue = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + escapedValue = new Span(ptr, JsonConstants.StackallocByteThreshold); } } @@ -1230,7 +1230,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocCharThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -1240,8 +1240,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan propertyName, R // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - char* ptr = stackalloc char[length]; - escapedPropertyName = new Span(ptr, length); + char* ptr = stackalloc char[JsonConstants.StackallocCharThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocCharThreshold); } } @@ -1275,7 +1275,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span escapedValue; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocCharThreshold) { valueArray = ArrayPool.Shared.Rent(length); escapedValue = valueArray; @@ -1285,8 +1285,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - char* ptr = stackalloc char[length]; - escapedValue = new Span(ptr, length); + char* ptr = stackalloc char[JsonConstants.StackallocCharThreshold]; + escapedValue = new Span(ptr, JsonConstants.StackallocCharThreshold); } } @@ -1299,7 +1299,7 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span escapedPropertyName; - if (length > JsonConstants.StackallocThreshold) + if (length > JsonConstants.StackallocByteThreshold) { propertyArray = ArrayPool.Shared.Rent(length); escapedPropertyName = propertyArray; @@ -1309,8 +1309,8 @@ private void WriteStringEscapePropertyOrValue(ReadOnlySpan utf8PropertyNam // Cannot create a span directly since it gets assigned to parameter and passed down. unsafe { - byte* ptr = stackalloc byte[length]; - escapedPropertyName = new Span(ptr, length); + byte* ptr = stackalloc byte[JsonConstants.StackallocByteThreshold]; + escapedPropertyName = new Span(ptr, JsonConstants.StackallocByteThreshold); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.UnsignedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.UnsignedNumber.cs index 5b6698143f111..b7602d4f481c0 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.UnsignedNumber.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.UnsignedNumber.cs @@ -222,8 +222,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan propertyName, ulong va int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -245,8 +245,8 @@ private void WriteNumberEscapeProperty(ReadOnlySpan utf8PropertyName, ulon int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Helpers.cs index 6e617bdec880c..d95817a638e85 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Helpers.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Helpers.cs @@ -39,8 +39,8 @@ private void Base64EncodeAndWrite(ReadOnlySpan bytes, Span output, i { byte[]? outputText = null; - Span encodedBytes = encodingLength <= JsonConstants.StackallocThreshold ? - stackalloc byte[encodingLength] : + Span encodedBytes = encodingLength <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (outputText = ArrayPool.Shared.Rent(encodingLength)); OperationStatus status = Base64.EncodeToUtf8(bytes, encodedBytes, out int consumed, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.String.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.String.cs index 15cb0b8d1af5e..b4be2dfe8b3bb 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.String.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.String.cs @@ -189,8 +189,8 @@ private void WriteStringEscapeValue(ReadOnlySpan value, int firstEscapeInd int length = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedValue = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written); @@ -336,8 +336,8 @@ private void WriteStringEscapeValue(ReadOnlySpan utf8Value, int firstEscap int length = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); - Span escapedValue = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedValue = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (valueArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8Value, escapedValue, firstEscapeIndexVal, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs index 2c5cc11afbd7a..8c9ae27228b7a 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.cs @@ -662,8 +662,8 @@ private void WriteStartEscapeProperty(ReadOnlySpan utf8PropertyName, byte int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc byte[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ? + stackalloc byte[JsonConstants.StackallocByteThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); @@ -805,8 +805,8 @@ private void WriteStartEscapeProperty(ReadOnlySpan propertyName, byte toke int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); - Span escapedPropertyName = length <= JsonConstants.StackallocThreshold ? - stackalloc char[length] : + Span escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ? + stackalloc char[JsonConstants.StackallocCharThreshold] : (propertyArray = ArrayPool.Shared.Rent(length)); JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written); diff --git a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Dictionary.cs b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Dictionary.cs index 738679e1beb63..61126269362cb 100644 --- a/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Dictionary.cs +++ b/src/libraries/System.Text.Json/tests/Common/CollectionTests/CollectionTests.Dictionary.cs @@ -954,7 +954,7 @@ public async Task UnicodePropertyNames() } { - // We want to go over StackallocThreshold=256 to force a pooled allocation, so this property is 200 chars and 400 bytes. + // We want to go over StackallocByteThreshold=256 to force a pooled allocation, so this property is 200 chars and 400 bytes. const int charsInProperty = 200; string longPropertyName = new string('\u0467', charsInProperty); diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDateTimeTestData.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDateTimeTestData.cs index 8d4efb5e3cb2a..8f4f2af3269c4 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDateTimeTestData.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonDateTimeTestData.cs @@ -232,6 +232,11 @@ public static IEnumerable InvalidISO8601Tests() // High byte expansion - parsing fails early at 254 characters. yield return new object[] { "\"" + new string('\u20AC', 250) + "\"" }; yield return new object[] { "\"" + new string('\u20AC', 260) + "\"" }; + + // Whitespace + yield return new object[] { "\"\\t1997-07-16\"" }; + yield return new object[] { "\"1997-07-16 \"" }; + yield return new object[] { "\" 1997-07-16 \"" }; } public static IEnumerable DateTimeFractionTrimBaseTests() diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PropertyNameTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PropertyNameTests.cs index 33f4f5f2bb5fc..6a14f3ed63cb8 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PropertyNameTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/PropertyNameTests.cs @@ -348,7 +348,7 @@ public static void UnicodePropertyNames() [Fact] public static void UnicodePropertyNamesWithPooledAlloc() { - // We want to go over StackallocThreshold=256 to force a pooled allocation, so this property is 400 chars and 401 bytes. + // We want to go over StackallocByteThreshold=256 to force a pooled allocation, so this property is 400 chars and 401 bytes. ClassWithUnicodeProperty obj = JsonSerializer.Deserialize("{\"A\u046734567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890\":1}"); Assert.Equal(1, obj.A\u046734567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890); diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReferenceHandlerTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReferenceHandlerTests.cs index 7de2c8fa1e243..030f637b9c3f3 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReferenceHandlerTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/ReferenceHandlerTests.cs @@ -209,7 +209,7 @@ public static void UnicodePropertyNames() objCopy = JsonSerializer.Deserialize(json, optionsWithEncoder); Assert.Equal(1, objCopy.A\u0467); - // We want to go over StackallocThreshold=256 to force a pooled allocation, so this property is 400 chars and 401 bytes. + // We want to go over StackallocByteThreshold=256 to force a pooled allocation, so this property is 400 chars and 401 bytes. obj = new ClassWithUnicodeProperty { A\u046734567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 = 1 @@ -398,7 +398,7 @@ public static void UnicodeDictionaryKeys() objCopy = JsonSerializer.Deserialize>(json, optionsWithEncoder); Assert.Equal(1, objCopy["A\u0467"]); - // We want to go over StackallocThreshold=256 to force a pooled allocation, so this property is 200 chars and 400 bytes. + // We want to go over StackallocByteThreshold=256 to force a pooled allocation, so this property is 200 chars and 400 bytes. const int charsInProperty = 200; string longPropertyName = new string('\u0467', charsInProperty); obj = new Dictionary { { $"{longPropertyName}", 1 } }; diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.ReadTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.ReadTests.cs index 75ad7d1e6bae4..3bfb3c9bbd6f2 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.ReadTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/Value.ReadTests.cs @@ -467,6 +467,20 @@ public static void TimeSpan_Read_Success(string json, string? actual = null) Assert.Equal(value, JsonConvert.DeserializeObject($"\"{json}\"")); } + [Fact] + public static void TimeSpan_Read_Nullable_Tests() + { + TimeSpan? value = JsonSerializer.Deserialize("null"); + Assert.False(value.HasValue); + + value = JsonSerializer.Deserialize("\"23:59:59\""); + Assert.True(value.HasValue); + Assert.Equal(TimeSpan.Parse("23:59:59"), value); + Assert.Equal(value, JsonConvert.DeserializeObject("\"23:59:59\"")); + + Assert.Throws(() => JsonSerializer.Deserialize("null")); + } + [Fact] public static void TimeSpan_Read_KnownDifferences() { diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.Date.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.Date.cs index bd4cbdb864e77..e5bf98633c788 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.Date.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.Date.cs @@ -207,7 +207,7 @@ public static void TestingDateTimeMaxValue() [MemberData(nameof(JsonDateTimeTestData.InvalidISO8601Tests), MemberType = typeof(JsonDateTimeTestData))] public static void TryGetDateTime_HasValueSequence_False(string testString) { - static void test(string testString, bool isFinalBlock) + static void Test(string testString, bool isFinalBlock) { byte[] dataUtf8 = Encoding.UTF8.GetBytes(testString); ReadOnlySequence sequence = JsonTestHelper.GetSequence(dataUtf8, 1); @@ -224,15 +224,15 @@ static void test(string testString, bool isFinalBlock) JsonTestHelper.AssertThrows(json, (jsonReader) => jsonReader.GetDateTime()); } - test(testString, isFinalBlock: true); - test(testString, isFinalBlock: false); + Test(testString, isFinalBlock: true); + Test(testString, isFinalBlock: false); } [Theory] [MemberData(nameof(JsonDateTimeTestData.InvalidISO8601Tests), MemberType = typeof(JsonDateTimeTestData))] public static void TryGetDateTimeOffset_HasValueSequence_False(string testString) { - static void test(string testString, bool isFinalBlock) + static void Test(string testString, bool isFinalBlock) { byte[] dataUtf8 = Encoding.UTF8.GetBytes(testString); ReadOnlySequence sequence = JsonTestHelper.GetSequence(dataUtf8, 1); @@ -249,8 +249,8 @@ static void test(string testString, bool isFinalBlock) JsonTestHelper.AssertThrows(json, (jsonReader) => jsonReader.GetDateTimeOffset()); } - test(testString, isFinalBlock: true); - test(testString, isFinalBlock: false); + Test(testString, isFinalBlock: true); + Test(testString, isFinalBlock: false); } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs index 27a53606963c5..44b8d2bffb004 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.TryGet.cs @@ -1335,7 +1335,7 @@ public static void TestingStringsConversionToGuid(string testString, string expe [MemberData(nameof(JsonGuidTestData.ValidGuidTests), MemberType = typeof(JsonGuidTestData))] public static void TryGetGuid_HasValueSequence_RetrievesGuid(string testString, string expectedString) { - static void test(string testString, string expectedString, bool isFinalBlock) + static void Test(string testString, string expectedString, bool isFinalBlock) { byte[] dataUtf8 = Encoding.UTF8.GetBytes($"\"{testString}\""); ReadOnlySequence sequence = JsonTestHelper.GetSequence(dataUtf8, 1); @@ -1354,8 +1354,8 @@ static void test(string testString, string expectedString, bool isFinalBlock) Assert.Equal(expected, json.GetGuid()); } - test(testString, expectedString, isFinalBlock: true); - test(testString, expectedString, isFinalBlock: false); + Test(testString, expectedString, isFinalBlock: true); + Test(testString, expectedString, isFinalBlock: false); } [Theory] @@ -1378,7 +1378,7 @@ public static void TestingStringsInvalidConversionToGuid(string testString) [MemberData(nameof(JsonGuidTestData.InvalidGuidTests), MemberType = typeof(JsonGuidTestData))] public static void TryGetGuid_HasValueSequence_False(string testString) { - static void test(string testString, bool isFinalBlock) + static void Test(string testString, bool isFinalBlock) { byte[] dataUtf8 = Encoding.UTF8.GetBytes($"\"{testString}\""); ReadOnlySequence sequence = JsonTestHelper.GetSequence(dataUtf8, 1); @@ -1395,8 +1395,8 @@ static void test(string testString, bool isFinalBlock) JsonTestHelper.AssertThrows(json, (jsonReader) => jsonReader.GetGuid()); } - test(testString, isFinalBlock: true); - test(testString, isFinalBlock: false); + Test(testString, isFinalBlock: true); + Test(testString, isFinalBlock: false); } } } diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.cs index 50926d7af7244..274fbdf978e36 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonReaderTests.cs @@ -4359,6 +4359,7 @@ public static IEnumerable InvalidJsonStrings new object[] {"nul", 0, 3}, new object[] {"tru", 0, 3}, new object[] {"fals", 0, 4}, + new object[] {"falseinvalid", 0, 5}, new object[] {"\"a\u6F22\u5B57ge\":", 0, 11}, new object[] {"{\"a\u6F22\u5B57ge\":", 0, 13}, new object[] {"{\"name\":\"A\u6F22\u5B57hso", 0, 19},