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); }
private void WriteEndIndented(byte token) { // Do not format/indent empty JSON object/array. if ((_tokenType == JsonTokenType.StartObject && token == JsonConstants.CloseBrace) || (_tokenType == JsonTokenType.StartArray && token == JsonConstants.CloseBracket)) { WriteEndMinimized(token); } else { int idx = 0; WriteNewLine(ref idx); int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = token; Advance(idx); } }
private void WriteCommentIndented(ref ReadOnlySpan <char> escapedValue) { int idx = 0; if (_currentDepth < 0) { while (_buffer.Length <= idx) { GrowAndEnsure(); } _buffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref idx); } int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } WriteCommentValue(ref escapedValue, ref idx); Advance(idx); }
private void WriteValueUtf8Pretty(long value, int bytesNeeded) { bool insertNegationSign = false; if (value < 0) { insertNegationSign = true; value = -value; bytesNeeded += 1; } int digitCount = JsonWriterHelper.CountDigits((ulong)value); bytesNeeded += digitCount; Span <byte> byteBuffer = EnsureBuffer(bytesNeeded); int idx = 0; if (_indent < 0) { byteBuffer[idx++] = JsonConstants.ListSeperator; } _indent |= 1 << 31; idx += AddNewLineAndIndentation(byteBuffer.Slice(idx)); if (insertNegationSign) { byteBuffer[idx++] = (byte)'-'; } JsonWriterHelper.WriteDigitsUInt64D((ulong)value, byteBuffer.Slice(idx, digitCount)); _bufferWriter.Advance(bytesNeeded); }
public void WriteComment(ReadOnlySpan <char> utf16Text) { JsonWriterHelper.ValidateValue(ref utf16Text); WriteCommentWithEncodingValue(MemoryMarshal.AsBytes(utf16Text)); }
private int WritePropertyNameIndented(ref ReadOnlySpan <byte> escapedPropertyName) { int idx = 0; if (_currentDepth < 0) { while (_buffer.Length <= idx) { GrowAndEnsure(); } _buffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref idx); } int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; CopyLoop(ref escapedPropertyName, ref idx); while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.KeyValueSeperator; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Space; return(idx); }
private int WritePropertyNameIndented(ref ReadOnlySpan <char> escapedPropertyName) { int idx = 0; if (_currentDepth < 0) { while (_buffer.Length <= idx) { GrowAndEnsure(); } _buffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref idx); } int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; ReadOnlySpan <byte> byteSpan = MemoryMarshal.AsBytes(escapedPropertyName); int partialConsumed = 0; while (true) { OperationStatus status = Buffers.Text.TextEncodings.Utf16.ToUtf8(byteSpan.Slice(partialConsumed), _buffer.Slice(idx), out int consumed, out int written); idx += written; if (status == OperationStatus.Done) { break; } partialConsumed += consumed; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.KeyValueSeperator; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Space; return(idx); }
private void WriteAttributeUtf8Pretty(ReadOnlySpan <byte> nameSpan, long value) { // quote {name} quote colon, hence 3 int bytesNeeded = 3; if (_indent < 0) { bytesNeeded++; } if (Encodings.Utf16.ToUtf8Length(nameSpan, out int bytesNeededName) != OperationStatus.Done) { JsonThrowHelper.ThrowArgumentExceptionInvalidUtf8String(); } bytesNeeded += bytesNeededName; // For the new line, \r\n or \n, and the space after the colon bytesNeeded += JsonWriterHelper.NewLineUtf8.Length + 1 + (_indent & RemoveFlagsBitMask) * 2; bool insertNegationSign = false; if (value < 0) { insertNegationSign = true; value = -value; bytesNeeded += 1; } int digitCount = JsonWriterHelper.CountDigits((ulong)value); bytesNeeded += digitCount; Span <byte> byteBuffer = EnsureBuffer(bytesNeeded); int idx = 0; if (_indent < 0) { byteBuffer[idx++] = JsonConstants.ListSeperator; } idx += AddNewLineAndIndentation(byteBuffer.Slice(idx)); byteBuffer[idx++] = JsonConstants.Quote; OperationStatus status = Encodings.Utf16.ToUtf8(nameSpan, byteBuffer.Slice(idx), out int consumed, out int written); if (status != OperationStatus.Done) { JsonThrowHelper.ThrowFormatException(); } Debug.Assert(consumed == nameSpan.Length); idx += written; byteBuffer[idx++] = JsonConstants.Quote; byteBuffer[idx++] = JsonConstants.KeyValueSeperator; byteBuffer[idx++] = JsonConstants.Space; _indent |= 1 << 31; if (insertNegationSign) { byteBuffer[idx++] = (byte)'-'; } JsonWriterHelper.WriteDigitsUInt64D((ulong)value, byteBuffer.Slice(idx, digitCount)); _bufferWriter.Advance(bytesNeeded); }