private Span <byte> WriteValueFormatted(int bytesNeeded, int indent, out int idx) { if (_currentDepth >= 0) { bytesNeeded--; } if (_tokenType == JsonTokenType.None) { bytesNeeded -= JsonWriterHelper.NewLineUtf8.Length; } Span <byte> byteBuffer = GetSpan(bytesNeeded); idx = 0; if (_currentDepth < 0) { byteBuffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref byteBuffer, ref idx); } idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); return(byteBuffer); }
private void WriteCommentFormattedWithEncodingValue(ref ReadOnlySpan <byte> escapedValue) { int indent = Indentation; // This is guaranteed not to overflow. Debug.Assert(int.MaxValue - escapedValue.Length / 2 * 3 - 4 - JsonWriterHelper.NewLineUtf8.Length - indent >= 0); // Calculated based on the following: '\r\n /*encoded value*/' int bytesNeeded = escapedValue.Length / 2 * 3 + 4 + JsonWriterHelper.NewLineUtf8.Length + indent; if (_tokenType == JsonTokenType.None) { bytesNeeded -= JsonWriterHelper.NewLineUtf8.Length; } Ensure(bytesNeeded); Span <byte> byteBuffer = _buffer; int idx = 0; if (_tokenType != JsonTokenType.None) { WriteNewLine(ref byteBuffer, ref idx); } idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); byteBuffer[idx++] = JsonConstants.Solidus; byteBuffer[idx++] = (byte)'*'; // TODO: Replace with JsonConstants.Asterisk OperationStatus status = Encodings.Utf16.ToUtf8(escapedValue, byteBuffer.Slice(idx), out int consumed, out int written); Debug.Assert(status != OperationStatus.DestinationTooSmall); if (status != OperationStatus.Done) { JsonThrowHelper.ThrowArgumentExceptionInvalidUtf8String(); } Debug.Assert(consumed == escapedValue.Length); idx += written; byteBuffer[idx++] = (byte)'*'; // TODO: Replace with JsonConstants.Asterisk byteBuffer[idx++] = JsonConstants.Solidus; Advance(idx); }
private void WriteStartFormatted(byte token) { int indent = Indentation; // This is guaranteed not to overflow. Debug.Assert(int.MaxValue - JsonWriterHelper.NewLineUtf8.Length - 2 - indent >= 0); // Calculated based on the following: ',\r\n [' OR ',\r\n {' int bytesNeeded = JsonWriterHelper.NewLineUtf8.Length + 2 + indent; if (_currentDepth >= 0) { bytesNeeded--; } if (_tokenType == JsonTokenType.None) { bytesNeeded -= JsonWriterHelper.NewLineUtf8.Length; } Span <byte> byteBuffer = GetSpan(bytesNeeded); int idx = 0; if (_currentDepth < 0) { byteBuffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref byteBuffer, ref idx); } idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); byteBuffer[idx++] = token; Debug.Assert(idx == bytesNeeded); Advance(idx); }
private void WriteEndFormatted(byte token) { // Do not format/indent empty JSON object/array. if ((token == JsonConstants.CloseBrace && _tokenType == JsonTokenType.StartObject) || (token == JsonConstants.CloseBracket && _tokenType == JsonTokenType.StartArray)) { WriteEndFast(token); } else { // Necessary if WriteEndX is called without a corresponding WriteStartX first. // Checking for int.MaxValue because int.MinValue - 1 = int.MaxValue if (_currentDepth == int.MaxValue) { _currentDepth = 0; } int indent = Indentation; // This is guaranteed not to overflow. Debug.Assert(int.MaxValue - JsonWriterHelper.NewLineUtf8.Length - 1 - indent >= 0); // For new line (\r\n or \n), indentation (based on depth) and end token ('}' or ']'). int bytesNeeded = JsonWriterHelper.NewLineUtf8.Length + 1 + indent; Span <byte> byteBuffer = GetSpan(bytesNeeded); int idx = 0; WriteNewLine(ref byteBuffer, ref idx); idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); byteBuffer[idx++] = token; Debug.Assert(idx == bytesNeeded); Advance(idx); } }
private void WriteCommentFormatted(ref ReadOnlySpan <byte> escapedValue) { int indent = Indentation; // This is guaranteed not to overflow. Debug.Assert(int.MaxValue - JsonWriterHelper.NewLineUtf8.Length - escapedValue.Length - 4 - indent >= 0); // Calculated based on the following: ',\r\n /*value*/' int bytesNeeded = 4 + JsonWriterHelper.NewLineUtf8.Length + indent + escapedValue.Length; if (_tokenType == JsonTokenType.None) { bytesNeeded -= JsonWriterHelper.NewLineUtf8.Length; } Ensure(bytesNeeded); Span <byte> byteBuffer = _buffer; int idx = 0; if (_tokenType != JsonTokenType.None) { WriteNewLine(ref byteBuffer, ref idx); } idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); byteBuffer[idx++] = JsonConstants.Solidus; byteBuffer[idx++] = (byte)'*'; // TODO: Replace with JsonConstants.Asterisk escapedValue.CopyTo(byteBuffer.Slice(idx)); idx += escapedValue.Length; byteBuffer[idx++] = (byte)'*'; // TODO: Replace with JsonConstants.Asterisk byteBuffer[idx++] = JsonConstants.Solidus; Advance(idx); }
private Span <byte> WritePropertyNameFormatted(ref ReadOnlySpan <byte> propertyName, int bytesNeeded, int indent, out int idx) { if (_currentDepth >= 0) { bytesNeeded--; } if (_tokenType == JsonTokenType.None) { bytesNeeded -= JsonWriterHelper.NewLineUtf8.Length; } Span <byte> byteBuffer = GetSpan(bytesNeeded); idx = 0; if (_currentDepth < 0) { byteBuffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref byteBuffer, ref idx); } idx += JsonWriterHelper.WriteIndentation(byteBuffer.Slice(idx, indent)); byteBuffer[idx++] = JsonConstants.Quote; propertyName.CopyTo(byteBuffer.Slice(idx)); idx += propertyName.Length; byteBuffer[idx++] = JsonConstants.Quote; byteBuffer[idx++] = JsonConstants.KeyValueSeperator; byteBuffer[idx++] = JsonConstants.Space; return(byteBuffer); }