Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write-raw APIs to Utf8JsonWriter #54254

Merged
6 commits merged into from
Jul 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/libraries/System.Text.Json/ref/System.Text.Json.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ public void WritePropertyName(System.ReadOnlySpan<byte> utf8PropertyName) { }
public void WritePropertyName(System.ReadOnlySpan<char> propertyName) { }
public void WritePropertyName(string propertyName) { }
public void WritePropertyName(System.Text.Json.JsonEncodedText propertyName) { }
public void WriteRawValue(string json, bool skipInputValidation = false) { }
public void WriteRawValue(System.ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false) { }
public void WriteRawValue(System.ReadOnlySpan<char> json, bool skipInputValidation = false) { }
public void WriteStartArray() { }
public void WriteStartArray(System.ReadOnlySpan<byte> utf8PropertyName) { }
public void WriteStartArray(System.ReadOnlySpan<char> propertyName) { }
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Text.Json/src/System.Text.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Guid.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Helpers.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Literal.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.Raw.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.SignedNumber.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.String.cs" />
<Compile Include="System\Text\Json\Writer\Utf8JsonWriter.WriteValues.UnsignedNumber.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ internal static class JsonConstants
// All other UTF-16 characters can be represented by either 1 or 2 UTF-8 bytes.
public const int MaxExpansionFactorWhileTranscoding = 3;

// When transcoding from UTF8 -> UTF16, the byte count threshold where we rent from the array pool before performing a normal alloc.
public const long ArrayPoolMaxSizeBeforeUsingNormalAlloc = 1024 * 1024;

public const int MaxEscapedTokenSize = 1_000_000_000; // Max size for already escaped value.
public const int MaxUnescapedTokenSize = MaxEscapedTokenSize / MaxExpansionFactorWhileEscaping; // 166_666_666 bytes
public const int MaxBase64ValueTokenSize = (MaxEscapedTokenSize >> 2) * 3 / MaxExpansionFactorWhileEscaping; // 125_000_000 bytes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,10 @@ public static partial class JsonSerializer

private static TValue? ReadUsingMetadata<TValue>(ReadOnlySpan<char> json, JsonTypeInfo jsonTypeInfo)
{
const long ArrayPoolMaxSizeBeforeUsingNormalAlloc = 1024 * 1024;

byte[]? tempArray = null;

// For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
Span<byte> utf8 = json.Length <= (ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorWhileTranscoding) ?
Span<byte> utf8 = json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorWhileTranscoding) ?
// Use a pooled alloc.
tempArray = ArrayPool<byte>.Shared.Rent(json.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) :
// Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per current implementation)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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;

namespace System.Text.Json
{
public sealed partial class Utf8JsonWriter
{
/// <summary>
/// Writes the input as JSON content.
/// </summary>
/// <param name="json">The raw JSON content to write.</param>
/// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param>
public void WriteRawValue(string json, bool skipInputValidation = false)
{
if (json == null)
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
throw new ArgumentNullException(nameof(json));
}

WriteRawValue(json.AsSpan(), skipInputValidation);
}

/// <summary>
/// Writes the input as JSON content.
/// </summary>
/// <param name="json">The raw JSON content to write.</param>
/// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param>
public void WriteRawValue(ReadOnlySpan<char> json, bool skipInputValidation = false)
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
byte[]? tempArray = null;

// For performance, avoid obtaining actual byte count unless memory usage is higher than the threshold.
Span<byte> utf8Json = json.Length <= (JsonConstants.ArrayPoolMaxSizeBeforeUsingNormalAlloc / JsonConstants.MaxExpansionFactorWhileTranscoding) ?
// Use a pooled alloc.
tempArray = ArrayPool<byte>.Shared.Rent(json.Length * JsonConstants.MaxExpansionFactorWhileTranscoding) :
// Use a normal alloc since the pool would create a normal alloc anyway based on the threshold (per current implementation)
// and by using a normal alloc we can avoid the Clear().
new byte[JsonReaderHelper.GetUtf8ByteCount(json)];

try
{
int actualByteCount = JsonReaderHelper.GetUtf8FromText(json, utf8Json);
utf8Json = utf8Json.Slice(0, actualByteCount);
WriteRawValue(utf8Json, skipInputValidation);
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
if (tempArray != null)
{
utf8Json.Clear();
ArrayPool<byte>.Shared.Return(tempArray);
}
}
}

/// <summary>
/// Writes the input as JSON content.
/// </summary>
/// <param name="utf8Json">The raw JSON content to write.</param>
/// <param name="skipInputValidation">Whether to skip validation of the input JSON content.</param>
layomia marked this conversation as resolved.
Show resolved Hide resolved
public void WriteRawValue(ReadOnlySpan<byte> utf8Json, bool skipInputValidation = false)
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
if (utf8Json.Length == 0)
{
ThrowHelper.ThrowArgumentException(SR.ExpectedJsonTokens);
}

if (!skipInputValidation)
{
Utf8JsonReader reader = new Utf8JsonReader(utf8Json);
layomia marked this conversation as resolved.
Show resolved Hide resolved

try
{
while (reader.Read());
}
catch (JsonReaderException ex)
{
ThrowHelper.ThrowArgumentException(ex.Message);
}
layomia marked this conversation as resolved.
Show resolved Hide resolved
}

int maxRequired = utf8Json.Length + 1; // Optionally, 1 list separator
layomia marked this conversation as resolved.
Show resolved Hide resolved

if (_memory.Length - BytesPending < maxRequired)
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
Grow(maxRequired);
}

Span<byte> output = _memory.Span;

if (_currentDepth < 0)
{
output[BytesPending++] = JsonConstants.ListSeparator;
}

utf8Json.CopyTo(output.Slice(BytesPending));
BytesPending += utf8Json.Length;

SetFlagToAddListSeparatorBeforeNextItem();

// Treat all raw JSON value writes as string.
_tokenType = JsonTokenType.String;
layomia marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\System\Text\Json\BitStack.cs" Link="BitStack.cs" />
<Compile Include="Utf8JsonWriterTests.WriteRaw.cs" />
layomia marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != '$(NetCoreAppCurrent)'">
<Compile Include="$(CommonPath)System\Runtime\CompilerServices\IsExternalInit.cs" Link="Common\System\Runtime\CompilerServices\IsExternalInit.cs" />
Expand Down
Loading