Skip to content

[API Proposal]: Add ReadOnlySequence overload for WriteRawValue #68223

Closed
@BrennanConroy

Description

@BrennanConroy

Background and motivation

When using the Utf8JsonWriter.WriteRawValue API, you can't pass multiple segments in to write a single value, you have to allocate a contiguous array and do a single call to WriteRawValue. In our use case we already have a ReadOnlySequence<byte> and want to write it as a raw json value.

But because the API only accepts string or span, we need to write the following:

if (result.RawSerializedData.IsSingleSegment)
{
    writer.WriteRawValue(result.RawSerializedData.First.Span, skipInputValidation: true);
}
else
{
    writer.WriteRawValue(result.RawSerializedData.ToArray(), skipInputValidation: true);
}

API Proposal

namespace System.Text.Json
{
    public sealed partial class Utf8JsonWriter
    {
+        public void WriteRawValue(ReadOnlySequence<byte> json, bool skipInputValidation = false);
+        public void WriteRawValue(ReadOnlySequence<char> json, bool skipInputValidation = false);
    }
}

API Usage

void MyFunc(Utf8JsonWriter writer, ReadOnlySequence<byte> sequence)
{
    writer.WriteRawValue(sequence);
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.Text.Jsonhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions