<PackageReference Include="System.Text.Json" Version="9.0.6" />

Utf8JsonWriter

Provides a high-performance API for forward-only, non-cached writing of UTF-8 encoded JSON text.
using System.Buffers; using System.Buffers.Text; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; namespace System.Text.Json { [DebuggerDisplay("{DebuggerDisplay,nq}")] public sealed class Utf8JsonWriter : IDisposable, IAsyncDisposable { private const int DefaultGrowthSize = 4096; private const int InitialGrowthSize = 256; private IBufferWriter<byte> _output; private Stream _stream; private System.Buffers.ArrayBufferWriter<byte> _arrayBufferWriter; private Memory<byte> _memory; private bool _inObject; private bool _commentAfterNoneOrPropertyName; private JsonTokenType _tokenType; private BitStack _bitStack; private int _currentDepth; private JsonWriterOptions _options; private byte _indentByte; private int _indentLength; private int _newLineLength; private static readonly char[] s_singleLineCommentDelimiter = new char[2] { '*', '/' }; public int BytesPending { get; set; } public long BytesCommitted { get; set; } public JsonWriterOptions Options => _options; private int Indentation => CurrentDepth * _indentLength; internal JsonTokenType TokenType => _tokenType; public int CurrentDepth => _currentDepth & 2147483647; [System.Runtime.CompilerServices.Nullable(1)] [DebuggerBrowsable(DebuggerBrowsableState.Never)] private string DebuggerDisplay { get { return $"""{BytesCommitted}""{BytesPending}""{CurrentDepth}"; } } private unsafe static ReadOnlySpan<byte> SingleLineCommentDelimiterUtf8 => new ReadOnlySpan<byte>(&global::<PrivateImplementationDetails>.BCCD0BA7F848775347BA9B64603DDEAC80563D9BFC8FB67AF20A7FB468DF6FD4, 2); private Utf8JsonWriter() { } [System.Runtime.CompilerServices.NullableContext(1)] public Utf8JsonWriter(IBufferWriter<byte> bufferWriter, JsonWriterOptions options = default(JsonWriterOptions)) { if (bufferWriter == null) ThrowHelper.ThrowArgumentNullException("bufferWriter"); _output = bufferWriter; SetOptions(options); } [System.Runtime.CompilerServices.NullableContext(1)] public Utf8JsonWriter(Stream utf8Json, JsonWriterOptions options = default(JsonWriterOptions)) { if (utf8Json == null) ThrowHelper.ThrowArgumentNullException("utf8Json"); if (!utf8Json.CanWrite) throw new ArgumentException(System.SR.StreamNotWritable); _stream = utf8Json; SetOptions(options); _arrayBufferWriter = new System.Buffers.ArrayBufferWriter<byte>(); } private void SetOptions(JsonWriterOptions options) { _options = options; _indentByte = (byte)_options.IndentCharacter; _indentLength = options.IndentSize; _newLineLength = options.NewLine.Length; if (_options.MaxDepth == 0) _options.MaxDepth = 1000; } public void Reset() { CheckNotDisposed(); _arrayBufferWriter?.Clear(); ResetHelper(); } [System.Runtime.CompilerServices.NullableContext(1)] public void Reset(Stream utf8Json) { CheckNotDisposed(); if (utf8Json == null) throw new ArgumentNullException("utf8Json"); if (!utf8Json.CanWrite) throw new ArgumentException(System.SR.StreamNotWritable); _stream = utf8Json; if (_arrayBufferWriter == null) _arrayBufferWriter = new System.Buffers.ArrayBufferWriter<byte>(); else _arrayBufferWriter.Clear(); _output = null; ResetHelper(); } [System.Runtime.CompilerServices.NullableContext(1)] public void Reset(IBufferWriter<byte> bufferWriter) { CheckNotDisposed(); if (bufferWriter == null) throw new ArgumentNullException("bufferWriter"); _output = bufferWriter; _stream = null; _arrayBufferWriter = null; ResetHelper(); } internal void ResetAllStateForCacheReuse() { ResetHelper(); _stream = null; _arrayBufferWriter = null; _output = null; } internal void Reset(IBufferWriter<byte> bufferWriter, JsonWriterOptions options) { _output = bufferWriter; SetOptions(options); } internal static Utf8JsonWriter CreateEmptyInstanceForCaching() { return new Utf8JsonWriter(); } private void ResetHelper() { BytesPending = 0; BytesCommitted = 0; _memory = default(Memory<byte>); _inObject = false; _tokenType = JsonTokenType.None; _commentAfterNoneOrPropertyName = false; _currentDepth = 0; _bitStack = default(BitStack); } private void CheckNotDisposed() { if (_stream == null && _output == null) ThrowHelper.ThrowObjectDisposedException_Utf8JsonWriter(); } public void Flush() { CheckNotDisposed(); _memory = default(Memory<byte>); if (_stream != null) { if (BytesPending != 0) { _arrayBufferWriter.Advance(BytesPending); BytesPending = 0; MemoryMarshal.TryGetArray(_arrayBufferWriter.WrittenMemory, out ArraySegment<byte> segment); _stream.Write(segment.Array, segment.Offset, segment.Count); BytesCommitted += _arrayBufferWriter.WrittenCount; _arrayBufferWriter.Clear(); } _stream.Flush(); } else if (BytesPending != 0) { _output.Advance(BytesPending); BytesCommitted += BytesPending; BytesPending = 0; } } public void Dispose() { if (_stream != null || _output != null) { Flush(); ResetHelper(); _stream = null; _arrayBufferWriter = null; _output = null; } } [AsyncStateMachine(typeof(<DisposeAsync>d__45))] public ValueTask DisposeAsync() { <DisposeAsync>d__45 stateMachine = default(<DisposeAsync>d__45); stateMachine.<>t__builder = AsyncValueTaskMethodBuilder.Create(); stateMachine.<>4__this = this; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } [System.Runtime.CompilerServices.NullableContext(1)] [AsyncStateMachine(typeof(<FlushAsync>d__46))] public Task FlushAsync(CancellationToken cancellationToken = default(CancellationToken)) { <FlushAsync>d__46 stateMachine = default(<FlushAsync>d__46); stateMachine.<>t__builder = AsyncTaskMethodBuilder.Create(); stateMachine.<>4__this = this; stateMachine.cancellationToken = cancellationToken; stateMachine.<>1__state = -1; stateMachine.<>t__builder.Start(ref stateMachine); return stateMachine.<>t__builder.Task; } public void WriteStartArray() { WriteStart(91); _tokenType = JsonTokenType.StartArray; } public void WriteStartObject() { WriteStart(123); _tokenType = JsonTokenType.StartObject; } private void WriteStart(byte token) { if (CurrentDepth >= _options.MaxDepth) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.DepthTooLarge, _currentDepth, _options.MaxDepth, 0, JsonTokenType.None); if (_options.IndentedOrNotSkipValidation) WriteStartSlow(token); else WriteStartMinimized(token); _currentDepth &= 2147483647; _currentDepth++; } private void WriteStartMinimized(byte token) { if (_memory.Length - BytesPending < 2) Grow(2); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = token; } private void WriteStartSlow(byte token) { if (_options.Indented) { if (!_options.SkipValidation) { ValidateStart(); UpdateBitStackOnStart(token); } WriteStartIndented(token); } else { ValidateStart(); UpdateBitStackOnStart(token); WriteStartMinimized(token); } } private void ValidateStart() { if (_inObject) { if (_tokenType != JsonTokenType.PropertyName) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotStartObjectArrayWithoutProperty, 0, _options.MaxDepth, 0, _tokenType); } else if (CurrentDepth == 0 && _tokenType != 0) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotStartObjectArrayAfterPrimitiveOrClose, 0, _options.MaxDepth, 0, _tokenType); } } private void WriteStartIndented(byte token) { int indentation = Indentation; int num = indentation + 1 + 3; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; JsonTokenType tokenType = _tokenType; if ((tokenType != JsonTokenType.PropertyName && tokenType != 0) || _commentAfterNoneOrPropertyName) { WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = token; } public void WriteStartArray(JsonEncodedText propertyName) { WriteStartHelper(propertyName.EncodedUtf8Bytes, 91); _tokenType = JsonTokenType.StartArray; } public void WriteStartObject(JsonEncodedText propertyName) { WriteStartHelper(propertyName.EncodedUtf8Bytes, 123); _tokenType = JsonTokenType.StartObject; } private void WriteStartHelper(ReadOnlySpan<byte> utf8PropertyName, byte token) { ValidateDepth(); WriteStartByOptions(utf8PropertyName, token); _currentDepth &= 2147483647; _currentDepth++; } public void WriteStartArray(ReadOnlySpan<byte> utf8PropertyName) { ValidatePropertyNameAndDepth(utf8PropertyName); WriteStartEscape(utf8PropertyName, 91); _currentDepth &= 2147483647; _currentDepth++; _tokenType = JsonTokenType.StartArray; } public void WriteStartObject(ReadOnlySpan<byte> utf8PropertyName) { ValidatePropertyNameAndDepth(utf8PropertyName); WriteStartEscape(utf8PropertyName, 123); _currentDepth &= 2147483647; _currentDepth++; _tokenType = JsonTokenType.StartObject; } private void WriteStartEscape(ReadOnlySpan<byte> utf8PropertyName, byte token) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStartEscapeProperty(utf8PropertyName, token, num); else WriteStartByOptions(utf8PropertyName, token); } private void WriteStartByOptions(ReadOnlySpan<byte> utf8PropertyName, byte token) { ValidateWritingProperty(token); if (_options.Indented) WritePropertyNameIndented(utf8PropertyName, token); else WritePropertyNameMinimized(utf8PropertyName, token); } private unsafe void WriteStartEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, byte token, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStartByOptions(destination.Slice(0, written), token); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteStartArray(string propertyName) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteStartArray(propertyName.AsSpan()); } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteStartObject(string propertyName) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteStartObject(propertyName.AsSpan()); } public void WriteStartArray(ReadOnlySpan<char> propertyName) { ValidatePropertyNameAndDepth(propertyName); WriteStartEscape(propertyName, 91); _currentDepth &= 2147483647; _currentDepth++; _tokenType = JsonTokenType.StartArray; } public void WriteStartObject(ReadOnlySpan<char> propertyName) { ValidatePropertyNameAndDepth(propertyName); WriteStartEscape(propertyName, 123); _currentDepth &= 2147483647; _currentDepth++; _tokenType = JsonTokenType.StartObject; } private void WriteStartEscape(ReadOnlySpan<char> propertyName, byte token) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStartEscapeProperty(propertyName, token, num); else WriteStartByOptions(propertyName, token); } private void WriteStartByOptions(ReadOnlySpan<char> propertyName, byte token) { ValidateWritingProperty(token); if (_options.Indented) WritePropertyNameIndented(propertyName, token); else WritePropertyNameMinimized(propertyName, token); } private unsafe void WriteStartEscapeProperty(ReadOnlySpan<char> propertyName, byte token, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStartByOptions(destination.Slice(0, written), token); if (array != null) ArrayPool<char>.Shared.Return(array, false); } public void WriteEndArray() { WriteEnd(93); _tokenType = JsonTokenType.EndArray; } public void WriteEndObject() { WriteEnd(125); _tokenType = JsonTokenType.EndObject; } private void WriteEnd(byte token) { if (_options.IndentedOrNotSkipValidation) WriteEndSlow(token); else WriteEndMinimized(token); SetFlagToAddListSeparatorBeforeNextItem(); if (CurrentDepth != 0) _currentDepth--; } private void WriteEndMinimized(byte token) { if (_memory.Length - BytesPending < 1) Grow(1); _memory.Span[BytesPending++] = token; } private void WriteEndSlow(byte token) { if (_options.Indented) { if (!_options.SkipValidation) ValidateEnd(token); WriteEndIndented(token); } else { ValidateEnd(token); WriteEndMinimized(token); } } private void ValidateEnd(byte token) { if (_bitStack.CurrentDepth <= 0 || _tokenType == JsonTokenType.PropertyName) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.MismatchedObjectArray, 0, _options.MaxDepth, token, _tokenType); if (token == 93) { if (_inObject) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.MismatchedObjectArray, 0, _options.MaxDepth, token, _tokenType); } else if (!_inObject) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.MismatchedObjectArray, 0, _options.MaxDepth, token, _tokenType); } _inObject = _bitStack.Pop(); } private void WriteEndIndented(byte token) { if (_tokenType == JsonTokenType.StartObject || _tokenType == JsonTokenType.StartArray) WriteEndMinimized(token); else { int num = Indentation; if (num != 0) num -= _indentLength; int num2 = num + 3; if (_memory.Length - BytesPending < num2) Grow(num2); Span<byte> span = _memory.Span; WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), num); BytesPending += num; span[BytesPending++] = token; } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteNewLine(Span<byte> output) { if (_newLineLength == 2) output[BytesPending++] = 13; output[BytesPending++] = 10; } private void WriteIndentation(Span<byte> buffer, int indent) { JsonWriterHelper.WriteIndentation(buffer, indent, _indentByte); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void UpdateBitStackOnStart(byte token) { if (token == 91) { _bitStack.PushFalse(); _inObject = false; } else { _bitStack.PushTrue(); _inObject = true; } } private void Grow(int requiredSize) { if (_memory.Length == 0) FirstCallToGetMemory(requiredSize); else { int num = Math.Max(4096, requiredSize); if (_stream != null) { int num2 = BytesPending + num; JsonHelpers.ValidateInt32MaxArrayLength((uint)num2); _memory = _arrayBufferWriter.GetMemory(num2); } else { _output.Advance(BytesPending); BytesCommitted += BytesPending; BytesPending = 0; _memory = _output.GetMemory(num); if (_memory.Length < num) ThrowHelper.ThrowInvalidOperationException_NeedLargerSpan(); } } } private void FirstCallToGetMemory(int requiredSize) { int num = Math.Max(256, requiredSize); if (_stream != null) _memory = _arrayBufferWriter.GetMemory(num); else { _memory = _output.GetMemory(num); if (_memory.Length < num) ThrowHelper.ThrowInvalidOperationException_NeedLargerSpan(); } } private void SetFlagToAddListSeparatorBeforeNextItem() { _currentDepth |= -2147483648; } public void WriteBase64String(JsonEncodedText propertyName, ReadOnlySpan<byte> bytes) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteBase64ByOptions(encodedUtf8Bytes, bytes); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteBase64String([System.Runtime.CompilerServices.Nullable(1)] string propertyName, ReadOnlySpan<byte> bytes) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteBase64String(propertyName.AsSpan(), bytes); } public void WriteBase64String(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes) { JsonWriterHelper.ValidatePropertyNameLength(propertyName); WriteBase64Escape(propertyName, bytes); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteBase64String(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes) { JsonWriterHelper.ValidatePropertyNameLength(utf8PropertyName); WriteBase64Escape(utf8PropertyName, bytes); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteBase64Escape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteBase64EscapeProperty(propertyName, bytes, num); else WriteBase64ByOptions(propertyName, bytes); } private void WriteBase64Escape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteBase64EscapeProperty(utf8PropertyName, bytes, num); else WriteBase64ByOptions(utf8PropertyName, bytes); } private unsafe void WriteBase64EscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteBase64ByOptions(destination.Slice(0, written), bytes); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteBase64EscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteBase64ByOptions(destination.Slice(0, written), bytes); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteBase64ByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> bytes) { ValidateWritingProperty(); if (_options.Indented) WriteBase64Indented(propertyName, bytes); else WriteBase64Minimized(propertyName, bytes); } private void WriteBase64ByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> bytes) { ValidateWritingProperty(); if (_options.Indented) WriteBase64Indented(utf8PropertyName, bytes); else WriteBase64Minimized(utf8PropertyName, bytes); } private void WriteBase64Minimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> bytes) { int maxEncodedToUtf8Length = Base64.GetMaxEncodedToUtf8Length(bytes.Length); int num = escapedPropertyName.Length * 3 + maxEncodedToUtf8Length + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } private void WriteBase64Minimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> bytes) { int maxEncodedToUtf8Length = Base64.GetMaxEncodedToUtf8Length(bytes.Length); int num = escapedPropertyName.Length + maxEncodedToUtf8Length + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } private void WriteBase64Indented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> bytes) { int indentation = Indentation; int maxEncodedToUtf8Length = Base64.GetMaxEncodedToUtf8Length(bytes.Length); int num = indentation + escapedPropertyName.Length * 3 + maxEncodedToUtf8Length + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } private void WriteBase64Indented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> bytes) { int indentation = Indentation; int maxEncodedToUtf8Length = Base64.GetMaxEncodedToUtf8Length(bytes.Length); int num = indentation + escapedPropertyName.Length + maxEncodedToUtf8Length + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } public void WriteString(JsonEncodedText propertyName, DateTime value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteStringByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteString(string propertyName, DateTime value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), value); } public void WriteString(ReadOnlySpan<char> propertyName, DateTime value) { JsonWriterHelper.ValidateProperty(propertyName); WriteStringEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, DateTime value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteStringEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTime value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(propertyName, value, num); else WriteStringByOptions(propertyName, value); } private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTime value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(utf8PropertyName, value, num); else WriteStringByOptions(utf8PropertyName, value); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTime value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, DateTime value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTime value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(propertyName, value); else WriteStringMinimized(propertyName, value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTime value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(utf8PropertyName, value); else WriteStringMinimized(utf8PropertyName, value); } private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, DateTime value) { int num = escapedPropertyName.Length * 3 + 33 + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, DateTime value) { int num = escapedPropertyName.Length + 33 + 5 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTime value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 33 + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTime value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 33 + 6 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } internal unsafe void WritePropertyName(DateTime value) { Span<byte> buffer = new Span<byte>(stackalloc byte[33], 33); JsonWriterHelper.WriteDateTimeTrimmed(buffer, value, out int bytesWritten); WritePropertyNameUnescaped(buffer.Slice(0, bytesWritten)); } public void WriteString(JsonEncodedText propertyName, DateTimeOffset value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteStringByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteString(string propertyName, DateTimeOffset value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), value); } public void WriteString(ReadOnlySpan<char> propertyName, DateTimeOffset value) { JsonWriterHelper.ValidateProperty(propertyName); WriteStringEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteStringEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringEscape(ReadOnlySpan<char> propertyName, DateTimeOffset value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(propertyName, value, num); else WriteStringByOptions(propertyName, value); } private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(utf8PropertyName, value, num); else WriteStringByOptions(utf8PropertyName, value); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, DateTimeOffset value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteStringByOptions(ReadOnlySpan<char> propertyName, DateTimeOffset value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(propertyName, value); else WriteStringMinimized(propertyName, value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, DateTimeOffset value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(utf8PropertyName, value); else WriteStringMinimized(utf8PropertyName, value); } private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, DateTimeOffset value) { int num = escapedPropertyName.Length * 3 + 33 + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, DateTimeOffset value) { int num = escapedPropertyName.Length + 33 + 5 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, DateTimeOffset value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 33 + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, DateTimeOffset value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 33 + 6 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } internal unsafe void WritePropertyName(DateTimeOffset value) { Span<byte> buffer = new Span<byte>(stackalloc byte[33], 33); JsonWriterHelper.WriteDateTimeOffsetTrimmed(buffer, value, out int bytesWritten); WritePropertyNameUnescaped(buffer.Slice(0, bytesWritten)); } public void WriteNumber(JsonEncodedText propertyName, decimal value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteNumberByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNumber(string propertyName, decimal value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), value); } public void WriteNumber(ReadOnlySpan<char> propertyName, decimal value) { JsonWriterHelper.ValidateProperty(propertyName); WriteNumberEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, decimal value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteNumberEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, decimal value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, decimal value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, decimal value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, decimal value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, decimal value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(propertyName, value); else WriteNumberMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, decimal value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(utf8PropertyName, value); else WriteNumberMinimized(utf8PropertyName, value); } private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, decimal value) { int num = escapedPropertyName.Length * 3 + 31 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, decimal value) { int num = escapedPropertyName.Length + 31 + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, decimal value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 31 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, decimal value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 31 + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal unsafe void WritePropertyName(decimal value) { Span<byte> destination = new Span<byte>(stackalloc byte[31], 31); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } public void WriteNumber(JsonEncodedText propertyName, double value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; JsonWriterHelper.ValidateDouble(value); WriteNumberByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNumber(string propertyName, double value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), value); } public void WriteNumber(ReadOnlySpan<char> propertyName, double value) { JsonWriterHelper.ValidateProperty(propertyName); JsonWriterHelper.ValidateDouble(value); WriteNumberEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, double value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); JsonWriterHelper.ValidateDouble(value); WriteNumberEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, double value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, double value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, double value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, double value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, double value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(propertyName, value); else WriteNumberMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, double value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(utf8PropertyName, value); else WriteNumberMinimized(utf8PropertyName, value); } private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, double value) { int num = escapedPropertyName.Length * 3 + 128 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, double value) { int num = escapedPropertyName.Length + 128 + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, double value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 128 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, double value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 128 + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } internal unsafe void WritePropertyName(double value) { JsonWriterHelper.ValidateDouble(value); Span<byte> destination = new Span<byte>(stackalloc byte[128], 128); TryFormatDouble(value, destination, out int bytesWritten); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } public void WriteNumber(JsonEncodedText propertyName, float value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; JsonWriterHelper.ValidateSingle(value); WriteNumberByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNumber(string propertyName, float value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), value); } public void WriteNumber(ReadOnlySpan<char> propertyName, float value) { JsonWriterHelper.ValidateProperty(propertyName); JsonWriterHelper.ValidateSingle(value); WriteNumberEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, float value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); JsonWriterHelper.ValidateSingle(value); WriteNumberEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, float value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, float value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, float value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, float value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, float value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(propertyName, value); else WriteNumberMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, float value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(utf8PropertyName, value); else WriteNumberMinimized(utf8PropertyName, value); } private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, float value) { int num = escapedPropertyName.Length * 3 + 128 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, float value) { int num = escapedPropertyName.Length + 128 + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, float value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 128 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, float value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 128 + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } internal unsafe void WritePropertyName(float value) { Span<byte> destination = new Span<byte>(stackalloc byte[128], 128); TryFormatSingle(value, destination, out int bytesWritten); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } internal void WriteNumber(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8FormattedNumber) { JsonWriterHelper.ValidateProperty(propertyName); JsonWriterHelper.ValidateValue(utf8FormattedNumber); JsonWriterHelper.ValidateNumber(utf8FormattedNumber); WriteNumberEscape(propertyName, utf8FormattedNumber); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } internal void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8FormattedNumber) { JsonWriterHelper.ValidateProperty(utf8PropertyName); JsonWriterHelper.ValidateValue(utf8FormattedNumber); JsonWriterHelper.ValidateNumber(utf8FormattedNumber); WriteNumberEscape(utf8PropertyName, utf8FormattedNumber); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } internal void WriteNumber(JsonEncodedText propertyName, ReadOnlySpan<byte> utf8FormattedNumber) { JsonWriterHelper.ValidateValue(utf8FormattedNumber); JsonWriterHelper.ValidateNumber(utf8FormattedNumber); WriteNumberByOptions(propertyName.EncodedUtf8Bytes, utf8FormattedNumber); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value) { ValidateWritingProperty(); if (_options.Indented) WriteLiteralIndented(propertyName, value); else WriteLiteralMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value) { ValidateWritingProperty(); if (_options.Indented) WriteLiteralIndented(utf8PropertyName, value); else WriteLiteralMinimized(utf8PropertyName, value); } public void WriteString(JsonEncodedText propertyName, Guid value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteStringByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteString(string propertyName, Guid value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), value); } public void WriteString(ReadOnlySpan<char> propertyName, Guid value) { JsonWriterHelper.ValidateProperty(propertyName); WriteStringEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, Guid value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteStringEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringEscape(ReadOnlySpan<char> propertyName, Guid value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(propertyName, value, num); else WriteStringByOptions(propertyName, value); } private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, Guid value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(utf8PropertyName, value, num); else WriteStringByOptions(utf8PropertyName, value); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<char> propertyName, Guid value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteStringEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, Guid value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteStringByOptions(ReadOnlySpan<char> propertyName, Guid value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(propertyName, value); else WriteStringMinimized(propertyName, value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, Guid value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(utf8PropertyName, value); else WriteStringMinimized(utf8PropertyName, value); } private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, Guid value) { int num = escapedPropertyName.Length * 3 + 36 + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, Guid value) { int num = escapedPropertyName.Length + 36 + 5 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, Guid value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 36 + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, Guid value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 36 + 6 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } internal unsafe void WritePropertyName(Guid value) { Span<byte> destination = new Span<byte>(stackalloc byte[36], 36); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidatePropertyNameAndDepth(ReadOnlySpan<char> propertyName) { if (propertyName.Length > 166666666 || CurrentDepth >= _options.MaxDepth) ThrowHelper.ThrowInvalidOperationOrArgumentException(propertyName, _currentDepth, _options.MaxDepth); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidatePropertyNameAndDepth(ReadOnlySpan<byte> utf8PropertyName) { if (utf8PropertyName.Length > 166666666 || CurrentDepth >= _options.MaxDepth) ThrowHelper.ThrowInvalidOperationOrArgumentException(utf8PropertyName, _currentDepth, _options.MaxDepth); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidateDepth() { if (CurrentDepth >= _options.MaxDepth) ThrowHelper.ThrowInvalidOperationException(_currentDepth, _options.MaxDepth); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidateWritingProperty() { if (!_options.SkipValidation && (!_inObject || _tokenType == JsonTokenType.PropertyName)) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWritePropertyWithinArray, 0, _options.MaxDepth, 0, _tokenType); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void ValidateWritingProperty(byte token) { if (!_options.SkipValidation) { if (!_inObject || _tokenType == JsonTokenType.PropertyName) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWritePropertyWithinArray, 0, _options.MaxDepth, 0, _tokenType); UpdateBitStackOnStart(token); } } private void WritePropertyNameMinimized(ReadOnlySpan<byte> escapedPropertyName, byte token) { int num = escapedPropertyName.Length + 4 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = token; } private void WritePropertyNameIndented(ReadOnlySpan<byte> escapedPropertyName, byte token) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 5 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = token; } private void WritePropertyNameMinimized(ReadOnlySpan<char> escapedPropertyName, byte token) { int num = escapedPropertyName.Length * 3 + 5; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = token; } private void WritePropertyNameIndented(ReadOnlySpan<char> escapedPropertyName, byte token) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 6 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = token; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void TranscodeAndWrite(ReadOnlySpan<char> escapedPropertyName, Span<byte> output) { JsonWriterHelper.ToUtf8(escapedPropertyName, output.Slice(BytesPending), out int written); BytesPending += written; } public void WriteNull(JsonEncodedText propertyName) { WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.NullValue); _tokenType = JsonTokenType.Null; } internal void WriteNullSection(ReadOnlySpan<byte> escapedPropertyNameSection) { if (_options.Indented) { ReadOnlySpan<byte> utf8PropertyName = escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3); WriteLiteralHelper(utf8PropertyName, JsonConstants.NullValue); _tokenType = JsonTokenType.Null; } else { ReadOnlySpan<byte> nullValue = JsonConstants.NullValue; WriteLiteralSection(escapedPropertyNameSection, nullValue); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Null; } } private void WriteLiteralHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value) { WriteLiteralByOptions(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNull(string propertyName) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNull(propertyName.AsSpan()); } public void WriteNull(ReadOnlySpan<char> propertyName) { JsonWriterHelper.ValidateProperty(propertyName); ReadOnlySpan<byte> nullValue = JsonConstants.NullValue; WriteLiteralEscape(propertyName, nullValue); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Null; } public void WriteNull(ReadOnlySpan<byte> utf8PropertyName) { JsonWriterHelper.ValidateProperty(utf8PropertyName); ReadOnlySpan<byte> nullValue = JsonConstants.NullValue; WriteLiteralEscape(utf8PropertyName, nullValue); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Null; } public void WriteBoolean(JsonEncodedText propertyName, bool value) { if (value) { WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.TrueValue); _tokenType = JsonTokenType.True; } else { WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.FalseValue); _tokenType = JsonTokenType.False; } } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteBoolean(string propertyName, bool value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteBoolean(propertyName.AsSpan(), value); } public void WriteBoolean(ReadOnlySpan<char> propertyName, bool value) { JsonWriterHelper.ValidateProperty(propertyName); ReadOnlySpan<byte> value2 = value ? JsonConstants.TrueValue : JsonConstants.FalseValue; WriteLiteralEscape(propertyName, value2); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = (value ? JsonTokenType.True : JsonTokenType.False); } public void WriteBoolean(ReadOnlySpan<byte> utf8PropertyName, bool value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); ReadOnlySpan<byte> value2 = value ? JsonConstants.TrueValue : JsonConstants.FalseValue; WriteLiteralEscape(utf8PropertyName, value2); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = (value ? JsonTokenType.True : JsonTokenType.False); } private void WriteLiteralEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteLiteralEscapeProperty(propertyName, value, num); else WriteLiteralByOptions(propertyName, value); } private void WriteLiteralEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteLiteralEscapeProperty(utf8PropertyName, value, num); else WriteLiteralByOptions(utf8PropertyName, value); } private unsafe void WriteLiteralEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteLiteralByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteLiteralEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteLiteralByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteLiteralByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value) { ValidateWritingProperty(); if (_options.Indented) WriteLiteralIndented(propertyName, value); else WriteLiteralMinimized(propertyName, value); } private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value) { ValidateWritingProperty(); if (_options.Indented) WriteLiteralIndented(utf8PropertyName, value); else WriteLiteralMinimized(utf8PropertyName, value); } private void WriteLiteralMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> value) { int num = escapedPropertyName.Length * 3 + value.Length + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; value.CopyTo(span.Slice(BytesPending)); BytesPending += value.Length; } private void WriteLiteralMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value) { int num = escapedPropertyName.Length + value.Length + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; value.CopyTo(span.Slice(BytesPending)); BytesPending += value.Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteLiteralSection(ReadOnlySpan<byte> escapedPropertyNameSection, ReadOnlySpan<byte> value) { int num = escapedPropertyNameSection.Length + value.Length + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; escapedPropertyNameSection.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyNameSection.Length; value.CopyTo(span.Slice(BytesPending)); BytesPending += value.Length; } private void WriteLiteralIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + value.Length + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; value.CopyTo(span.Slice(BytesPending)); BytesPending += value.Length; } private void WriteLiteralIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + value.Length + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; value.CopyTo(span.Slice(BytesPending)); BytesPending += value.Length; } internal unsafe void WritePropertyName(bool value) { Span<byte> destination = new Span<byte>(stackalloc byte[5], 5); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } public void WriteNumber(JsonEncodedText propertyName, long value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteNumberByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNumber(string propertyName, long value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), value); } public void WriteNumber(ReadOnlySpan<char> propertyName, long value) { JsonWriterHelper.ValidateProperty(propertyName); WriteNumberEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, long value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteNumberEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } public void WriteNumber(JsonEncodedText propertyName, int value) { WriteNumber(propertyName, (long)value); } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteNumber(string propertyName, int value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), (long)value); } public void WriteNumber(ReadOnlySpan<char> propertyName, int value) { WriteNumber(propertyName, (long)value); } public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, int value) { WriteNumber(utf8PropertyName, (long)value); } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, long value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, long value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, long value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, long value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, long value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(propertyName, value); else WriteNumberMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, long value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(utf8PropertyName, value); else WriteNumberMinimized(utf8PropertyName, value); } private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, long value) { int num = escapedPropertyName.Length * 3 + 20 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, long value) { int num = escapedPropertyName.Length + 20 + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, long value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 20 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, long value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 20 + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal void WritePropertyName(int value) { WritePropertyName((long)value); } internal unsafe void WritePropertyName(long value) { Span<byte> destination = new Span<byte>(stackalloc byte[20], 20); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } public void WritePropertyName(JsonEncodedText propertyName) { WritePropertyNameHelper(propertyName.EncodedUtf8Bytes); } internal void WritePropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection) { if (_options.Indented) { ReadOnlySpan<byte> utf8PropertyName = escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3); WritePropertyNameHelper(utf8PropertyName); } else { WriteStringPropertyNameSection(escapedPropertyNameSection); _currentDepth &= 2147483647; _tokenType = JsonTokenType.PropertyName; _commentAfterNoneOrPropertyName = false; } } private void WritePropertyNameHelper(ReadOnlySpan<byte> utf8PropertyName) { WriteStringByOptionsPropertyName(utf8PropertyName); _currentDepth &= 2147483647; _tokenType = JsonTokenType.PropertyName; _commentAfterNoneOrPropertyName = false; } [System.Runtime.CompilerServices.NullableContext(1)] public void WritePropertyName(string propertyName) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WritePropertyName(propertyName.AsSpan()); } public void WritePropertyName(ReadOnlySpan<char> propertyName) { JsonWriterHelper.ValidateProperty(propertyName); int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(propertyName, num); else WriteStringByOptionsPropertyName(propertyName); _currentDepth &= 2147483647; _tokenType = JsonTokenType.PropertyName; _commentAfterNoneOrPropertyName = false; } private unsafe void WriteStringEscapeProperty([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<char> propertyName, int firstEscapeIndexProp) { char[] array = null; if (firstEscapeIndexProp != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> destination; if (maxEscapedLength > 128) { array = ArrayPool<char>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<char>(stackalloc byte[256], 128); JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); propertyName = destination.Slice(0, written); } WriteStringByOptionsPropertyName(propertyName); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private void WriteStringByOptionsPropertyName(ReadOnlySpan<char> propertyName) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndentedPropertyName(propertyName); else WriteStringMinimizedPropertyName(propertyName); } private void WriteStringMinimizedPropertyName(ReadOnlySpan<char> escapedPropertyName) { int num = escapedPropertyName.Length * 3 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; } private void WriteStringIndentedPropertyName(ReadOnlySpan<char> escapedPropertyName) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; } public void WritePropertyName(ReadOnlySpan<byte> utf8PropertyName) { JsonWriterHelper.ValidateProperty(utf8PropertyName); int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStringEscapeProperty(utf8PropertyName, num); else WriteStringByOptionsPropertyName(utf8PropertyName); _currentDepth &= 2147483647; _tokenType = JsonTokenType.PropertyName; _commentAfterNoneOrPropertyName = false; } private void WritePropertyNameUnescaped(ReadOnlySpan<byte> utf8PropertyName) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteStringByOptionsPropertyName(utf8PropertyName); _currentDepth &= 2147483647; _tokenType = JsonTokenType.PropertyName; _commentAfterNoneOrPropertyName = false; } private unsafe void WriteStringEscapeProperty([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<byte> utf8PropertyName, int firstEscapeIndexProp) { byte[] array = null; if (firstEscapeIndexProp != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> destination; if (maxEscapedLength > 256) { array = ArrayPool<byte>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<byte>(stackalloc byte[256], 256); JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); utf8PropertyName = destination.Slice(0, written); } WriteStringByOptionsPropertyName(utf8PropertyName); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteStringByOptionsPropertyName(ReadOnlySpan<byte> utf8PropertyName) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndentedPropertyName(utf8PropertyName); else WriteStringMinimizedPropertyName(utf8PropertyName); } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteStringMinimizedPropertyName(ReadOnlySpan<byte> escapedPropertyName) { int num = escapedPropertyName.Length + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteStringPropertyNameSection(ReadOnlySpan<byte> escapedPropertyNameSection) { int num = escapedPropertyNameSection.Length + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; escapedPropertyNameSection.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyNameSection.Length; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteStringIndentedPropertyName(ReadOnlySpan<byte> escapedPropertyName) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; } public void WriteString(JsonEncodedText propertyName, JsonEncodedText value) { WriteStringHelper(propertyName.EncodedUtf8Bytes, value.EncodedUtf8Bytes); } private void WriteStringHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { WriteStringByOptions(utf8PropertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteString(string propertyName, JsonEncodedText value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), value); } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteString(string propertyName, [System.Runtime.CompilerServices.Nullable(2)] string value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); if (value == null) WriteNull(propertyName.AsSpan()); else WriteString(propertyName.AsSpan(), value.AsSpan()); } public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) { JsonWriterHelper.ValidatePropertyAndValue(propertyName, value); WriteStringEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, utf8Value); WriteStringEscape(utf8PropertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(2)] public void WriteString(JsonEncodedText propertyName, string value) { if (value == null) WriteNull(propertyName); else WriteString(propertyName, value.AsSpan()); } public void WriteString(JsonEncodedText propertyName, ReadOnlySpan<char> value) { WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, value); } private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) { JsonWriterHelper.ValidateValue(value); int num = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); if (num != -1) WriteStringEscapeValueOnly(utf8PropertyName, value, num); else WriteStringByOptions(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString([System.Runtime.CompilerServices.Nullable(1)] string propertyName, ReadOnlySpan<char> value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), value); } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) { JsonWriterHelper.ValidatePropertyAndValue(utf8PropertyName, value); WriteStringEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(JsonEncodedText propertyName, ReadOnlySpan<byte> utf8Value) { WriteStringHelperEscapeValue(propertyName.EncodedUtf8Bytes, utf8Value); } private void WriteStringHelperEscapeValue(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidateValue(utf8Value); int num = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); if (num != -1) WriteStringEscapeValueOnly(utf8PropertyName, utf8Value, num); else WriteStringByOptions(utf8PropertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString([System.Runtime.CompilerServices.Nullable(1)] string propertyName, ReadOnlySpan<byte> utf8Value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteString(propertyName.AsSpan(), utf8Value); } public void WriteString(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidatePropertyAndValue(propertyName, utf8Value); WriteStringEscape(propertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<char> propertyName, JsonEncodedText value) { WriteStringHelperEscapeProperty(propertyName, value.EncodedUtf8Bytes); } private void WriteStringHelperEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidateProperty(propertyName); int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteStringEscapePropertyOnly(propertyName, utf8Value, num); else WriteStringByOptions(propertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<char> propertyName, [System.Runtime.CompilerServices.Nullable(2)] string value) { if (value == null) WriteNull(propertyName); else WriteString(propertyName, value.AsSpan()); } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, JsonEncodedText value) { WriteStringHelperEscapeProperty(utf8PropertyName, value.EncodedUtf8Bytes); } private void WriteStringHelperEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteStringEscapePropertyOnly(utf8PropertyName, utf8Value, num); else WriteStringByOptions(utf8PropertyName, utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } public void WriteString(ReadOnlySpan<byte> utf8PropertyName, [System.Runtime.CompilerServices.Nullable(2)] string value) { if (value == null) WriteNull(utf8PropertyName); else WriteString(utf8PropertyName, value.AsSpan()); } private unsafe void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> utf8Value, int firstEscapeIndex) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndex); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8Value, destination, firstEscapeIndex, _options.Encoder, out int written); WriteStringByOptions(escapedPropertyName, destination.Slice(0, written)); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private unsafe void WriteStringEscapeValueOnly(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> value, int firstEscapeIndex) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndex); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(value, destination, firstEscapeIndex, _options.Encoder, out int written); WriteStringByOptions(escapedPropertyName, destination.Slice(0, written)); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteStringEscapePropertyOnly(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> escapedValue, int firstEscapeIndex) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndex); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndex, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), escapedValue); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteStringEscapePropertyOnly(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> escapedValue, int firstEscapeIndex) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndex); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndex, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written), escapedValue); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) { int num = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); int num2 = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num + num2 != -2) WriteStringEscapePropertyOrValue(propertyName, value, num2, num); else WriteStringByOptions(propertyName, value); } private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { int num = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); int num2 = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num + num2 != -2) WriteStringEscapePropertyOrValue(utf8PropertyName, utf8Value, num2, num); else WriteStringByOptions(utf8PropertyName, utf8Value); } private void WriteStringEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) { int num = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); int num2 = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num + num2 != -2) WriteStringEscapePropertyOrValue(propertyName, utf8Value, num2, num); else WriteStringByOptions(propertyName, utf8Value); } private void WriteStringEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) { int num = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); int num2 = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num + num2 != -2) WriteStringEscapePropertyOrValue(utf8PropertyName, value, num2, num); else WriteStringByOptions(utf8PropertyName, value); } private unsafe void WriteStringEscapePropertyOrValue([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<char> propertyName, [System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<char> value, int firstEscapeIndexProp, int firstEscapeIndexVal) { char[] array = null; char[] array2 = null; if (firstEscapeIndexVal != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span<char> destination; if (maxEscapedLength > 128) { array = ArrayPool<char>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<char>(stackalloc byte[256], 128); JsonWriterHelper.EscapeString(value, destination, firstEscapeIndexVal, _options.Encoder, out int written); value = destination.Slice(0, written); } if (firstEscapeIndexProp != -1) { int maxEscapedLength2 = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> destination2; if (maxEscapedLength2 > 128) { array2 = ArrayPool<char>.Shared.Rent(maxEscapedLength2); destination2 = array2; } else destination2 = new Span<char>(stackalloc byte[256], 128); JsonWriterHelper.EscapeString(propertyName, destination2, firstEscapeIndexProp, _options.Encoder, out int written2); propertyName = destination2.Slice(0, written2); } WriteStringByOptions(propertyName, value); if (array != null) ArrayPool<char>.Shared.Return(array, false); if (array2 != null) ArrayPool<char>.Shared.Return(array2, false); } private unsafe void WriteStringEscapePropertyOrValue([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<byte> utf8PropertyName, [System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<byte> utf8Value, int firstEscapeIndexProp, int firstEscapeIndexVal) { byte[] array = null; byte[] array2 = null; if (firstEscapeIndexVal != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span<byte> destination; if (maxEscapedLength > 256) { array = ArrayPool<byte>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<byte>(stackalloc byte[256], 256); JsonWriterHelper.EscapeString(utf8Value, destination, firstEscapeIndexVal, _options.Encoder, out int written); utf8Value = destination.Slice(0, written); } if (firstEscapeIndexProp != -1) { int maxEscapedLength2 = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> destination2; if (maxEscapedLength2 > 256) { array2 = ArrayPool<byte>.Shared.Rent(maxEscapedLength2); destination2 = array2; } else destination2 = new Span<byte>(stackalloc byte[256], 256); JsonWriterHelper.EscapeString(utf8PropertyName, destination2, firstEscapeIndexProp, _options.Encoder, out int written2); utf8PropertyName = destination2.Slice(0, written2); } WriteStringByOptions(utf8PropertyName, utf8Value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); if (array2 != null) ArrayPool<byte>.Shared.Return(array2, false); } private unsafe void WriteStringEscapePropertyOrValue([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<char> propertyName, [System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<byte> utf8Value, int firstEscapeIndexProp, int firstEscapeIndexVal) { byte[] array = null; char[] array2 = null; if (firstEscapeIndexVal != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span<byte> destination; if (maxEscapedLength > 256) { array = ArrayPool<byte>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<byte>(stackalloc byte[256], 256); JsonWriterHelper.EscapeString(utf8Value, destination, firstEscapeIndexVal, _options.Encoder, out int written); utf8Value = destination.Slice(0, written); } if (firstEscapeIndexProp != -1) { int maxEscapedLength2 = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> destination2; if (maxEscapedLength2 > 128) { array2 = ArrayPool<char>.Shared.Rent(maxEscapedLength2); destination2 = array2; } else destination2 = new Span<char>(stackalloc byte[256], 128); JsonWriterHelper.EscapeString(propertyName, destination2, firstEscapeIndexProp, _options.Encoder, out int written2); propertyName = destination2.Slice(0, written2); } WriteStringByOptions(propertyName, utf8Value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); if (array2 != null) ArrayPool<char>.Shared.Return(array2, false); } private unsafe void WriteStringEscapePropertyOrValue([System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<byte> utf8PropertyName, [System.Runtime.CompilerServices.ScopedRef] ReadOnlySpan<char> value, int firstEscapeIndexProp, int firstEscapeIndexVal) { char[] array = null; byte[] array2 = null; if (firstEscapeIndexVal != -1) { int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span<char> destination; if (maxEscapedLength > 128) { array = ArrayPool<char>.Shared.Rent(maxEscapedLength); destination = array; } else destination = new Span<char>(stackalloc byte[256], 128); JsonWriterHelper.EscapeString(value, destination, firstEscapeIndexVal, _options.Encoder, out int written); value = destination.Slice(0, written); } if (firstEscapeIndexProp != -1) { int maxEscapedLength2 = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> destination2; if (maxEscapedLength2 > 256) { array2 = ArrayPool<byte>.Shared.Rent(maxEscapedLength2); destination2 = array2; } else destination2 = new Span<byte>(stackalloc byte[256], 256); JsonWriterHelper.EscapeString(utf8PropertyName, destination2, firstEscapeIndexProp, _options.Encoder, out int written2); utf8PropertyName = destination2.Slice(0, written2); } WriteStringByOptions(utf8PropertyName, value); if (array != null) ArrayPool<char>.Shared.Return(array, false); if (array2 != null) ArrayPool<byte>.Shared.Return(array2, false); } private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<char> value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(propertyName, value); else WriteStringMinimized(propertyName, value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> utf8Value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(utf8PropertyName, utf8Value); else WriteStringMinimized(utf8PropertyName, utf8Value); } private void WriteStringByOptions(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> utf8Value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(propertyName, utf8Value); else WriteStringMinimized(propertyName, utf8Value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<char> value) { ValidateWritingProperty(); if (_options.Indented) WriteStringIndented(utf8PropertyName, value); else WriteStringMinimized(utf8PropertyName, value); } private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue) { int num = (escapedPropertyName.Length + escapedValue.Length) * 3 + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue) { int num = escapedPropertyName.Length + escapedValue.Length + 5 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue) { int num = escapedPropertyName.Length * 3 + escapedValue.Length + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private void WriteStringMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue) { int num = escapedValue.Length * 3 + escapedPropertyName.Length + 6; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<char> escapedValue) { int indentation = Indentation; int num = indentation + (escapedPropertyName.Length + escapedValue.Length) * 3 + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> escapedValue) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + escapedValue.Length + 6 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> escapedValue) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + escapedValue.Length + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<char> escapedValue) { int indentation = Indentation; int num = indentation + escapedValue.Length * 3 + escapedPropertyName.Length + 7 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } [CLSCompliant(false)] public void WriteNumber(JsonEncodedText propertyName, ulong value) { ReadOnlySpan<byte> encodedUtf8Bytes = propertyName.EncodedUtf8Bytes; WriteNumberByOptions(encodedUtf8Bytes, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [System.Runtime.CompilerServices.NullableContext(1)] [CLSCompliant(false)] public void WriteNumber(string propertyName, ulong value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), value); } [CLSCompliant(false)] public void WriteNumber(ReadOnlySpan<char> propertyName, ulong value) { JsonWriterHelper.ValidateProperty(propertyName); WriteNumberEscape(propertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [CLSCompliant(false)] public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, ulong value) { JsonWriterHelper.ValidateProperty(utf8PropertyName); WriteNumberEscape(utf8PropertyName, value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } [CLSCompliant(false)] public void WriteNumber(JsonEncodedText propertyName, uint value) { WriteNumber(propertyName, (ulong)value); } [System.Runtime.CompilerServices.NullableContext(1)] [CLSCompliant(false)] public void WriteNumber(string propertyName, uint value) { if (propertyName == null) ThrowHelper.ThrowArgumentNullException("propertyName"); WriteNumber(propertyName.AsSpan(), (ulong)value); } [CLSCompliant(false)] public void WriteNumber(ReadOnlySpan<char> propertyName, uint value) { WriteNumber(propertyName, (ulong)value); } [CLSCompliant(false)] public void WriteNumber(ReadOnlySpan<byte> utf8PropertyName, uint value) { WriteNumber(utf8PropertyName, (ulong)value); } private void WriteNumberEscape(ReadOnlySpan<char> propertyName, ulong value) { int num = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(propertyName, value, num); else WriteNumberByOptions(propertyName, value); } private void WriteNumberEscape(ReadOnlySpan<byte> utf8PropertyName, ulong value) { int num = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder); if (num != -1) WriteNumberEscapeProperty(utf8PropertyName, value, num); else WriteNumberByOptions(utf8PropertyName, value); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<char> propertyName, ulong value, int firstEscapeIndexProp) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(propertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<char>.Shared.Return(array, false); } private unsafe void WriteNumberEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ulong value, int firstEscapeIndexProp) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8PropertyName, destination, firstEscapeIndexProp, _options.Encoder, out int written); WriteNumberByOptions(destination.Slice(0, written), value); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } private void WriteNumberByOptions(ReadOnlySpan<char> propertyName, ulong value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(propertyName, value); else WriteNumberMinimized(propertyName, value); } private void WriteNumberByOptions(ReadOnlySpan<byte> utf8PropertyName, ulong value) { ValidateWritingProperty(); if (_options.Indented) WriteNumberIndented(utf8PropertyName, value); else WriteNumberMinimized(utf8PropertyName, value); } private void WriteNumberMinimized(ReadOnlySpan<char> escapedPropertyName, ulong value) { int num = escapedPropertyName.Length * 3 + 20 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberMinimized(ReadOnlySpan<byte> escapedPropertyName, ulong value) { int num = escapedPropertyName.Length + 20 + 3 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<char> escapedPropertyName, ulong value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length * 3 + 20 + 5 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; TranscodeAndWrite(escapedPropertyName, span); span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberIndented(ReadOnlySpan<byte> escapedPropertyName, ulong value) { int indentation = Indentation; int num = indentation + escapedPropertyName.Length + 20 + 4 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; span[BytesPending++] = 34; escapedPropertyName.CopyTo(span.Slice(BytesPending)); BytesPending += escapedPropertyName.Length; span[BytesPending++] = 34; span[BytesPending++] = 58; span[BytesPending++] = 32; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal void WritePropertyName(uint value) { WritePropertyName((ulong)value); } internal unsafe void WritePropertyName(ulong value) { Span<byte> destination = new Span<byte>(stackalloc byte[20], 20); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WritePropertyNameUnescaped(destination.Slice(0, bytesWritten)); } public void WriteBase64StringValue(ReadOnlySpan<byte> bytes) { WriteBase64ByOptions(bytes); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteBase64ByOptions(ReadOnlySpan<byte> bytes) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteBase64Indented(bytes); else WriteBase64Minimized(bytes); } private void WriteBase64Minimized(ReadOnlySpan<byte> bytes) { if (bytes.Length > 1610612733) ThrowHelper.ThrowArgumentException_ValueTooLarge(bytes.Length); int num = Base64.GetMaxEncodedToUtf8Length(bytes.Length) + 3; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } private void WriteBase64Indented(ReadOnlySpan<byte> bytes) { int indentation = Indentation; int num = indentation + 3 + _newLineLength; int num2 = 1610612733 - num; if (bytes.Length > num2) ThrowHelper.ThrowArgumentException_ValueTooLarge(bytes.Length); int num3 = Base64.GetMaxEncodedToUtf8Length(bytes.Length) + num; if (_memory.Length - BytesPending < num3) Grow(num3); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; Base64EncodeAndWrite(bytes, span); span[BytesPending++] = 34; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteCommentValue(string value) { if (value == null) ThrowHelper.ThrowArgumentNullException("value"); WriteCommentValue(value.AsSpan()); } public void WriteCommentValue(ReadOnlySpan<char> value) { JsonWriterHelper.ValidateValue(value); if (value.IndexOf(s_singleLineCommentDelimiter) != -1) ThrowHelper.ThrowArgumentException_InvalidCommentValue(); WriteCommentByOptions(value); JsonTokenType tokenType = _tokenType; if ((tokenType == JsonTokenType.None || tokenType == JsonTokenType.PropertyName) ? true : false) _commentAfterNoneOrPropertyName = true; } private void WriteCommentByOptions(ReadOnlySpan<char> value) { if (_options.Indented) WriteCommentIndented(value); else WriteCommentMinimized(value); } private void WriteCommentMinimized(ReadOnlySpan<char> value) { int num = value.Length * 3 + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; span[BytesPending++] = 47; span[BytesPending++] = 42; if (JsonWriterHelper.ToUtf8(value, span.Slice(BytesPending), out int written) == OperationStatus.InvalidData) ThrowHelper.ThrowArgumentException_InvalidUTF16(value[written]); BytesPending += written; span[BytesPending++] = 42; span[BytesPending++] = 47; } private void WriteCommentIndented(ReadOnlySpan<char> value) { int indentation = Indentation; int num = indentation + value.Length * 3 + 4 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_tokenType != 0 || _commentAfterNoneOrPropertyName) { WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 47; span[BytesPending++] = 42; if (JsonWriterHelper.ToUtf8(value, span.Slice(BytesPending), out int written) == OperationStatus.InvalidData) ThrowHelper.ThrowArgumentException_InvalidUTF16(value[written]); BytesPending += written; span[BytesPending++] = 42; span[BytesPending++] = 47; } public void WriteCommentValue(ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidateValue(utf8Value); if (utf8Value.IndexOf(SingleLineCommentDelimiterUtf8) != -1) ThrowHelper.ThrowArgumentException_InvalidCommentValue(); if (!JsonWriterHelper.IsValidUtf8String(utf8Value)) ThrowHelper.ThrowArgumentException_InvalidUTF8(utf8Value); WriteCommentByOptions(utf8Value); JsonTokenType tokenType = _tokenType; if ((tokenType == JsonTokenType.None || tokenType == JsonTokenType.PropertyName) ? true : false) _commentAfterNoneOrPropertyName = true; } private void WriteCommentByOptions(ReadOnlySpan<byte> utf8Value) { if (_options.Indented) WriteCommentIndented(utf8Value); else WriteCommentMinimized(utf8Value); } private void WriteCommentMinimized(ReadOnlySpan<byte> utf8Value) { int num = utf8Value.Length + 4; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; span[BytesPending++] = 47; span[BytesPending++] = 42; utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; span[BytesPending++] = 42; span[BytesPending++] = 47; } private void WriteCommentIndented(ReadOnlySpan<byte> utf8Value) { int indentation = Indentation; int num = indentation + utf8Value.Length + 4 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_tokenType != 0 || _commentAfterNoneOrPropertyName) { WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 47; span[BytesPending++] = 42; utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; span[BytesPending++] = 42; span[BytesPending++] = 47; } public void WriteStringValue(DateTime value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteStringValueIndented(value); else WriteStringValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringValueMinimized(DateTime value) { int num = 36; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringValueIndented(DateTime value) { int indentation = Indentation; int num = indentation + 33 + 3 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } public void WriteStringValue(DateTimeOffset value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteStringValueIndented(value); else WriteStringValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringValueMinimized(DateTimeOffset value) { int num = 36; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringValueIndented(DateTimeOffset value) { int indentation = Indentation; int num = indentation + 33 + 3 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; JsonWriterHelper.WriteDateTimeOffsetTrimmed(span.Slice(BytesPending), value, out int bytesWritten); BytesPending += bytesWritten; span[BytesPending++] = 34; } public void WriteNumberValue(decimal value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(value); else WriteNumberValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(decimal value) { int num = 32; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberValueIndented(decimal value) { int indentation = Indentation; int num = indentation + 31 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal unsafe void WriteNumberValueAsString(decimal value) { Span<byte> destination = new Span<byte>(stackalloc byte[31], 31); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WriteNumberValueAsStringUnescaped(destination.Slice(0, bytesWritten)); } public void WriteNumberValue(double value) { JsonWriterHelper.ValidateDouble(value); if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(value); else WriteNumberValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(double value) { int num = 129; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberValueIndented(double value) { int indentation = Indentation; int num = indentation + 128 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } TryFormatDouble(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private static bool TryFormatDouble(double value, Span<byte> destination, out int bytesWritten) { string text = value.ToString("G17", CultureInfo.InvariantCulture); if (text.Length <= destination.Length) try { byte[] bytes = Encoding.UTF8.GetBytes(text); if (bytes.Length <= destination.Length) { bytes.CopyTo(destination); bytesWritten = bytes.Length; return true; } bytesWritten = 0; return false; } catch { bytesWritten = 0; return false; } bytesWritten = 0; return false; } internal unsafe void WriteNumberValueAsString(double value) { Span<byte> destination = new Span<byte>(stackalloc byte[128], 128); TryFormatDouble(value, destination, out int bytesWritten); WriteNumberValueAsStringUnescaped(destination.Slice(0, bytesWritten)); } internal void WriteFloatingPointConstant(double value) { if (double.IsNaN(value)) WriteNumberValueAsStringUnescaped(JsonConstants.NaNValue); else if (double.IsPositiveInfinity(value)) { WriteNumberValueAsStringUnescaped(JsonConstants.PositiveInfinityValue); } else if (double.IsNegativeInfinity(value)) { WriteNumberValueAsStringUnescaped(JsonConstants.NegativeInfinityValue); } else { WriteNumberValue(value); } } public void WriteNumberValue(float value) { JsonWriterHelper.ValidateSingle(value); if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(value); else WriteNumberValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(float value) { int num = 129; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private void WriteNumberValueIndented(float value) { int indentation = Indentation; int num = indentation + 128 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } TryFormatSingle(value, span.Slice(BytesPending), out int bytesWritten); BytesPending += bytesWritten; } private static bool TryFormatSingle(float value, Span<byte> destination, out int bytesWritten) { string text = value.ToString("G9", CultureInfo.InvariantCulture); if (text.Length <= destination.Length) try { byte[] bytes = Encoding.UTF8.GetBytes(text); if (bytes.Length <= destination.Length) { bytes.CopyTo(destination); bytesWritten = bytes.Length; return true; } bytesWritten = 0; return false; } catch { bytesWritten = 0; return false; } bytesWritten = 0; return false; } internal unsafe void WriteNumberValueAsString(float value) { Span<byte> destination = new Span<byte>(stackalloc byte[128], 128); TryFormatSingle(value, destination, out int bytesWritten); WriteNumberValueAsStringUnescaped(destination.Slice(0, bytesWritten)); } internal void WriteFloatingPointConstant(float value) { if (float.IsNaN(value)) WriteNumberValueAsStringUnescaped(JsonConstants.NaNValue); else if (float.IsPositiveInfinity(value)) { WriteNumberValueAsStringUnescaped(JsonConstants.PositiveInfinityValue); } else if (float.IsNegativeInfinity(value)) { WriteNumberValueAsStringUnescaped(JsonConstants.NegativeInfinityValue); } else { WriteNumberValue(value); } } internal void WriteNumberValue(ReadOnlySpan<byte> utf8FormattedNumber) { JsonWriterHelper.ValidateValue(utf8FormattedNumber); JsonWriterHelper.ValidateNumber(utf8FormattedNumber); if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(utf8FormattedNumber); else WriteNumberValueMinimized(utf8FormattedNumber); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(ReadOnlySpan<byte> utf8Value) { int num = utf8Value.Length + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; } private void WriteNumberValueIndented(ReadOnlySpan<byte> utf8Value) { int indentation = Indentation; int num = indentation + utf8Value.Length + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; } public void WriteStringValue(Guid value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteStringValueIndented(value); else WriteStringValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringValueMinimized(Guid value) { int num = 39; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void WriteStringValueIndented(Guid value) { int indentation = Indentation; int num = indentation + 36 + 3 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; span[BytesPending++] = 34; } private void ValidateWritingValue() { if (_inObject) { if (_tokenType != JsonTokenType.PropertyName) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueWithinObject, 0, _options.MaxDepth, 0, _tokenType); } else if (CurrentDepth == 0 && _tokenType != 0) { ThrowHelper.ThrowInvalidOperationException(ExceptionResource.CannotWriteValueAfterPrimitiveOrClose, 0, _options.MaxDepth, 0, _tokenType); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void Base64EncodeAndWrite(ReadOnlySpan<byte> bytes, Span<byte> output) { Span<byte> utf = output.Slice(BytesPending); Base64.EncodeToUtf8(bytes, utf, out int _, out int bytesWritten, true); BytesPending += bytesWritten; } public void WriteNullValue() { WriteLiteralByOptions(JsonConstants.NullValue); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Null; } public void WriteBooleanValue(bool value) { if (value) { WriteLiteralByOptions(JsonConstants.TrueValue); _tokenType = JsonTokenType.True; } else { WriteLiteralByOptions(JsonConstants.FalseValue); _tokenType = JsonTokenType.False; } SetFlagToAddListSeparatorBeforeNextItem(); } private void WriteLiteralByOptions(ReadOnlySpan<byte> utf8Value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteLiteralIndented(utf8Value); else WriteLiteralMinimized(utf8Value); } private void WriteLiteralMinimized(ReadOnlySpan<byte> utf8Value) { int num = utf8Value.Length + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; } private void WriteLiteralIndented(ReadOnlySpan<byte> utf8Value) { int indentation = Indentation; int num = indentation + utf8Value.Length + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } utf8Value.CopyTo(span.Slice(BytesPending)); BytesPending += utf8Value.Length; } [System.Runtime.CompilerServices.NullableContext(1)] public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] string json, bool skipInputValidation = false) { if (!_options.SkipValidation) ValidateWritingValue(); if (json == null) throw new ArgumentNullException("json"); TranscodeAndWriteRawValue(json.AsSpan(), skipInputValidation); } public void WriteRawValue([System.Diagnostics.CodeAnalysis.StringSyntax("Json")] ReadOnlySpan<char> json, bool skipInputValidation = false) { if (!_options.SkipValidation) ValidateWritingValue(); TranscodeAndWriteRawValue(json, skipInputValidation); } public void WriteRawValue(ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false) { if (!_options.SkipValidation) ValidateWritingValue(); if (utf8Json.Length == 2147483647) ThrowHelper.ThrowArgumentException_ValueTooLarge(2147483647); WriteRawValueCore(utf8Json, skipInputValidation); } public void WriteRawValue(ReadOnlySequence<byte> utf8Json, bool skipInputValidation = false) { if (!_options.SkipValidation) ValidateWritingValue(); long length = utf8Json.Length; if (length == 0) ThrowHelper.ThrowArgumentException(System.SR.ExpectedJsonTokens); if (length >= 2147483647) ThrowHelper.ThrowArgumentException_ValueTooLarge(length); if (skipInputValidation) _tokenType = JsonTokenType.String; else { Utf8JsonReader utf8JsonReader = new Utf8JsonReader(utf8Json, default(JsonReaderOptions)); while (utf8JsonReader.Read()) { } _tokenType = utf8JsonReader.TokenType; } int num = (int)length; int num2 = num + 1; if (_memory.Length - BytesPending < num2) Grow(num2); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; ref utf8Json.CopyTo(span.Slice(BytesPending)); BytesPending += num; SetFlagToAddListSeparatorBeforeNextItem(); } private unsafe void TranscodeAndWriteRawValue(ReadOnlySpan<char> json, bool skipInputValidation) { if (json.Length > 715827882) ThrowHelper.ThrowArgumentException_ValueTooLarge(json.Length); byte[] array = null; Span<byte> span = (json.Length > 85) ? ((Span<byte>)(((long)json.Length > 349525) ? new byte[JsonReaderHelper.GetUtf8ByteCount(json)] : (array = ArrayPool<byte>.Shared.Rent(json.Length * 3)))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> span2 = span; try { int utf8FromText = JsonReaderHelper.GetUtf8FromText(json, span2); span2 = span2.Slice(0, utf8FromText); WriteRawValueCore(span2, skipInputValidation); } finally { if (array != null) { span2.Clear(); ArrayPool<byte>.Shared.Return(array, false); } } } private void WriteRawValueCore(ReadOnlySpan<byte> utf8Json, bool skipInputValidation) { int length = utf8Json.Length; if (length == 0) ThrowHelper.ThrowArgumentException(System.SR.ExpectedJsonTokens); if (skipInputValidation) _tokenType = JsonTokenType.String; else { Utf8JsonReader utf8JsonReader = new Utf8JsonReader(utf8Json, default(JsonReaderOptions)); while (utf8JsonReader.Read()) { } _tokenType = utf8JsonReader.TokenType; } int num = length + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; utf8Json.CopyTo(span.Slice(BytesPending)); BytesPending += length; SetFlagToAddListSeparatorBeforeNextItem(); } public void WriteNumberValue(int value) { WriteNumberValue((long)value); } public void WriteNumberValue(long value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(value); else WriteNumberValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(long value) { int num = 21; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberValueIndented(long value) { int indentation = Indentation; int num = indentation + 20 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal unsafe void WriteNumberValueAsString(long value) { Span<byte> destination = new Span<byte>(stackalloc byte[20], 20); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WriteNumberValueAsStringUnescaped(destination.Slice(0, bytesWritten)); } public void WriteStringValue(JsonEncodedText value) { ReadOnlySpan<byte> encodedUtf8Bytes = value.EncodedUtf8Bytes; WriteStringByOptions(encodedUtf8Bytes); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [System.Runtime.CompilerServices.NullableContext(2)] public void WriteStringValue(string value) { if (value == null) WriteNullValue(); else WriteStringValue(value.AsSpan()); } public void WriteStringValue(ReadOnlySpan<char> value) { JsonWriterHelper.ValidateValue(value); WriteStringEscape(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringEscape(ReadOnlySpan<char> value) { int num = JsonWriterHelper.NeedsEscaping(value, _options.Encoder); if (num != -1) WriteStringEscapeValue(value, num); else WriteStringByOptions(value); } private void WriteStringByOptions(ReadOnlySpan<char> value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteStringIndented(value); else WriteStringMinimized(value); } private void WriteStringMinimized(ReadOnlySpan<char> escapedValue) { int num = escapedValue.Length * 3 + 3; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<char> escapedValue) { int indentation = Indentation; int num = indentation + escapedValue.Length * 3 + 3 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; TranscodeAndWrite(escapedValue, span); span[BytesPending++] = 34; } private unsafe void WriteStringEscapeValue(ReadOnlySpan<char> value, int firstEscapeIndexVal) { char[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(value.Length, firstEscapeIndexVal); Span<char> span = (maxEscapedLength > 128) ? ((Span<char>)(array = ArrayPool<char>.Shared.Rent(maxEscapedLength))) : new Span<char>(stackalloc byte[256], 128); Span<char> destination = span; JsonWriterHelper.EscapeString(value, destination, firstEscapeIndexVal, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written)); if (array != null) ArrayPool<char>.Shared.Return(array, false); } public void WriteStringValue(ReadOnlySpan<byte> utf8Value) { JsonWriterHelper.ValidateValue(utf8Value); WriteStringEscape(utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } private void WriteStringEscape(ReadOnlySpan<byte> utf8Value) { int num = JsonWriterHelper.NeedsEscaping(utf8Value, _options.Encoder); if (num != -1) WriteStringEscapeValue(utf8Value, num); else WriteStringByOptions(utf8Value); } private void WriteStringByOptions(ReadOnlySpan<byte> utf8Value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteStringIndented(utf8Value); else WriteStringMinimized(utf8Value); } private void WriteStringMinimized(ReadOnlySpan<byte> escapedValue) { int num = escapedValue.Length + 2 + 1; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private void WriteStringIndented(ReadOnlySpan<byte> escapedValue) { int indentation = Indentation; int num = indentation + escapedValue.Length + 2 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } span[BytesPending++] = 34; escapedValue.CopyTo(span.Slice(BytesPending)); BytesPending += escapedValue.Length; span[BytesPending++] = 34; } private unsafe void WriteStringEscapeValue(ReadOnlySpan<byte> utf8Value, int firstEscapeIndexVal) { byte[] array = null; int maxEscapedLength = JsonWriterHelper.GetMaxEscapedLength(utf8Value.Length, firstEscapeIndexVal); Span<byte> span = (maxEscapedLength > 256) ? ((Span<byte>)(array = ArrayPool<byte>.Shared.Rent(maxEscapedLength))) : new Span<byte>(stackalloc byte[256], 256); Span<byte> destination = span; JsonWriterHelper.EscapeString(utf8Value, destination, firstEscapeIndexVal, _options.Encoder, out int written); WriteStringByOptions(destination.Slice(0, written)); if (array != null) ArrayPool<byte>.Shared.Return(array, false); } internal void WriteNumberValueAsStringUnescaped(ReadOnlySpan<byte> utf8Value) { WriteStringByOptions(utf8Value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.String; } [CLSCompliant(false)] public void WriteNumberValue(uint value) { WriteNumberValue((ulong)value); } [CLSCompliant(false)] public void WriteNumberValue(ulong value) { if (!_options.SkipValidation) ValidateWritingValue(); if (_options.Indented) WriteNumberValueIndented(value); else WriteNumberValueMinimized(value); SetFlagToAddListSeparatorBeforeNextItem(); _tokenType = JsonTokenType.Number; } private void WriteNumberValueMinimized(ulong value) { int num = 21; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } private void WriteNumberValueIndented(ulong value) { int indentation = Indentation; int num = indentation + 20 + 1 + _newLineLength; if (_memory.Length - BytesPending < num) Grow(num); Span<byte> span = _memory.Span; if (_currentDepth < 0) span[BytesPending++] = 44; if (_tokenType != JsonTokenType.PropertyName) { if (_tokenType != 0) WriteNewLine(span); WriteIndentation(span.Slice(BytesPending), indentation); BytesPending += indentation; } int bytesWritten = default(int); Utf8Formatter.TryFormat(value, span.Slice(BytesPending), out bytesWritten, default(StandardFormat)); BytesPending += bytesWritten; } internal unsafe void WriteNumberValueAsString(ulong value) { Span<byte> destination = new Span<byte>(stackalloc byte[20], 20); int bytesWritten = default(int); Utf8Formatter.TryFormat(value, destination, out bytesWritten, default(StandardFormat)); WriteNumberValueAsStringUnescaped(destination.Slice(0, bytesWritten)); } } }