diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int16Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int16Converter.cs index 83e8a4d3f841cb..c01ee00032cc55 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int16Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int16Converter.cs @@ -14,7 +14,8 @@ public override short Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSe public override void Write(Utf8JsonWriter writer, short value, JsonSerializerOptions options) { - writer.WriteNumberValue(value); + // For performance, lift up the writer implementation. + writer.WriteNumberValue((long)value); } internal override short ReadWithQuotes(ref Utf8JsonReader reader) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int32Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int32Converter.cs index 7064eb4a1d75e3..5fc881a6b2e4d1 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int32Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/Int32Converter.cs @@ -12,7 +12,8 @@ public override int Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSeri public override void Write(Utf8JsonWriter writer, int value, JsonSerializerOptions options) { - writer.WriteNumberValue(value); + // For performance, lift up the writer implementation. + writer.WriteNumberValue((long)value); } internal override int ReadWithQuotes(ref Utf8JsonReader reader) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/StringConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/StringConverter.cs index 4e5677af54c877..f03d9ac1cba129 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/StringConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/StringConverter.cs @@ -14,7 +14,15 @@ internal sealed class StringConverter : JsonConverter public override void Write(Utf8JsonWriter writer, string? value, JsonSerializerOptions options) { - writer.WriteStringValue(value); + // For performance, lift up the writer implementation. + if (value == null) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.AsSpan()); + } } internal override string ReadWithQuotes(ref Utf8JsonReader reader) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt16Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt16Converter.cs index 3997d079494182..db2af0a5f2e3ba 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt16Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt16Converter.cs @@ -12,7 +12,8 @@ public override ushort Read(ref Utf8JsonReader reader, Type typeToConvert, JsonS public override void Write(Utf8JsonWriter writer, ushort value, JsonSerializerOptions options) { - writer.WriteNumberValue(value); + // For performance, lift up the writer implementation. + writer.WriteNumberValue((long)value); } internal override ushort ReadWithQuotes(ref Utf8JsonReader reader) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt32Converter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt32Converter.cs index 59284fdc86dfd3..131e6feb1b0544 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt32Converter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/UInt32Converter.cs @@ -12,7 +12,8 @@ public override uint Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer public override void Write(Utf8JsonWriter writer, uint value, JsonSerializerOptions options) { - writer.WriteNumberValue(value); + // For performance, lift up the writer implementation. + writer.WriteNumberValue((ulong)value); } internal override uint ReadWithQuotes(ref Utf8JsonReader reader) 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 6ee1df069608a8..7852c0052001c6 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 @@ -18,10 +18,8 @@ public sealed partial class Utf8JsonWriter /// Thrown if this would result in invalid JSON being written (while validation is enabled). /// public void WriteBase64String(JsonEncodedText propertyName, ReadOnlySpan bytes) - => WriteBase64StringHelper(propertyName.EncodedUtf8Bytes, bytes); - - private void WriteBase64StringHelper(ReadOnlySpan utf8PropertyName, ReadOnlySpan bytes) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); JsonWriterHelper.ValidateBytes(bytes); 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 15309dfa2ea4c6..fa96ffc6e8dc2a 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 @@ -22,10 +22,8 @@ public sealed partial class Utf8JsonWriter /// The property name should already be escaped when the instance of was created. /// public void WriteString(JsonEncodedText propertyName, DateTime value) - => WriteStringHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteStringHelper(ReadOnlySpan utf8PropertyName, DateTime value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteStringByOptions(utf8PropertyName, value); 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 78fdedb7e0a12d..3cbfef7cf4fb59 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the round-trippable ('O') , for example: 2017-06-12T05:30:45.7680000-07:00. /// public void WriteString(JsonEncodedText propertyName, DateTimeOffset value) - => WriteStringHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteStringHelper(ReadOnlySpan utf8PropertyName, DateTimeOffset value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteStringByOptions(utf8PropertyName, value); 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 2ffce4d8c4ff45..861ce7e00599ac 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the default (that is, 'G'). /// public void WriteNumber(JsonEncodedText propertyName, decimal value) - => WriteNumberHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteNumberHelper(ReadOnlySpan utf8PropertyName, decimal value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteNumberByOptions(utf8PropertyName, value); 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 eae9668079fb84..359cfd43036766 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the default (that is, 'G'). /// public void WriteNumber(JsonEncodedText propertyName, double value) - => WriteNumberHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteNumberHelper(ReadOnlySpan utf8PropertyName, double value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); JsonWriterHelper.ValidateDouble(value); 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 a9226298567c57..6e46a4f6efbf84 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the default (that is, 'G'). /// public void WriteNumber(JsonEncodedText propertyName, float value) - => WriteNumberHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteNumberHelper(ReadOnlySpan utf8PropertyName, float value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); JsonWriterHelper.ValidateSingle(value); 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 9dc0569ce336b3..899ff6febf3476 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the default (that is, 'D'), as the form: nnnnnnnn-nnnn-nnnn-nnnn-nnnnnnnnnnnn. /// public void WriteString(JsonEncodedText propertyName, Guid value) - => WriteStringHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteStringHelper(ReadOnlySpan utf8PropertyName, Guid value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteStringByOptions(utf8PropertyName, value); 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 ec0ebad5c4706b..b231446cd68048 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 @@ -21,10 +21,8 @@ public sealed partial class Utf8JsonWriter /// Writes the using the default (that is, 'G'), for example: 32767. /// public void WriteNumber(JsonEncodedText propertyName, long value) - => WriteNumberHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteNumberHelper(ReadOnlySpan utf8PropertyName, long value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteNumberByOptions(utf8PropertyName, value); 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 653fb190dcca45..5b6698143f111f 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 @@ -22,10 +22,8 @@ public sealed partial class Utf8JsonWriter /// [CLSCompliant(false)] public void WriteNumber(JsonEncodedText propertyName, ulong value) - => WriteNumberHelper(propertyName.EncodedUtf8Bytes, value); - - private void WriteNumberHelper(ReadOnlySpan utf8PropertyName, ulong value) { + ReadOnlySpan utf8PropertyName = propertyName.EncodedUtf8Bytes; Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize); WriteNumberByOptions(utf8PropertyName, value); 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 4e7e209dd86ca7..07d8077c16c193 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 @@ -33,7 +33,10 @@ public void WriteBase64StringValue(ReadOnlySpan bytes) private void WriteBase64ByOptions(ReadOnlySpan bytes) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } if (_options.Indented) { 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 194232e69c556d..0d5687c2be4a00 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 @@ -21,7 +21,11 @@ public sealed partial class Utf8JsonWriter /// public void WriteStringValue(DateTime value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteStringValueIndented(value); 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 6b42c4d369c400..b92b799217954b 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 @@ -22,7 +22,11 @@ public sealed partial class Utf8JsonWriter /// public void WriteStringValue(DateTimeOffset value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteStringValueIndented(value); 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 71d7d37cf9f063..ca5b059be50bbd 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 @@ -21,7 +21,11 @@ public sealed partial class Utf8JsonWriter /// public void WriteNumberValue(decimal value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteNumberValueIndented(value); 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 7eb318ee3541e4..cf8732bc44ec8a 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 @@ -25,7 +25,11 @@ public void WriteNumberValue(double value) { JsonWriterHelper.ValidateDouble(value); - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteNumberValueIndented(value); 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 5de5bab3393202..f120a09373c479 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 @@ -25,7 +25,11 @@ public void WriteNumberValue(float value) { JsonWriterHelper.ValidateSingle(value); - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteNumberValueIndented(value); 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 069dc310efa224..5dfb4e9e8ff484 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 @@ -25,7 +25,11 @@ internal void WriteNumberValue(ReadOnlySpan utf8FormattedNumber) { JsonWriterHelper.ValidateValue(utf8FormattedNumber); JsonWriterHelper.ValidateNumber(utf8FormattedNumber); - ValidateWritingValue(); + + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } if (_options.Indented) { 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 26a757600a5fb6..e9884f744a5812 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 @@ -21,7 +21,11 @@ public sealed partial class Utf8JsonWriter /// public void WriteStringValue(Guid value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteStringValueIndented(value); 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 5ac6a4bafc2037..6e617bdec880cd 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 @@ -12,25 +12,24 @@ public sealed partial class Utf8JsonWriter { private void ValidateWritingValue() { - if (!_options.SkipValidation) + Debug.Assert(!_options.SkipValidation); + + if (_inObject) { - if (_inObject) + if (_tokenType != JsonTokenType.PropertyName) { - if (_tokenType != JsonTokenType.PropertyName) - { - Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray); - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueWithinObject, currentDepth: default, token: default, _tokenType); - } + Debug.Assert(_tokenType != JsonTokenType.None && _tokenType != JsonTokenType.StartArray); + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueWithinObject, currentDepth: default, token: default, _tokenType); } - else - { - Debug.Assert(_tokenType != JsonTokenType.PropertyName); + } + else + { + Debug.Assert(_tokenType != JsonTokenType.PropertyName); - // It is more likely for CurrentDepth to not equal 0 when writing valid JSON, so check that first to rely on short-circuiting and return quickly. - if (CurrentDepth == 0 && _tokenType != JsonTokenType.None) - { - ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueAfterPrimitiveOrClose, currentDepth: default, token: default, _tokenType); - } + // It is more likely for CurrentDepth to not equal 0 when writing valid JSON, so check that first to rely on short-circuiting and return quickly. + if (CurrentDepth == 0 && _tokenType != JsonTokenType.None) + { + ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueAfterPrimitiveOrClose, currentDepth: default, token: default, _tokenType); } } } 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 e1eba43d1ae519..c5beb15ae3f1db 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 @@ -46,7 +46,11 @@ public void WriteBooleanValue(bool value) private void WriteLiteralByOptions(ReadOnlySpan utf8Value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteLiteralIndented(utf8Value); 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 c37211d622a930..957573ddb7a7f4 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 @@ -34,7 +34,11 @@ public void WriteNumberValue(int value) /// public void WriteNumberValue(long value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteNumberValueIndented(value); 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 c3eb222a2fed01..c86a8af22f1628 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 @@ -16,10 +16,8 @@ public sealed partial class Utf8JsonWriter /// Thrown if this would result in invalid JSON being written (while validation is enabled). /// public void WriteStringValue(JsonEncodedText value) - => WriteStringValueHelper(value.EncodedUtf8Bytes); - - private void WriteStringValueHelper(ReadOnlySpan utf8Value) { + ReadOnlySpan utf8Value = value.EncodedUtf8Bytes; Debug.Assert(utf8Value.Length <= JsonConstants.MaxUnescapedTokenSize); WriteStringByOptions(utf8Value); @@ -99,7 +97,11 @@ private void WriteStringEscape(ReadOnlySpan value) private void WriteStringByOptions(ReadOnlySpan value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteStringIndented(value); @@ -242,7 +244,11 @@ private void WriteStringEscape(ReadOnlySpan utf8Value) private void WriteStringByOptions(ReadOnlySpan utf8Value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteStringIndented(utf8Value); 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 4cb253bef86695..2c15441d432593 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 @@ -36,7 +36,11 @@ public void WriteNumberValue(uint value) [CLSCompliant(false)] public void WriteNumberValue(ulong value) { - ValidateWritingValue(); + if (!_options.SkipValidation) + { + ValidateWritingValue(); + } + if (_options.Indented) { WriteNumberValueIndented(value);