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

[API Proposal]: Allow segmented raw writes for Utf8JsonWriter.WriteBase64StringValue #95186

Closed
paulomorgado opened this issue Nov 23, 2023 · 2 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json

Comments

@paulomorgado
Copy link
Contributor

Background and motivation

To avoid extra allocations, it would be nice to able to write segmented data to Utf8JsonWriter.WriteBase64StringValue.

API Proposal

public sealed class Utf8JsonWriter : IAsyncDisposable, IDisposable
{
    public void WriteBase64StringValue (ReadOnlySequence<byte> bytes);
}

API Usage

public class Dto
{
    public ReadOnlyMemory<byte> Data { get; set; }

    public ReadOnlySequenceOfByteWrapper Wrapper { get; set; }
}

[JsonConverter(typeof(ReadOnlySequenceOfByteWrapperJsonConverter))]
public readonly struct ReadOnlySequenceOfByteWrapper(ReadOnlySequence<byte> data)
{
    [JsonIgnore]
    public ReadOnlySequence<byte> Data { get; } = data;
}

[JsonSerializable(typeof(Dto))]
public partial class Class1JsonSerializationContext : JsonSerializerContext
{
}

public class ReadOnlySequenceOfByteWrapperJsonConverter : JsonConverter<ReadOnlySequenceOfByteWrapper>
{
    public override ReadOnlySequenceOfByteWrapper Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, ReadOnlySequenceOfByteWrapper value, JsonSerializerOptions options)
    {
        writer.WriteBase64StringValue(value.Data);
    }
}

Alternative Designs

No response

Risks

No response

@paulomorgado paulomorgado added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 23, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 23, 2023
@ghost
Copy link

ghost commented Nov 23, 2023

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

To avoid extra allocations, it would be nice to able to write segmented data to Utf8JsonWriter.WriteBase64StringValue.

API Proposal

public sealed class Utf8JsonWriter : IAsyncDisposable, IDisposable
{
    public void WriteBase64StringValue (ReadOnlySequence<byte> bytes);
}

API Usage

public class Dto
{
    public ReadOnlyMemory<byte> Data { get; set; }

    public ReadOnlySequenceOfByteWrapper Wrapper { get; set; }
}

[JsonConverter(typeof(ReadOnlySequenceOfByteWrapperJsonConverter))]
public readonly struct ReadOnlySequenceOfByteWrapper(ReadOnlySequence<byte> data)
{
    [JsonIgnore]
    public ReadOnlySequence<byte> Data { get; } = data;
}

[JsonSerializable(typeof(Dto))]
public partial class Class1JsonSerializationContext : JsonSerializerContext
{
}

public class ReadOnlySequenceOfByteWrapperJsonConverter : JsonConverter<ReadOnlySequenceOfByteWrapper>
{
    public override ReadOnlySequenceOfByteWrapper Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
    {
        throw new NotImplementedException();
    }

    public override void Write(Utf8JsonWriter writer, ReadOnlySequenceOfByteWrapper value, JsonSerializerOptions options)
    {
        writer.WriteBase64StringValue(value.Data);
    }
}

Alternative Designs

No response

Risks

No response

Author: paulomorgado
Assignees: -
Labels:

api-suggestion, area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #67337.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 24, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

2 participants