|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Buffers;
using System.Buffers.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace System.Text.Json
{
public sealed partial class Utf8JsonWriter
{
/// <summary>
/// Writes the pre-encoded property name and the JSON literal "null" as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The JSON-encoded name of the property to write.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
public void WriteNull(JsonEncodedText propertyName)
{
WriteLiteralHelper(propertyName.EncodedUtf8Bytes, JsonConstants.NullValue);
_tokenType = JsonTokenType.Null;
}
internal void WriteNullSection(ReadOnlySpan<byte> escapedPropertyNameSection)
{
if (_options.Indented)
{
ReadOnlySpan<byte> escapedName =
escapedPropertyNameSection.Slice(1, escapedPropertyNameSection.Length - 3);
WriteLiteralHelper(escapedName, JsonConstants.NullValue);
_tokenType = JsonTokenType.Null;
}
else
{
Debug.Assert(escapedPropertyNameSection.Length <= JsonConstants.MaxUnescapedTokenSize - 3);
ReadOnlySpan<byte> span = JsonConstants.NullValue;
WriteLiteralSection(escapedPropertyNameSection, span);
SetFlagToAddListSeparatorBeforeNextItem();
_tokenType = JsonTokenType.Null;
}
}
private void WriteLiteralHelper(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
Debug.Assert(utf8PropertyName.Length <= JsonConstants.MaxUnescapedTokenSize);
WriteLiteralByOptions(utf8PropertyName, value);
SetFlagToAddListSeparatorBeforeNextItem();
}
/// <summary>
/// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The name of the property to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="ArgumentNullException">
/// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteNull(string propertyName)
{
if (propertyName is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(propertyName));
}
WriteNull(propertyName.AsSpan());
}
/// <summary>
/// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The name of the property to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteNull(ReadOnlySpan<char> propertyName)
{
JsonWriterHelper.ValidateProperty(propertyName);
ReadOnlySpan<byte> span = JsonConstants.NullValue;
WriteLiteralEscape(propertyName, span);
SetFlagToAddListSeparatorBeforeNextItem();
_tokenType = JsonTokenType.Null;
}
/// <summary>
/// Writes the property name and the JSON literal "null" as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteNull(ReadOnlySpan<byte> utf8PropertyName)
{
JsonWriterHelper.ValidateProperty(utf8PropertyName);
ReadOnlySpan<byte> span = JsonConstants.NullValue;
WriteLiteralEscape(utf8PropertyName, span);
SetFlagToAddListSeparatorBeforeNextItem();
_tokenType = JsonTokenType.Null;
}
/// <summary>
/// Writes the pre-encoded property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The JSON-encoded name of the property to write.</param>
/// <param name="value">The value to write.</param>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
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;
}
}
/// <summary>
/// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The name of the property to write.</param>
/// <param name="value">The value to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="ArgumentNullException">
/// The <paramref name="propertyName"/> parameter is <see langword="null"/>.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteBoolean(string propertyName, bool value)
{
if (propertyName is null)
{
ThrowHelper.ThrowArgumentNullException(nameof(propertyName));
}
WriteBoolean(propertyName.AsSpan(), value);
}
/// <summary>
/// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="propertyName">The name of the property to write.</param>
/// <param name="value">The value to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteBoolean(ReadOnlySpan<char> propertyName, bool value)
{
JsonWriterHelper.ValidateProperty(propertyName);
ReadOnlySpan<byte> span = value ? JsonConstants.TrueValue : JsonConstants.FalseValue;
WriteLiteralEscape(propertyName, span);
SetFlagToAddListSeparatorBeforeNextItem();
_tokenType = value ? JsonTokenType.True : JsonTokenType.False;
}
/// <summary>
/// Writes the property name and <see cref="bool"/> value (as a JSON literal "true" or "false") as part of a name/value pair of a JSON object.
/// </summary>
/// <param name="utf8PropertyName">The UTF-8 encoded name of the property to write.</param>
/// <param name="value">The value to write.</param>
/// <exception cref="ArgumentException">
/// Thrown when the specified property name is too large.
/// </exception>
/// <exception cref="InvalidOperationException">
/// Thrown if this would result in invalid JSON being written (while validation is enabled).
/// </exception>
/// <remarks>
/// The property name is escaped before writing.
/// </remarks>
public void WriteBoolean(ReadOnlySpan<byte> utf8PropertyName, bool value)
{
JsonWriterHelper.ValidateProperty(utf8PropertyName);
ReadOnlySpan<byte> span = value ? JsonConstants.TrueValue : JsonConstants.FalseValue;
WriteLiteralEscape(utf8PropertyName, span);
SetFlagToAddListSeparatorBeforeNextItem();
_tokenType = value ? JsonTokenType.True : JsonTokenType.False;
}
private void WriteLiteralEscape(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value)
{
int propertyIdx = JsonWriterHelper.NeedsEscaping(propertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < propertyName.Length);
if (propertyIdx != -1)
{
WriteLiteralEscapeProperty(propertyName, value, propertyIdx);
}
else
{
WriteLiteralByOptions(propertyName, value);
}
}
private void WriteLiteralEscape(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value)
{
int propertyIdx = JsonWriterHelper.NeedsEscaping(utf8PropertyName, _options.Encoder);
Debug.Assert(propertyIdx >= -1 && propertyIdx < utf8PropertyName.Length);
if (propertyIdx != -1)
{
WriteLiteralEscapeProperty(utf8PropertyName, value, propertyIdx);
}
else
{
WriteLiteralByOptions(utf8PropertyName, value);
}
}
private void WriteLiteralEscapeProperty(ReadOnlySpan<char> propertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp)
{
Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= propertyName.Length);
Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < propertyName.Length);
char[]? propertyArray = null;
int length = JsonWriterHelper.GetMaxEscapedLength(propertyName.Length, firstEscapeIndexProp);
Span<char> escapedPropertyName = length <= JsonConstants.StackallocCharThreshold ?
stackalloc char[JsonConstants.StackallocCharThreshold] :
(propertyArray = ArrayPool<char>.Shared.Rent(length));
JsonWriterHelper.EscapeString(propertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
if (propertyArray != null)
{
ArrayPool<char>.Shared.Return(propertyArray);
}
}
private void WriteLiteralEscapeProperty(ReadOnlySpan<byte> utf8PropertyName, ReadOnlySpan<byte> value, int firstEscapeIndexProp)
{
Debug.Assert(int.MaxValue / JsonConstants.MaxExpansionFactorWhileEscaping >= utf8PropertyName.Length);
Debug.Assert(firstEscapeIndexProp >= 0 && firstEscapeIndexProp < utf8PropertyName.Length);
byte[]? propertyArray = null;
int length = JsonWriterHelper.GetMaxEscapedLength(utf8PropertyName.Length, firstEscapeIndexProp);
Span<byte> escapedPropertyName = length <= JsonConstants.StackallocByteThreshold ?
stackalloc byte[JsonConstants.StackallocByteThreshold] :
(propertyArray = ArrayPool<byte>.Shared.Rent(length));
JsonWriterHelper.EscapeString(utf8PropertyName, escapedPropertyName, firstEscapeIndexProp, _options.Encoder, out int written);
WriteLiteralByOptions(escapedPropertyName.Slice(0, written), value);
if (propertyArray != null)
{
ArrayPool<byte>.Shared.Return(propertyArray);
}
}
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)
{
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - value.Length - 4);
// All ASCII, 2 quotes for property name, and 1 colon => escapedPropertyName.Length + value.Length + 3
// Optionally, 1 list separator, and up to 3x growth when transcoding
int maxRequired = (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + value.Length + 4;
if (_memory.Length - BytesPending < maxRequired)
{
Grow(maxRequired);
}
Span<byte> output = _memory.Span;
if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
output[BytesPending++] = JsonConstants.Quote;
TranscodeAndWrite(escapedPropertyName, output);
output[BytesPending++] = JsonConstants.Quote;
output[BytesPending++] = JsonConstants.KeyValueSeparator;
value.CopyTo(output.Slice(BytesPending));
BytesPending += value.Length;
}
private void WriteLiteralMinimized(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value)
{
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - value.Length - 4);
int minRequired = escapedPropertyName.Length + value.Length + 3; // 2 quotes for property name, and 1 colon
int maxRequired = minRequired + 1; // Optionally, 1 list separator
if (_memory.Length - BytesPending < maxRequired)
{
Grow(maxRequired);
}
Span<byte> output = _memory.Span;
if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
output[BytesPending++] = JsonConstants.Quote;
escapedPropertyName.CopyTo(output.Slice(BytesPending));
BytesPending += escapedPropertyName.Length;
output[BytesPending++] = JsonConstants.Quote;
output[BytesPending++] = JsonConstants.KeyValueSeparator;
value.CopyTo(output.Slice(BytesPending));
BytesPending += value.Length;
}
// AggressiveInlining used since this is only called from one location.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteLiteralSection(ReadOnlySpan<byte> escapedPropertyNameSection, ReadOnlySpan<byte> value)
{
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyNameSection.Length < int.MaxValue - value.Length - 1);
int minRequired = escapedPropertyNameSection.Length + value.Length;
int maxRequired = minRequired + 1; // Optionally, 1 list separator
if (_memory.Length - BytesPending < maxRequired)
{
Grow(maxRequired);
}
Span<byte> output = _memory.Span;
if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
escapedPropertyNameSection.CopyTo(output.Slice(BytesPending));
BytesPending += escapedPropertyNameSection.Length;
value.CopyTo(output.Slice(BytesPending));
BytesPending += value.Length;
}
private void WriteLiteralIndented(ReadOnlySpan<char> escapedPropertyName, ReadOnlySpan<byte> value)
{
int indent = Indentation;
Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < (int.MaxValue / JsonConstants.MaxExpansionFactorWhileTranscoding) - indent - value.Length - 5 - _newLineLength);
// All ASCII, 2 quotes for property name, 1 colon, and 1 space => escapedPropertyName.Length + value.Length + 4
// Optionally, 1 list separator, 1-2 bytes for new line, and up to 3x growth when transcoding
int maxRequired = indent + (escapedPropertyName.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) + value.Length + 5 + _newLineLength;
if (_memory.Length - BytesPending < maxRequired)
{
Grow(maxRequired);
}
Span<byte> output = _memory.Span;
if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
}
WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
TranscodeAndWrite(escapedPropertyName, output);
output[BytesPending++] = JsonConstants.Quote;
output[BytesPending++] = JsonConstants.KeyValueSeparator;
output[BytesPending++] = JsonConstants.Space;
value.CopyTo(output.Slice(BytesPending));
BytesPending += value.Length;
}
private void WriteLiteralIndented(ReadOnlySpan<byte> escapedPropertyName, ReadOnlySpan<byte> value)
{
int indent = Indentation;
Debug.Assert(indent <= _indentLength * _options.MaxDepth);
Debug.Assert(value.Length <= JsonConstants.MaxUnescapedTokenSize);
Debug.Assert(escapedPropertyName.Length < int.MaxValue - indent - value.Length - 5 - _newLineLength);
int minRequired = indent + escapedPropertyName.Length + value.Length + 4; // 2 quotes for property name, 1 colon, and 1 space
int maxRequired = minRequired + 1 + _newLineLength; // Optionally, 1 list separator and 1-2 bytes for new line
if (_memory.Length - BytesPending < maxRequired)
{
Grow(maxRequired);
}
Span<byte> output = _memory.Span;
if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}
Debug.Assert(_options.SkipValidation || _tokenType != JsonTokenType.PropertyName);
if (_tokenType != JsonTokenType.None)
{
WriteNewLine(output);
}
WriteIndentation(output.Slice(BytesPending), indent);
BytesPending += indent;
output[BytesPending++] = JsonConstants.Quote;
escapedPropertyName.CopyTo(output.Slice(BytesPending));
BytesPending += escapedPropertyName.Length;
output[BytesPending++] = JsonConstants.Quote;
output[BytesPending++] = JsonConstants.KeyValueSeparator;
output[BytesPending++] = JsonConstants.Space;
value.CopyTo(output.Slice(BytesPending));
BytesPending += value.Length;
}
internal void WritePropertyName(bool value)
{
Span<byte> utf8PropertyName = stackalloc byte[JsonConstants.MaximumFormatBooleanLength];
bool result = Utf8Formatter.TryFormat(value, utf8PropertyName, out int bytesWritten);
Debug.Assert(result);
WritePropertyNameUnescaped(utf8PropertyName.Slice(0, bytesWritten));
}
}
}
|