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 System.BinaryData #41686
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
namespace System
{
public readonly struct BinaryData
{
public BinaryData(ReadOnlySpan<byte> data);
public BinaryData(byte[] data);
public BinaryData(object jsonSerializable, Type? type = null);
public BinaryData(ReadOnlyMemory<byte> data);
public BinaryData(string data);
public static BinaryData FromBytes(ReadOnlyMemory<byte> data);
public static BinaryData FromBytes(ReadOnlySpan<byte> data);
public static BinaryData FromBytes(byte[] data);
public static BinaryData FromObject<T>(T jsonSerializable, CancellationToken cancellationToken = default);
public static Task<BinaryData> FromObjectAsync<T>(T jsonSerializable, CancellationToken cancellationToken = default);
public static BinaryData FromStream(Stream stream);
public static Task<BinaryData> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default);
public static BinaryData FromString(string data);
public static implicit operator ReadOnlyMemory<byte>(BinaryData data);
public ReadOnlyMemory<byte> ToBytes();
public T ToObject<T>(CancellationToken cancellationToken = default);
public ValueTask<T> ToObjectAsync<T>(CancellationToken cancellationToken = default);
public Stream ToStream();
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj);
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode();
public override string ToString();
}
} |
I have a similar type in my own libraries and one thing I've found useful which isn't in this surface is base64 read and write capability. |
|
Regarding constructors vs factory methods, perhaps future tooling could suggest |
I didn't see @carlreinke comment when I suggested #42314 but I think this is exactly what you were referring to |
namespace System
{
public class BinaryData
{
public BinaryData(byte[] data);
public BinaryData(object jsonSerializable, JsonSerializerOptions options = default, Type? type = null);
public BinaryData(ReadOnlyMemory<byte> data);
public BinaryData(string data);
public static BinaryData FromBytes(ReadOnlyMemory<byte> data);
public static BinaryData FromBytes(byte[] data);
public static BinaryData FromObjectAsJson<T>(T jsonSerializable, JsonSerializerOptions options = default, CancellationToken cancellationToken = default);
public static BinaryData FromStream(Stream stream);
public static Task<BinaryData> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default);
public static BinaryData FromString(string data);
public static implicit operator ReadOnlyMemory<byte>(BinaryData data);
public static implicit operator ReadOnlySpan<byte>(BinaryData data);
public ReadOnlyMemory<byte> ToBytes();
public T ToObjectFromJson<T>(JsonSerializerOptions options = default, CancellationToken cancellationToken = default);
public Stream ToStream();
[EditorBrowsable(EditorBrowsableState.Never)]
public override bool Equals(object? obj);
[EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode();
public override string ToString();
}
} |
I believe this should support namespace System
{
public class BinaryData
{
public BinaryData(ReadOnlySequence<byte> data);
public static BinaryData FromBytes(ReadOnlySequence<byte> data);
}
} |
It seems like a layering violation that...
As it stands, this type appears to be a DTO helper object hard-coded to use |
@terrajobst, with https://github.com/dotnet/runtime/tree/8a52f1e948b6f22f418817ec1068f07b8dae2aa5/src/libraries/System.Memory.Data, can this be closed now? |
Yep |
@KrzysztofCwalina suggested to add a new type called
BinaryData
. The Azure SDK team found in user studies that people struggle with the various ways we represent binary data (span, memory, byte arrays, streams). They experimented with a new typeBinaryData
that is a struct-based wrapper around abyte[]
and provides constructors, factory methods, and conversion methods to convert data from and to the various representations, including JSON serialization.Full spec is here: dotnet/designs#155
The text was updated successfully, but these errors were encountered: