System.Memory.Data 8.0.0

About

System.Memory.Data introduces the BinaryData type, a lightweight abstraction for a byte payload. It makes it easy to convert between string, bytes, and stream.

This abstraction can simplify the API surface by exposing a single type instead of numerous overloads or properties. The BinaryData type handles data ownership efficiently, wrapping passed-in bytes when using byte[] or ReadOnlyMemory<byte> constructors or methods, and managing data as bytes when dealing with streams, strings, or rich model types serialized as JSON.

Key Features

  • Lightweight abstraction for byte payload via BinaryData type.
  • Convenient helper methods for common conversions among string, bytes, and stream.
  • Efficient data ownership handling.

How to Use

To/From String:

var data = new BinaryData("some data");

// ToString will decode the bytes using UTF-8
Console.WriteLine(data.ToString()); // prints "some data"

To/From Bytes:

byte[] bytes = Encoding.UTF8.GetBytes("some data");

// Create BinaryData using a constructor ...
BinaryData data = new BinaryData(bytes);

// Or using a static factory method.
data = BinaryData.FromBytes(bytes);

// There is an implicit cast defined for ReadOnlyMemory<byte>
ReadOnlyMemory<byte> rom = data;

// There is also an implicit cast defined for ReadOnlySpan<byte>
ReadOnlySpan<byte> ros = data;

// there is also a ToMemory method that gives access to the ReadOnlyMemory.
rom = data.ToMemory();

// and a ToArray method that converts into a byte array.
byte[] array = data.ToArray();

To/From stream:

var bytes = Encoding.UTF8.GetBytes("some data");
Stream stream = new MemoryStream(bytes);
var data = BinaryData.FromStream(stream);

// Calling ToStream will give back a stream that is backed by ReadOnlyMemory, so it is not writable.
stream = data.ToStream();
Console.WriteLine(stream.CanWrite); // prints false

BinaryData also can be used to integrate with ObjectSerializer. By default, the JsonObjectSerializer will be used, but any serializer deriving from ObjectSerializer can be used.

var model = new CustomModel
{
    A = "some text",
    B = 5,
    C = true
};

var data = BinaryData.FromObjectAsJson(model);
model = data.ToObjectFromJson<CustomModel>();

Main Types

The main types provided by this library are:

  • System.BinaryData

Additional Documentation

Feedback & Contributing

System.Memory.Data is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.

Showing the top 20 packages that depend on System.Memory.Data.

Packages Downloads
Azure.Core
This is the implementation of the Azure Client Pipeline
50
Azure.Core
This is the implementation of the Azure Client Pipeline
45
Azure.Core
This is the implementation of the Azure Client Pipeline
40
Azure.Core
This is the implementation of the Azure Client Pipeline
39
Azure.Core
This is the implementation of the Azure Client Pipeline
36
Azure.Core
This is the implementation of the Azure Client Pipeline
34
Azure.Core
This is the implementation of the Azure Client Pipeline
33
Azure.Core
This is the implementation of the Azure Client Pipeline
32
Azure.Core
This is the implementation of the Azure Client Pipeline
31
Azure.Core
This is the implementation of the Azure Client Pipeline
29
Azure.Core
This is the implementation of the Azure Client Pipeline
28
Azure.Core
This is the implementation of the Azure Client Pipeline
26
Azure.Core
This is the implementation of the Azure Client Pipeline
24
Azure.Core
This is the implementation of the Azure Client Pipeline
23

https://go.microsoft.com/fwlink/?LinkID=799421

.NET Framework 4.6.2

.NET 6.0

.NET 7.0

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
9.0.0-preview.3.24172.9 2 5/7/2024
9.0.0-preview.2.24128.5 6 3/18/2024
9.0.0-preview.1.24080.9 7 2/28/2024
8.0.0 14 11/16/2023
8.0.0-rc.2.23479.6 17 10/23/2023
8.0.0-rc.1.23419.4 14 9/15/2023
8.0.0-preview.7.23375.6 16 8/22/2023
8.0.0-preview.6.23329.7 13 8/4/2023
8.0.0-preview.5.23280.8 18 8/2/2023
8.0.0-preview.4.23259.5 18 5/20/2023
8.0.0-preview.3.23174.8 16 5/20/2023
8.0.0-preview.2.23128.3 16 5/20/2023
8.0.0-preview.1.23110.8 21 2/26/2023
7.0.0 21 12/7/2022
7.0.0-rc.2.22472.3 21 12/7/2022
7.0.0-rc.1.22426.10 21 9/15/2022
7.0.0-preview.7.22375.6 21 9/7/2022
7.0.0-preview.6.22324.4 16 9/19/2022
7.0.0-preview.5.22301.12 23 7/1/2022
7.0.0-preview.4.22229.4 20 7/1/2022
7.0.0-preview.3.22175.4 22 7/1/2022
7.0.0-preview.2.22152.2 20 7/1/2022
7.0.0-preview.1.22076.8 21 7/1/2022
6.0.2-mauipre.1.22102.15 25 7/1/2022
6.0.2-mauipre.1.22054.8 20 7/1/2022
6.0.0 20 7/1/2022
6.0.0-rc.2.21480.5 19 7/1/2022
6.0.0-rc.1.21451.13 22 7/1/2022
6.0.0-preview.7.21377.19 17 7/1/2022
6.0.0-preview.6.21352.12 19 7/1/2022
6.0.0-preview.5.21301.5 22 7/1/2022
6.0.0-preview.4.21253.7 24 7/1/2022
6.0.0-preview.3.21201.4 21 7/1/2022
6.0.0-preview.2.21154.6 25 7/1/2022
6.0.0-preview.1.21102.12 22 7/1/2022
1.0.2 21 7/1/2022
1.0.1 16 7/1/2022
1.0.0 16 7/1/2022
1.0.0-beta.2 25 7/1/2022
1.0.0-beta.1 17 7/1/2022