镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

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

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Apr 19, 2022
  2. teo-tsirpanis commented on Apr 19, 2022

    @teo-tsirpanis
    Contributor

    There isn't an implicit conversion from a string to a ReadOnlySequence<char> so the [StringSyntax(StringSyntaxAttribute.Json)] is not needed. And you forgot an issue title. 😅

  3. BrennanConroy commented on Apr 19, 2022

    @BrennanConroy
    MemberAuthor

    And you forgot an issue title. 😅

    Haha, I was too focused on filling out the API template!

  4. changed the title [-][API Proposal]: [/-] [+][API Proposal]: Add ReadOnlySequence overload for WriteRawValue[/+] on Apr 19, 2022
  5. added this to the 8.0.0 milestone on Jun 7, 2022
  6. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Jun 7, 2022
  7. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 7, 2022
  8. krwq commented on Jun 7, 2022

    @krwq
    Member

    Suggestion looks good to me as proposed, although we unlikely will have time to do it in this release, marking as 8.0.0 so it's on our radar for vNext.

  9. terrajobst commented on Aug 16, 2022

    @terrajobst
    Contributor

    Video

    • We don't want to add the overload ReadOnlySequence<char> because we'd have to deal with transcoding surrogate pairs across elements in the sequence, which is quite a bit of work that nobody seems to be asking for right now.
    namespace System.Text.Json;
    
    public sealed partial class Utf8JsonWriter
    {
        public void WriteRawValue(ReadOnlySequence<byte> json, bool skipInputValidation = false);
    }
  10. added
    api-approvedAPI was approved in API review, it can be implemented
    and removed
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    on Aug 16, 2022
  11. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Sep 30, 2022
  12. lateapexearlyspeed commented on Sep 30, 2022

    @lateapexearlyspeed
    Contributor

    Hi, I would try to add this API by this PR, could you please review?

    Also, I modified parameter name from json to utf8Json because it is for byte version rather than char (string), which seems being followed by existing overloads.
    Correct me, thanks !

    public void WriteRawValue(ReadOnlySequence{byte} utf8Json, bool skipInputValidation = false);

  13. eiriktsarpalis commented on Sep 30, 2022

    @eiriktsarpalis
    Member

    Also, I modified parameter name from json to utf8Json because it is for byte version rather than char (string), which seems being followed by existing overloads.

    Change sounds reasonable to me, thanks for correcting it.

  14. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jan 25, 2023
  15. ghost locked as resolved and limited conversation to collaborators on Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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