diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerConfigureOptions.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerConfigureOptions.cs
index c30e842d3fc3e..3b725f1cb59e0 100644
--- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerConfigureOptions.cs
+++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerConfigureOptions.cs
@@ -25,7 +25,7 @@ public void EnsureConsoleLoggerOptions_ConfigureOptions_SupportsAllProperties()
Assert.Equal(3, typeof(ConsoleFormatterOptions).GetProperties(flags).Length);
Assert.Equal(5, typeof(SimpleConsoleFormatterOptions).GetProperties(flags).Length);
Assert.Equal(4, typeof(JsonConsoleFormatterOptions).GetProperties(flags).Length);
- Assert.Equal(4, typeof(JsonWriterOptions).GetProperties(flags).Length);
+ Assert.Equal(6, typeof(JsonWriterOptions).GetProperties(flags).Length);
}
[Theory]
diff --git a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerExtensionsTests.cs b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerExtensionsTests.cs
index 99b49170ed8e1..fec2d737c8d2c 100644
--- a/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerExtensionsTests.cs
+++ b/src/libraries/Microsoft.Extensions.Logging.Console/tests/Microsoft.Extensions.Logging.Console.Tests/ConsoleLoggerExtensionsTests.cs
@@ -597,6 +597,7 @@ private static void VerifyHasOnlySimpleProperties(Type type)
// or else NativeAOT would break
Assert.True(prop.PropertyType == typeof(string) ||
prop.PropertyType == typeof(bool) ||
+ prop.PropertyType == typeof(char) ||
prop.PropertyType == typeof(int) ||
prop.PropertyType.IsEnum, $"ConsoleOptions property '{type.Name}.{prop.Name}' must be a simple type in order for NativeAOT to work");
}
diff --git a/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs b/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs
index 8084d4a6f613f..0bfaf896c4034 100644
--- a/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs
+++ b/src/libraries/System.Text.Json/Common/JsonSourceGenerationOptionsAttribute.cs
@@ -125,6 +125,16 @@ public JsonSourceGenerationOptionsAttribute(JsonSerializerDefaults defaults)
///
public bool WriteIndented { get; set; }
+ ///
+ /// Specifies the default value of when set.
+ ///
+ public char IndentCharacter { get; set; }
+
+ ///
+ /// Specifies the default value of when set.
+ ///
+ public int IndentSize { get; set; }
+
///
/// Specifies the default source generation mode for type declarations that don't set a .
///
diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
index db08be80c53d0..a7e396983d15a 100644
--- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
+++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
@@ -1168,6 +1168,12 @@ private static void GetLogicForDefaultSerializerOptionsInit(SourceGenerationOpti
if (optionsSpec.WriteIndented is bool writeIndented)
writer.WriteLine($"WriteIndented = {FormatBool(writeIndented)},");
+ if (optionsSpec.IndentCharacter is char indentCharacter)
+ writer.WriteLine($"IndentCharacter = {FormatIndentChar(indentCharacter)},");
+
+ if (optionsSpec.IndentSize is int indentSize)
+ writer.WriteLine($"IndentSize = {indentSize},");
+
writer.Indentation--;
writer.WriteLine("};");
@@ -1344,6 +1350,7 @@ private static string FormatJsonSerializerDefaults(JsonSerializerDefaults defaul
private static string FormatBool(bool value) => value ? "true" : "false";
private static string FormatStringLiteral(string? value) => value is null ? "null" : $"\"{value}\"";
+ private static string FormatIndentChar(char value) => value is '\t' ? "'\\t'" : $"'{value}'";
///
/// Method used to generate JsonTypeInfo given options instance
diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
index 594f7ad9770c3..3be80faceb84f 100644
--- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
+++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
@@ -280,6 +280,8 @@ private SourceGenerationOptionsSpec ParseJsonSourceGenerationOptionsAttribute(IN
JsonUnmappedMemberHandling? unmappedMemberHandling = null;
bool? useStringEnumConverter = null;
bool? writeIndented = null;
+ char? indentCharacter = null;
+ int? indentSize = null;
if (attributeData.ConstructorArguments.Length > 0)
{
@@ -373,6 +375,14 @@ private SourceGenerationOptionsSpec ParseJsonSourceGenerationOptionsAttribute(IN
writeIndented = (bool)namedArg.Value.Value!;
break;
+ case nameof(JsonSourceGenerationOptionsAttribute.IndentCharacter):
+ indentCharacter = (char)namedArg.Value.Value!;
+ break;
+
+ case nameof(JsonSourceGenerationOptionsAttribute.IndentSize):
+ indentSize = (int)namedArg.Value.Value!;
+ break;
+
case nameof(JsonSourceGenerationOptionsAttribute.GenerationMode):
generationMode = (JsonSourceGenerationMode)namedArg.Value.Value!;
break;
@@ -404,6 +414,8 @@ private SourceGenerationOptionsSpec ParseJsonSourceGenerationOptionsAttribute(IN
UnmappedMemberHandling = unmappedMemberHandling,
UseStringEnumConverter = useStringEnumConverter,
WriteIndented = writeIndented,
+ IndentCharacter = indentCharacter,
+ IndentSize = indentSize,
};
}
diff --git a/src/libraries/System.Text.Json/gen/Model/SourceGenerationOptionsSpec.cs b/src/libraries/System.Text.Json/gen/Model/SourceGenerationOptionsSpec.cs
index 83b587fb962f7..17126929c0e09 100644
--- a/src/libraries/System.Text.Json/gen/Model/SourceGenerationOptionsSpec.cs
+++ b/src/libraries/System.Text.Json/gen/Model/SourceGenerationOptionsSpec.cs
@@ -52,6 +52,10 @@ public sealed record SourceGenerationOptionsSpec
public required bool? WriteIndented { get; init; }
+ public required char? IndentCharacter { get; init; }
+
+ public required int? IndentSize { get; init; }
+
public JsonKnownNamingPolicy? GetEffectivePropertyNamingPolicy()
=> PropertyNamingPolicy ?? (Defaults is JsonSerializerDefaults.Web ? JsonKnownNamingPolicy.CamelCase : null);
}
diff --git a/src/libraries/System.Text.Json/ref/System.Text.Json.cs b/src/libraries/System.Text.Json/ref/System.Text.Json.cs
index 6014c8e5dc630..25ccb474dbbb6 100644
--- a/src/libraries/System.Text.Json/ref/System.Text.Json.cs
+++ b/src/libraries/System.Text.Json/ref/System.Text.Json.cs
@@ -395,6 +395,8 @@ public JsonSerializerOptions(System.Text.Json.JsonSerializerOptions options) { }
public System.Text.Json.Serialization.JsonUnknownTypeHandling UnknownTypeHandling { get { throw null; } set { } }
public System.Text.Json.Serialization.JsonUnmappedMemberHandling UnmappedMemberHandling { get { throw null; } set { } }
public bool WriteIndented { get { throw null; } set { } }
+ public char IndentCharacter { get { throw null; } set { } }
+ public int IndentSize { get { throw null; } set { } }
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
[System.ObsoleteAttribute("JsonSerializerOptions.AddContext is obsolete. To register a JsonSerializerContext, use either the TypeInfoResolver or TypeInfoResolverChain properties.", DiagnosticId="SYSLIB0049", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public void AddContext() where TContext : System.Text.Json.Serialization.JsonSerializerContext, new() { }
@@ -440,6 +442,8 @@ public partial struct JsonWriterOptions
private int _dummyPrimitive;
public System.Text.Encodings.Web.JavaScriptEncoder? Encoder { readonly get { throw null; } set { } }
public bool Indented { get { throw null; } set { } }
+ public char IndentCharacter { get { throw null; } set { } }
+ public int IndentSize { get { throw null; } set { } }
public int MaxDepth { readonly get { throw null; } set { } }
public bool SkipValidation { get { throw null; } set { } }
}
@@ -1075,6 +1079,8 @@ public JsonSourceGenerationOptionsAttribute(System.Text.Json.JsonSerializerDefau
public System.Text.Json.Serialization.JsonUnmappedMemberHandling UnmappedMemberHandling { get { throw null; } set { } }
public bool UseStringEnumConverter { get { throw null; } set { } }
public bool WriteIndented { get { throw null; } set { } }
+ public char IndentCharacter { get { throw null; } set { } }
+ public int IndentSize { get { throw null; } set { } }
}
[System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute("JsonStringEnumConverter cannot be statically analyzed and requires runtime code generation. Applications should use the generic JsonStringEnumConverter instead.")]
public partial class JsonStringEnumConverter : System.Text.Json.Serialization.JsonConverterFactory
diff --git a/src/libraries/System.Text.Json/src/Resources/Strings.resx b/src/libraries/System.Text.Json/src/Resources/Strings.resx
index 411d20d45d6be..acd31cc6cce25 100644
--- a/src/libraries/System.Text.Json/src/Resources/Strings.resx
+++ b/src/libraries/System.Text.Json/src/Resources/Strings.resx
@@ -708,4 +708,10 @@
Either the JSON value is not in a supported format, or is out of bounds for a Half.
+
+ Supported indentation characters are space and horizontal tab.
+
+
+ Indentation size must be between {0} and {1}.
+
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 30751a04a363e..99012e30e8b36 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
@@ -47,7 +47,6 @@ internal static partial class JsonConstants
// Explicitly skipping ReverseSolidus since that is handled separately
public static ReadOnlySpan EscapableChars => "\"nrt/ubf"u8;
- public const int SpacesPerIndent = 2;
public const int RemoveFlagsBitMask = 0x7FFFFFFF;
// In the worst case, an ASCII character represented as a single utf-8 byte could expand 6x when escaped.
@@ -110,5 +109,13 @@ internal static partial class JsonConstants
// The maximum number of parameters a constructor can have where it can be considered
// for a path on deserialization where we don't box the constructor arguments.
public const int UnboxedParameterCountThreshold = 4;
+
+ // Two space characters is the default indentation.
+ public const char DefaultIndentCharacter = ' ';
+ public const char TabIndentCharacter = '\t';
+ public const int DefaultIndentSize = 2;
+ public const int MinimumIndentSize = 0;
+ public const int MaximumIndentSize = 127; // If this value is changed, the impact on the options masking used in the JsonWriterOptions struct must be checked carefully.
+
}
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
index 68551c7eedf12..f76bdea2de2a3 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Caching.cs
@@ -511,6 +511,8 @@ public bool Equals(JsonSerializerOptions? left, JsonSerializerOptions? right)
left._includeFields == right._includeFields &&
left._propertyNameCaseInsensitive == right._propertyNameCaseInsensitive &&
left._writeIndented == right._writeIndented &&
+ left._indentCharacter == right._indentCharacter &&
+ left._indentSize == right._indentSize &&
left._typeInfoResolver == right._typeInfoResolver &&
CompareLists(left._converters, right._converters);
@@ -565,6 +567,8 @@ public int GetHashCode(JsonSerializerOptions options)
AddHashCode(ref hc, options._includeFields);
AddHashCode(ref hc, options._propertyNameCaseInsensitive);
AddHashCode(ref hc, options._writeIndented);
+ AddHashCode(ref hc, options._indentCharacter);
+ AddHashCode(ref hc, options._indentSize);
AddHashCode(ref hc, options._typeInfoResolver);
AddListHashCode(ref hc, options._converters);
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
index 20df47b466649..78d02af2b7846 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.cs
@@ -90,6 +90,8 @@ public static JsonSerializerOptions Web
private bool _includeFields;
private bool _propertyNameCaseInsensitive;
private bool _writeIndented;
+ private char _indentCharacter = JsonConstants.DefaultIndentCharacter;
+ private int _indentSize = JsonConstants.DefaultIndentSize;
///
/// Constructs a new instance.
@@ -139,6 +141,8 @@ public JsonSerializerOptions(JsonSerializerOptions options)
_includeFields = options._includeFields;
_propertyNameCaseInsensitive = options._propertyNameCaseInsensitive;
_writeIndented = options._writeIndented;
+ _indentCharacter = options._indentCharacter;
+ _indentSize = options._indentSize;
_typeInfoResolver = options._typeInfoResolver;
EffectiveMaxDepth = options.EffectiveMaxDepth;
ReferenceHandlingStrategy = options.ReferenceHandlingStrategy;
@@ -660,6 +664,50 @@ public bool WriteIndented
}
}
+ ///
+ /// Defines the indentation character being used when is enabled. Defaults to the space character.
+ ///
+ /// Allowed characters are space and horizontal tab.
+ /// contains an invalid character.
+ ///
+ /// Thrown if this property is set after serialization or deserialization has occurred.
+ ///
+ public char IndentCharacter
+ {
+ get
+ {
+ return _indentCharacter;
+ }
+ set
+ {
+ JsonWriterHelper.ValidateIndentCharacter(value);
+ VerifyMutable();
+ _indentCharacter = value;
+ }
+ }
+
+ ///
+ /// Defines the indentation size being used when is enabled. Defaults to two.
+ ///
+ /// Allowed values are all integers between 0 and 127, included.
+ /// is out of the allowed range.
+ ///
+ /// Thrown if this property is set after serialization or deserialization has occurred.
+ ///
+ public int IndentSize
+ {
+ get
+ {
+ return _indentSize;
+ }
+ set
+ {
+ JsonWriterHelper.ValidateIndentSize(value);
+ VerifyMutable();
+ _indentSize = value;
+ }
+ }
+
///
/// Configures how object references are handled when reading and writing JSON.
///
@@ -891,6 +939,8 @@ internal JsonWriterOptions GetWriterOptions()
{
Encoder = Encoder,
Indented = WriteIndented,
+ IndentCharacter = IndentCharacter,
+ IndentSize = IndentSize,
MaxDepth = EffectiveMaxDepth,
#if !DEBUG
SkipValidation = true
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
index 3d8913f8d2c78..58bd0a7172491 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.cs
@@ -12,6 +12,18 @@ internal static partial class ThrowHelper
// If the exception source is this value, the serializer will re-throw as JsonException.
public const string ExceptionSourceValueToRethrowAsJsonException = "System.Text.Json.Rethrowable";
+ [DoesNotReturn]
+ public static void ThrowArgumentOutOfRangeException_IndentCharacter(string parameterName)
+ {
+ throw GetArgumentOutOfRangeException(parameterName, SR.InvalidIndentCharacter);
+ }
+
+ [DoesNotReturn]
+ public static void ThrowArgumentOutOfRangeException_IndentSize(string parameterName, int minimumSize, int maximumSize)
+ {
+ throw GetArgumentOutOfRangeException(parameterName, SR.Format(SR.InvalidIndentSize, minimumSize, maximumSize));
+ }
+
[DoesNotReturn]
public static void ThrowArgumentOutOfRangeException_MaxDepthMustBePositive(string parameterName)
{
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs
index 97037ddbff727..c34c8cd3d672f 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterHelper.cs
@@ -10,9 +10,8 @@ namespace System.Text.Json
{
internal static partial class JsonWriterHelper
{
- public static void WriteIndentation(Span buffer, int indent)
+ public static void WriteIndentation(Span buffer, int indent, byte indentByte)
{
- Debug.Assert(indent % JsonConstants.SpacesPerIndent == 0);
Debug.Assert(buffer.Length >= indent);
// Based on perf tests, the break-even point where vectorized Fill is faster
@@ -20,18 +19,37 @@ public static void WriteIndentation(Span buffer, int indent)
if (indent < 8)
{
int i = 0;
- while (i < indent)
+ while (i + 1 < indent)
{
- buffer[i++] = JsonConstants.Space;
- buffer[i++] = JsonConstants.Space;
+ buffer[i++] = indentByte;
+ buffer[i++] = indentByte;
+ }
+
+ if (i < indent)
+ {
+ buffer[i] = indentByte;
}
}
else
{
- buffer.Slice(0, indent).Fill(JsonConstants.Space);
+ buffer.Slice(0, indent).Fill(indentByte);
}
}
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void ValidateIndentCharacter(char value)
+ {
+ if (value is not JsonConstants.DefaultIndentCharacter and not JsonConstants.TabIndentCharacter)
+ ThrowHelper.ThrowArgumentOutOfRangeException_IndentCharacter(nameof(value));
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void ValidateIndentSize(int value)
+ {
+ if (value is < JsonConstants.MinimumIndentSize or > JsonConstants.MaximumIndentSize)
+ ThrowHelper.ThrowArgumentOutOfRangeException_IndentSize(nameof(value), JsonConstants.MinimumIndentSize, JsonConstants.MaximumIndentSize);
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ValidateProperty(ReadOnlySpan propertyName)
{
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs
index 2d89a261e3353..1d89e5d6aa149 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/JsonWriterOptions.cs
@@ -43,6 +43,48 @@ public bool Indented
}
}
+ ///
+ /// Defines the indentation character used by when is enabled. Defaults to the space character.
+ ///
+ /// Allowed characters are space and horizontal tab.
+ /// contains an invalid character.
+ public char IndentCharacter
+ {
+ readonly get => (_optionsMask & IndentCharacterBit) != 0 ? JsonConstants.TabIndentCharacter : JsonConstants.DefaultIndentCharacter;
+ set
+ {
+ JsonWriterHelper.ValidateIndentCharacter(value);
+ if (value is not JsonConstants.DefaultIndentCharacter)
+ _optionsMask |= IndentCharacterBit;
+ else
+ _optionsMask &= ~IndentCharacterBit;
+ }
+ }
+
+ ///
+ /// Defines the indentation size used by when is enabled. Defaults to two.
+ ///
+ /// Allowed values are integers between 0 and 127, included.
+ /// is out of the allowed range.
+ public int IndentSize
+ {
+ readonly get => EncodeIndentSize((_optionsMask & IndentSizeMask) >> 3);
+ set
+ {
+ JsonWriterHelper.ValidateIndentSize(value);
+ _optionsMask = (_optionsMask & ~IndentSizeMask) | (EncodeIndentSize(value) << 3);
+ }
+ }
+
+ // Encoding is applied by swapping 0 with the default value to ensure default(JsonWriterOptions) instances are well-defined.
+ // As this operation is symmetrical, it can also be used to decode.
+ private static int EncodeIndentSize(int value) => value switch
+ {
+ 0 => JsonConstants.DefaultIndentSize,
+ JsonConstants.DefaultIndentSize => 0,
+ _ => value
+ };
+
///
/// Gets or sets the maximum depth allowed when writing JSON, with the default (i.e. 0) indicating a max depth of 1000.
///
@@ -93,9 +135,11 @@ public bool SkipValidation
}
}
- internal bool IndentedOrNotSkipValidation => _optionsMask != SkipValidationBit; // Equivalent to: Indented || !SkipValidation;
+ internal bool IndentedOrNotSkipValidation => (_optionsMask & (IndentBit | SkipValidationBit)) != SkipValidationBit; // Equivalent to: Indented || !SkipValidation;
private const int IndentBit = 1;
private const int SkipValidationBit = 2;
+ private const int IndentCharacterBit = 4;
+ private const int IndentSizeMask = JsonConstants.MaximumIndentSize << 3;
}
}
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 9c4785e56bfab..a8d53aef63dec 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
@@ -280,7 +280,7 @@ private void WriteBase64Minimized(ReadOnlySpan escapedPropertyName, ReadOn
private void WriteBase64Indented(ReadOnlySpan escapedPropertyName, ReadOnlySpan bytes)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
@@ -309,7 +309,7 @@ private void WriteBase64Indented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -330,7 +330,7 @@ private void WriteBase64Indented(ReadOnlySpan escapedPropertyName, ReadOnl
private void WriteBase64Indented(ReadOnlySpan escapedPropertyName, ReadOnlySpan bytes)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int encodedLength = Base64.GetMaxEncodedToUtf8Length(bytes.Length);
@@ -359,7 +359,7 @@ private void WriteBase64Indented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 55c079f008faa..392facd7d85ee 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
@@ -284,7 +284,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedPropertyName, DateTi
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTime value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatDateTimeOffsetLength - 7 - s_newLineLength);
@@ -311,7 +311,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -333,7 +333,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTime value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDateTimeOffsetLength - 7 - s_newLineLength);
@@ -359,7 +359,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 9ace783d791e4..313693b3b7665 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
@@ -283,7 +283,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedPropertyName, DateTi
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTimeOffset value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatDateTimeOffsetLength - 7 - s_newLineLength);
@@ -310,7 +310,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -332,7 +332,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTimeOffset value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDateTimeOffsetLength - 7 - s_newLineLength);
@@ -358,7 +358,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, DateTim
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 933fcffc4fc1b..3f1af56067cd9 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
@@ -277,7 +277,7 @@ private void WriteNumberMinimized(ReadOnlySpan escapedPropertyName, decima
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, decimal value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatDecimalLength - 5 - s_newLineLength);
@@ -304,7 +304,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, decimal
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -323,7 +323,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, decimal
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, decimal value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDecimalLength - 5 - s_newLineLength);
@@ -349,7 +349,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, decimal
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 73bcc75f72ede..eee0535903cfa 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
@@ -281,7 +281,7 @@ private void WriteNumberMinimized(ReadOnlySpan escapedPropertyName, double
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, double value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatDoubleLength - 5 - s_newLineLength);
@@ -308,7 +308,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, double
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -327,7 +327,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, double
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, double value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatDoubleLength - 5 - s_newLineLength);
@@ -353,7 +353,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, double
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 bb695740d290f..133c95ece5d86 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
@@ -281,7 +281,7 @@ private void WriteNumberMinimized(ReadOnlySpan escapedPropertyName, float
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, float value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatSingleLength - 5 - s_newLineLength);
@@ -308,7 +308,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, float v
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -327,7 +327,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, float v
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, float value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatSingleLength - 5 - s_newLineLength);
@@ -353,7 +353,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, float v
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 a0c5713374ca1..1ded7b8f3f6a1 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
@@ -285,7 +285,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedPropertyName, Guid v
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, Guid value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatGuidLength - 7 - s_newLineLength);
@@ -312,7 +312,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, Guid va
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -335,7 +335,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, Guid va
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, Guid value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatGuidLength - 7 - s_newLineLength);
@@ -361,7 +361,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, Guid va
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Helpers.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Helpers.cs
index cd56c0a99aa9e..c09aaa40c8a23 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Helpers.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteProperties.Helpers.cs
@@ -89,7 +89,7 @@ private void WritePropertyNameMinimized(ReadOnlySpan escapedPropertyName,
private void WritePropertyNameIndented(ReadOnlySpan escapedPropertyName, byte token)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - 6 - s_newLineLength);
@@ -115,7 +115,7 @@ private void WritePropertyNameIndented(ReadOnlySpan escapedPropertyName, b
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -161,7 +161,7 @@ private void WritePropertyNameMinimized(ReadOnlySpan escapedPropertyName,
private void WritePropertyNameIndented(ReadOnlySpan escapedPropertyName, byte token)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - 6 - s_newLineLength);
@@ -188,7 +188,7 @@ private void WritePropertyNameIndented(ReadOnlySpan escapedPropertyName, b
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 e73131b7192f9..aff0da471b1a3 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
@@ -427,7 +427,7 @@ private void WriteLiteralSection(ReadOnlySpan escapedPropertyNameSection,
private void WriteLiteralIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - value.Length - 5 - s_newLineLength);
@@ -455,7 +455,7 @@ private void WriteLiteralIndented(ReadOnlySpan escapedPropertyName, ReadOn
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -473,7 +473,7 @@ private void WriteLiteralIndented(ReadOnlySpan escapedPropertyName, ReadOn
private void WriteLiteralIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - value.Length - 5 - s_newLineLength);
@@ -500,7 +500,7 @@ private void WriteLiteralIndented(ReadOnlySpan escapedPropertyName, ReadOn
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 10c390a98ffc4..82694f738cc76 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
@@ -353,7 +353,7 @@ private void WriteNumberMinimized(ReadOnlySpan escapedPropertyName, long v
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, long value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatInt64Length - 5 - s_newLineLength);
@@ -380,7 +380,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, long va
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -399,7 +399,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, long va
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, long value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatInt64Length - 5 - s_newLineLength);
@@ -425,7 +425,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, long va
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 7db29516ddbe0..16629ea424f4c 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
@@ -187,7 +187,7 @@ private void WriteStringMinimizedPropertyName(ReadOnlySpan escapedProperty
private void WriteStringIndentedPropertyName(ReadOnlySpan escapedPropertyName)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue - 5 - indent - s_newLineLength) / JsonConstants.MaxExpansionFactorWhileTranscoding);
@@ -213,7 +213,7 @@ private void WriteStringIndentedPropertyName(ReadOnlySpan escapedPropertyN
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -375,7 +375,7 @@ private void WriteStringPropertyNameSection(ReadOnlySpan escapedPropertyNa
private void WriteStringIndentedPropertyName(ReadOnlySpan escapedPropertyName)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - 5 - s_newLineLength);
@@ -402,7 +402,7 @@ private void WriteStringIndentedPropertyName(ReadOnlySpan escapedPropertyN
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -1513,7 +1513,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedPropertyName, ReadOn
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < ((int.MaxValue - 7 - indent - s_newLineLength) / JsonConstants.MaxExpansionFactorWhileTranscoding) - escapedValue.Length);
@@ -1541,7 +1541,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -1563,7 +1563,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - escapedValue.Length - 7 - s_newLineLength);
@@ -1590,7 +1590,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -1614,7 +1614,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - escapedValue.Length - 7 - indent - s_newLineLength);
@@ -1642,7 +1642,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -1665,7 +1665,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length <= JsonConstants.MaxEscapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - escapedValue.Length - 7 - indent - s_newLineLength);
@@ -1693,7 +1693,7 @@ private void WriteStringIndented(ReadOnlySpan escapedPropertyName, ReadOnl
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
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 ab73c36582c64..37aad46266599 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
@@ -362,7 +362,7 @@ private void WriteNumberMinimized(ReadOnlySpan escapedPropertyName, ulong
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, ulong value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - JsonConstants.MaximumFormatUInt64Length - 5 - s_newLineLength);
@@ -389,7 +389,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, ulong v
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
@@ -408,7 +408,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, ulong v
private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, ulong value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - JsonConstants.MaximumFormatUInt64Length - 5 - s_newLineLength);
@@ -434,7 +434,7 @@ private void WriteNumberIndented(ReadOnlySpan escapedPropertyName, ulong v
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Bytes.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Bytes.cs
index 6d04df1c3ce6e..31500fb2b00ec 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Bytes.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Bytes.cs
@@ -88,7 +88,7 @@ private void WriteBase64Minimized(ReadOnlySpan bytes)
private void WriteBase64Indented(ReadOnlySpan bytes)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
// Base64.GetMaxEncodedToUtf8Length checks to make sure the length is <= int.MaxValue / 4 * 3,
// as a length longer than that would overflow int.MaxValue when Base64 encoded. However, we
@@ -124,7 +124,7 @@ private void WriteBase64Indented(ReadOnlySpan bytes)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Comment.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Comment.cs
index 7e3d3a30d661b..93f11451f454d 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Comment.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Comment.cs
@@ -105,7 +105,7 @@ private void WriteCommentMinimized(ReadOnlySpan value)
private void WriteCommentIndented(ReadOnlySpan value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(value.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - 4 - s_newLineLength);
@@ -123,7 +123,7 @@ private void WriteCommentIndented(ReadOnlySpan value)
if (_tokenType != JsonTokenType.None || _commentAfterNoneOrPropertyName)
{
WriteNewLine(output);
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
@@ -212,7 +212,7 @@ private void WriteCommentMinimized(ReadOnlySpan utf8Value)
private void WriteCommentIndented(ReadOnlySpan utf8Value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(utf8Value.Length < int.MaxValue - indent - 4 - s_newLineLength);
@@ -230,7 +230,7 @@ private void WriteCommentIndented(ReadOnlySpan utf8Value)
{
WriteNewLine(output);
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTime.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTime.cs
index f87753be0ae37..ad8a887cdf55a 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTime.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTime.cs
@@ -66,7 +66,7 @@ private void WriteStringValueMinimized(DateTime value)
private void WriteStringValueIndented(DateTime value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
// 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
int maxRequired = indent + JsonConstants.MaximumFormatDateTimeOffsetLength + 3 + s_newLineLength;
@@ -89,7 +89,7 @@ private void WriteStringValueIndented(DateTime value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTimeOffset.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTimeOffset.cs
index 2608606375b39..1ee48004f21fd 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTimeOffset.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.DateTimeOffset.cs
@@ -67,7 +67,7 @@ private void WriteStringValueMinimized(DateTimeOffset value)
private void WriteStringValueIndented(DateTimeOffset value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
// 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
int maxRequired = indent + JsonConstants.MaximumFormatDateTimeOffsetLength + 3 + s_newLineLength;
@@ -90,7 +90,7 @@ private void WriteStringValueIndented(DateTimeOffset value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Decimal.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Decimal.cs
index 2462b667fe8ba..35089030c2d45 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Decimal.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Decimal.cs
@@ -63,7 +63,7 @@ private void WriteNumberValueMinimized(decimal value)
private void WriteNumberValueIndented(decimal value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int maxRequired = indent + JsonConstants.MaximumFormatDecimalLength + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -85,7 +85,7 @@ private void WriteNumberValueIndented(decimal value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs
index 036b9e3d12254..b638931ce1229 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Double.cs
@@ -67,7 +67,7 @@ private void WriteNumberValueMinimized(double value)
private void WriteNumberValueIndented(double value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int maxRequired = indent + JsonConstants.MaximumFormatDoubleLength + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -89,7 +89,7 @@ private void WriteNumberValueIndented(double value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs
index 5e173a7dc0ece..c1755c7c4dc5d 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Float.cs
@@ -67,7 +67,7 @@ private void WriteNumberValueMinimized(float value)
private void WriteNumberValueIndented(float value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int maxRequired = indent + JsonConstants.MaximumFormatSingleLength + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -89,7 +89,7 @@ private void WriteNumberValueIndented(float value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.FormattedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.FormattedNumber.cs
index f9005243fac4c..f4c902e184af4 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.FormattedNumber.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.FormattedNumber.cs
@@ -67,7 +67,7 @@ private void WriteNumberValueMinimized(ReadOnlySpan utf8Value)
private void WriteNumberValueIndented(ReadOnlySpan utf8Value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(utf8Value.Length < int.MaxValue - indent - 1 - s_newLineLength);
@@ -91,7 +91,7 @@ private void WriteNumberValueIndented(ReadOnlySpan utf8Value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Guid.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Guid.cs
index 227af26a3d2c0..695cc83e03680 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Guid.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Guid.cs
@@ -67,7 +67,7 @@ private void WriteStringValueMinimized(Guid value)
private void WriteStringValueIndented(Guid value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
// 2 quotes, and optionally, 1 list separator and 1-2 bytes for new line
int maxRequired = indent + JsonConstants.MaximumFormatGuidLength + 3 + s_newLineLength;
@@ -90,7 +90,7 @@ private void WriteStringValueIndented(Guid value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Literal.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Literal.cs
index b4bcc5b480246..8c0dee44e785e 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Literal.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.Literal.cs
@@ -86,7 +86,7 @@ private void WriteLiteralMinimized(ReadOnlySpan utf8Value)
private void WriteLiteralIndented(ReadOnlySpan utf8Value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(utf8Value.Length <= 5);
int maxRequired = indent + utf8Value.Length + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -109,7 +109,7 @@ private void WriteLiteralIndented(ReadOnlySpan utf8Value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.SignedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.SignedNumber.cs
index dd47dfe230550..b2126de77fb2f 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.SignedNumber.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.SignedNumber.cs
@@ -76,7 +76,7 @@ private void WriteNumberValueMinimized(long value)
private void WriteNumberValueIndented(long value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int maxRequired = indent + JsonConstants.MaximumFormatInt64Length + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -98,7 +98,7 @@ private void WriteNumberValueIndented(long value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
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 6513d001930b7..4ede0fc9bce93 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
@@ -143,7 +143,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedValue)
private void WriteStringIndented(ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - 3 - s_newLineLength);
@@ -169,7 +169,7 @@ private void WriteStringIndented(ReadOnlySpan escapedValue)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
@@ -290,7 +290,7 @@ private void WriteStringMinimized(ReadOnlySpan escapedValue)
private void WriteStringIndented(ReadOnlySpan escapedValue)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(escapedValue.Length < int.MaxValue - indent - 3 - s_newLineLength);
@@ -315,7 +315,7 @@ private void WriteStringIndented(ReadOnlySpan escapedValue)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.UnsignedNumber.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.UnsignedNumber.cs
index a348c125c8172..82ec4677b2224 100644
--- a/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.UnsignedNumber.cs
+++ b/src/libraries/System.Text.Json/src/System/Text/Json/Writer/Utf8JsonWriter.WriteValues.UnsignedNumber.cs
@@ -78,7 +78,7 @@ private void WriteNumberValueMinimized(ulong value)
private void WriteNumberValueIndented(ulong value)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int maxRequired = indent + JsonConstants.MaximumFormatUInt64Length + 1 + s_newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -100,7 +100,7 @@ private void WriteNumberValueIndented(ulong value)
{
WriteNewLine(output);
}
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
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 c84b864a0cfec..315b7a30b8e20 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
@@ -58,6 +58,10 @@ public sealed partial class Utf8JsonWriter : IDisposable, IAsyncDisposable
private JsonWriterOptions _options; // Since JsonWriterOptions is a struct, use a field to avoid a copy for internal code.
+ // Cache indentation settings from JsonWriterOptions to avoid recomputing them in the hot path.
+ private byte _indentByte;
+ private int _indentLength;
+
///
/// Returns the amount of bytes written by the so far
/// that have not yet been flushed to the output and committed.
@@ -80,7 +84,7 @@ public sealed partial class Utf8JsonWriter : IDisposable, IAsyncDisposable
///
public JsonWriterOptions Options => _options;
- private int Indentation => CurrentDepth * JsonConstants.SpacesPerIndent;
+ private int Indentation => CurrentDepth * _indentLength;
internal JsonTokenType TokenType => _tokenType;
@@ -112,12 +116,7 @@ public Utf8JsonWriter(IBufferWriter bufferWriter, JsonWriterOptions option
}
_output = bufferWriter;
- _options = options;
-
- if (_options.MaxDepth == 0)
- {
- _options.MaxDepth = JsonWriterOptions.DefaultMaxDepth; // If max depth is not set, revert to the default depth.
- }
+ SetOptions(options);
}
///
@@ -141,14 +140,21 @@ public Utf8JsonWriter(Stream utf8Json, JsonWriterOptions options = default)
throw new ArgumentException(SR.StreamNotWritable);
_stream = utf8Json;
+ SetOptions(options);
+
+ _arrayBufferWriter = new ArrayBufferWriter();
+ }
+
+ private void SetOptions(JsonWriterOptions options)
+ {
_options = options;
+ _indentByte = (byte)_options.IndentCharacter;
+ _indentLength = options.IndentSize;
if (_options.MaxDepth == 0)
{
_options.MaxDepth = JsonWriterOptions.DefaultMaxDepth; // If max depth is not set, revert to the default depth.
}
-
- _arrayBufferWriter = new ArrayBufferWriter();
}
///
@@ -245,11 +251,7 @@ internal void Reset(IBufferWriter bufferWriter, JsonWriterOptions options)
Debug.Assert(_output is null && _stream is null && _arrayBufferWriter is null);
_output = bufferWriter;
- _options = options;
- if (_options.MaxDepth == 0)
- {
- _options.MaxDepth = JsonWriterOptions.DefaultMaxDepth; // If max depth is not set, revert to the default depth.
- }
+ SetOptions(options);
}
internal static Utf8JsonWriter CreateEmptyInstanceForCaching() => new Utf8JsonWriter();
@@ -553,7 +555,7 @@ private void ValidateStart()
private void WriteStartIndented(byte token)
{
int indent = Indentation;
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
int minRequired = indent + 1; // 1 start token
int maxRequired = minRequired + 3; // Optionally, 1 list separator and 1-2 bytes for new line
@@ -573,7 +575,7 @@ private void WriteStartIndented(byte token)
if (_tokenType is not JsonTokenType.PropertyName and not JsonTokenType.None || _commentAfterNoneOrPropertyName)
{
WriteNewLine(output);
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
}
@@ -994,10 +996,10 @@ private void WriteEndIndented(byte token)
{
// The end token should be at an outer indent and since we haven't updated
// current depth yet, explicitly subtract here.
- indent -= JsonConstants.SpacesPerIndent;
+ indent -= _indentLength;
}
- Debug.Assert(indent <= 2 * _options.MaxDepth);
+ Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.None);
int maxRequired = indent + 3; // 1 end token, 1-2 bytes for new line
@@ -1011,7 +1013,7 @@ private void WriteEndIndented(byte token)
WriteNewLine(output);
- JsonWriterHelper.WriteIndentation(output.Slice(BytesPending), indent);
+ WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = token;
@@ -1029,6 +1031,11 @@ private void WriteNewLine(Span output)
output[BytesPending++] = JsonConstants.LineFeed;
}
+ private void WriteIndentation(Span buffer, int indent)
+ {
+ JsonWriterHelper.WriteIndentation(buffer, indent, _indentByte);
+ }
+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateBitStackOnStart(byte token)
{
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSourceGenerationOptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSourceGenerationOptionsTests.cs
index eb855b0241dfd..9c1c30818f6b2 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSourceGenerationOptionsTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSourceGenerationOptionsTests.cs
@@ -78,6 +78,8 @@ public static void ContextWithAllOptionsSet_GeneratesExpectedOptions()
UnknownTypeHandling = JsonUnknownTypeHandling.JsonNode,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
WriteIndented = true,
+ IndentCharacter = '\t',
+ IndentSize = 1,
TypeInfoResolver = ContextWithAllOptionsSet.Default,
};
@@ -104,7 +106,9 @@ public static void ContextWithAllOptionsSet_GeneratesExpectedOptions()
ReadCommentHandling = JsonCommentHandling.Skip,
UnknownTypeHandling = JsonUnknownTypeHandling.JsonNode,
UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow,
- WriteIndented = true)]
+ WriteIndented = true,
+ IndentCharacter = '\t',
+ IndentSize = 1)]
[JsonSerializable(typeof(PersonStruct))]
public partial class ContextWithAllOptionsSet : JsonSerializerContext
{ }
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonWriterOptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonWriterOptionsTests.cs
index 41a6c5c203a91..127ca9601150d 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonWriterOptionsTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/JsonWriterOptionsTests.cs
@@ -15,6 +15,8 @@ public static void JsonWriterOptionsDefaultCtor()
var expectedOption = new JsonWriterOptions
{
Indented = false,
+ IndentCharacter = ' ',
+ IndentSize = 2,
SkipValidation = false,
MaxDepth = 0,
};
@@ -29,6 +31,8 @@ public static void JsonWriterOptionsCtor()
var expectedOption = new JsonWriterOptions
{
Indented = false,
+ IndentCharacter = ' ',
+ IndentSize = 2,
SkipValidation = false,
MaxDepth = 0,
};
@@ -36,26 +40,92 @@ public static void JsonWriterOptionsCtor()
}
[Theory]
- [InlineData(true, true, 0)]
- [InlineData(true, false, 1)]
- [InlineData(false, true, 1024)]
- [InlineData(false, false, 1024 * 1024)]
- public static void JsonWriterOptions(bool indented, bool skipValidation, int maxDepth)
+ [InlineData(true, '\t', 1, true, 0)]
+ [InlineData(true, ' ', 127, false, 1)]
+ [InlineData(false, ' ', 0, true, 1024)]
+ [InlineData(false, ' ', 4, false, 1024 * 1024)]
+ public static void JsonWriterOptions(bool indented, char indentCharacter, int indentSize, bool skipValidation, int maxDepth)
{
var options = new JsonWriterOptions();
options.Indented = indented;
+ options.IndentCharacter = indentCharacter;
+ options.IndentSize = indentSize;
options.SkipValidation = skipValidation;
options.MaxDepth = maxDepth;
var expectedOption = new JsonWriterOptions
{
Indented = indented,
+ IndentCharacter = indentCharacter,
+ IndentSize = indentSize,
SkipValidation = skipValidation,
MaxDepth = maxDepth,
};
Assert.Equal(expectedOption, options);
}
+ [Theory]
+ [InlineData(true, '\t', 1, true, 0)]
+ [InlineData(true, ' ', 127, false, 1)]
+ [InlineData(false, ' ', 0, true, 1024)]
+ [InlineData(false, ' ', 4, false, 1024 * 1024)]
+ public static void JsonWriterOptions_Properties(bool indented, char indentCharacter, int indentSize, bool skipValidation, int maxDepth)
+ {
+ var options = new JsonWriterOptions();
+ options.Indented = indented;
+ options.IndentCharacter = indentCharacter;
+ options.IndentSize = indentSize;
+ options.SkipValidation = skipValidation;
+ options.MaxDepth = maxDepth;
+
+ Assert.Equal(indented, options.Indented);
+ Assert.Equal(indentCharacter, options.IndentCharacter);
+ Assert.Equal(indentSize, options.IndentSize);
+ Assert.Equal(skipValidation, options.SkipValidation);
+ Assert.Equal(maxDepth, options.MaxDepth);
+ }
+
+ [Fact]
+ public static void JsonWriterOptions_DefaultValues()
+ {
+ JsonWriterOptions options = default;
+
+ Assert.False(options.Indented);
+ Assert.Equal(' ', options.IndentCharacter);
+ Assert.Equal(2, options.IndentSize);
+ Assert.False(options.SkipValidation);
+ Assert.Equal(0, options.MaxDepth);
+ }
+
+ [Fact]
+ public static void JsonWriterOptions_MultipleValues()
+ {
+ JsonWriterOptions defaultOptions = default;
+ var options = new JsonWriterOptions();
+
+ options.Indented = true;
+ options.Indented = defaultOptions.Indented;
+ Assert.Equal(defaultOptions.Indented, options.Indented);
+
+ options.IndentCharacter = '\t';
+ options.IndentCharacter = defaultOptions.IndentCharacter;
+ Assert.Equal(defaultOptions.IndentCharacter, options.IndentCharacter);
+
+ options.IndentSize = 127;
+ options.IndentSize = defaultOptions.IndentSize;
+ Assert.Equal(defaultOptions.IndentSize, options.IndentSize);
+
+ options.SkipValidation = true;
+ options.SkipValidation = defaultOptions.SkipValidation;
+ Assert.Equal(defaultOptions.SkipValidation, options.SkipValidation);
+
+ options.MaxDepth = 1024 * 1024;
+ options.MaxDepth = defaultOptions.MaxDepth;
+ Assert.Equal(defaultOptions.MaxDepth, options.MaxDepth);
+
+ Assert.Equal(defaultOptions, options);
+ }
+
[Theory]
[InlineData(-1)]
[InlineData(-100)]
@@ -64,5 +134,28 @@ public static void JsonWriterOptions_MaxDepth_InvalidParameters(int maxDepth)
var options = new JsonWriterOptions();
Assert.Throws(() => options.MaxDepth = maxDepth);
}
+
+ [Theory]
+ [InlineData('\f')]
+ [InlineData('\n')]
+ [InlineData('\r')]
+ [InlineData('\0')]
+ [InlineData('a')]
+ public static void JsonWriterOptions_IndentCharacter_InvalidCharacter(char character)
+ {
+ var options = new JsonWriterOptions();
+ Assert.Throws(() => options.IndentCharacter = character);
+ }
+
+ [Theory]
+ [InlineData(-1)]
+ [InlineData(128)]
+ [InlineData(int.MinValue)]
+ [InlineData(int.MaxValue)]
+ public static void JsonWriterOptions_IndentSize_OutOfRange(int size)
+ {
+ var options = new JsonWriterOptions();
+ Assert.Throws(() => options.IndentSize = size);
+ }
}
}
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs
index 25c9e2084048b..50c6d865268fd 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/CacheTests.cs
@@ -371,6 +371,8 @@ public static void JsonSerializerOptions_EqualityComparer_ChangingAnySettingShou
yield return (GetProp(nameof(JsonSerializerOptions.ReadCommentHandling)), JsonCommentHandling.Skip);
yield return (GetProp(nameof(JsonSerializerOptions.UnknownTypeHandling)), JsonUnknownTypeHandling.JsonNode);
yield return (GetProp(nameof(JsonSerializerOptions.WriteIndented)), true);
+ yield return (GetProp(nameof(JsonSerializerOptions.IndentCharacter)), '\t');
+ yield return (GetProp(nameof(JsonSerializerOptions.IndentSize)), 1);
yield return (GetProp(nameof(JsonSerializerOptions.ReferenceHandler)), ReferenceHandler.Preserve);
yield return (GetProp(nameof(JsonSerializerOptions.TypeInfoResolver)), new DefaultJsonTypeInfoResolver());
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
index 28bd0fb86cfe3..cbca460362c4b 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/OptionsTests.cs
@@ -330,6 +330,90 @@ public static void WriteIndented()
Assert.Contains(Environment.NewLine, json);
}
+ [Theory]
+ [InlineData('\f')]
+ [InlineData('\n')]
+ [InlineData('\r')]
+ [InlineData('\0')]
+ [InlineData('a')]
+ public static void IndentCharacters_WithInvalidChartacters_ThrowsArgumentOutOfRangeException(char character)
+ {
+ var options = new JsonSerializerOptions();
+ Assert.Throws(() => options.IndentCharacter = character);
+ }
+
+ [Theory]
+ [InlineData(-1)]
+ [InlineData(128)]
+ [InlineData(int.MinValue)]
+ [InlineData(int.MaxValue)]
+ public static void IndentSize_WithInvalidSize_ThrowsArgumentOutOfRangeException(int size)
+ {
+ var options = new JsonSerializerOptions();
+ Assert.Throws(() => options.IndentSize = size);
+ }
+
+ [Fact]
+ public static void IndentCharacter()
+ {
+ var obj = new BasicCompany();
+ obj.Initialize();
+
+ // Verify default value.
+ var defaultIndent = " ";
+ string json = JsonSerializer.Serialize(obj);
+ Assert.DoesNotContain(defaultIndent, json);
+
+ // Verify default value on options.
+ var options = new JsonSerializerOptions();
+ json = JsonSerializer.Serialize(obj, options);
+ Assert.DoesNotContain(defaultIndent, json);
+
+ // Enable default indentation.
+ options = new JsonSerializerOptions();
+ options.WriteIndented = true;
+ json = JsonSerializer.Serialize(obj, options);
+ Assert.Contains(defaultIndent, json);
+
+ // Set custom indentCharacter without enabling indentation
+ var tab = '\t';
+ Assert.DoesNotContain(tab, json);
+ options = new JsonSerializerOptions();
+ options.IndentCharacter = tab;
+ json = JsonSerializer.Serialize(obj, options);
+ Assert.DoesNotContain(tab, json);
+
+ // Set custom indentCharacter with indentation enabled
+ options = new JsonSerializerOptions();
+ options.WriteIndented = true;
+ options.IndentCharacter = tab;
+ json = JsonSerializer.Serialize(obj, options);
+ Assert.Contains(tab, json);
+ }
+
+ [Fact]
+ public static void IndentSize()
+ {
+ var obj = new BasicCompany();
+ obj.Initialize();
+
+ var tab = '\t';
+ // Set custom indentSize without enabling indentation
+ var options = new JsonSerializerOptions();
+ options.IndentCharacter = tab;
+ options.IndentSize = 1;
+ var json = JsonSerializer.Serialize(obj, options);
+ Assert.DoesNotContain(tab, json);
+
+ // Set custom indentSize with indentation enabled
+ options = new JsonSerializerOptions();
+ options.WriteIndented = true;
+ options.IndentCharacter = tab;
+ options.IndentSize = 4;
+ json = JsonSerializer.Serialize(obj, options);
+ Assert.Contains(new string(tab, 4), json);
+ }
+
[Fact]
public static void ExtensionDataUsesReaderOptions()
{
@@ -1329,6 +1413,10 @@ and not nameof(JsonSerializerOptions.IsReadOnly)) // Property is not structural
{
property.SetValue(options, 32);
}
+ else if (propertyType == typeof(string))
+ {
+ property.SetValue(options, "\t");
+ }
else if (propertyType == typeof(IList))
{
options.Converters.Add(new JsonStringEnumConverter());
diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.cs
index 331f81947ae5d..643762183c713 100644
--- a/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.cs
+++ b/src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Utf8JsonWriterTests.cs
@@ -8,6 +8,7 @@
using System.Globalization;
using System.IO;
using System.IO.Pipelines;
+using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using System.Threading;
@@ -64,15 +65,14 @@ public partial class Utf8JsonWriterTests
/*Comment at start of doc*//*Multiple comment line*/{/*Comment before first object property*//*Multiple comment line*/"property1":/*Comment of string property value*/"stringValue","property2":/*Comment of object property value*/{}/*Comment in the middle of object*/,"property3":/*Comment of array property value*/[]/*Comment after the last property*/}/*Comment at end of doc*/
""";
+ public static IEnumerable