System.Memory.Data 1.0.2
System.Memory.Data library for .NET
Binary Data
The BinaryData
type provides a lightweight abstraction for a payload of bytes. It provides convenient helper methods to get out commonly used primitives, such as streams, strings, or bytes. The assumption when converting to and from string is that the encoding is UTF-8.
Data ownership
When using the byte[]
or ReadOnlyMemory<byte>
constructors or methods, BinaryData
will wrap the passed in bytes. When using streams, strings, or rich model types that will be serialized as Json, the data is converted into bytes and will be maintained by BinaryData
. Thus, if you are using bytes to create your instance of BinaryData
, changes to the underlying data will be reflected in BinaryData
as it does not copy the bytes.
Usage
The main value of this type is its ability to easily convert from string to bytes to stream. This can greatly simplify API surface areas by exposing this type as opposed to numerous overloads or properties.
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>();
Showing the top 20 packages that depend on System.Memory.Data.
Packages | Downloads |
---|---|
Azure.Core
This is the implementation of the Azure Client Pipeline
|
29 |
Azure.Core
This is the implementation of the Azure Client Pipeline
|
31 |
Azure.Core
This is the implementation of the Azure Client Pipeline
|
43 |
Azure.Core
This is the implementation of the Azure Client Pipeline
|
58 |
Azure.Core
This is the implementation of the Azure Client Pipeline
|
340 |
Azure.Core
This is the implementation of the Azure Client Pipeline
|
387 |
Azure.Messaging.EventGrid
This library can be used to publish events to Azure Event Grid and to consume events delivered by EventGrid. It also defines the event schemas for the events published to EventGrid by various Azure services.
|
27 |
Microsoft.Azure.WebJobs
This package contains the runtime assemblies for Microsoft.Azure.WebJobs.Host. It also adds rich diagnostics capabilities which makes it easier to monitor the WebJobs in the dashboard. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=320971
|
31 |
Microsoft.Azure.WebJobs
This package contains the runtime assemblies for Microsoft.Azure.WebJobs.Host. It also adds rich diagnostics capabilities which makes it easier to monitor the WebJobs in the dashboard. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=320971
|
33 |
Microsoft.Azure.WebJobs
This package contains the runtime host components of the WebJobs SDK. For more information, please visit https://go.microsoft.com/fwlink/?linkid=2279708.
|
33 |
Microsoft.Azure.WebJobs.Core
This library simplifies the task of adding background processing to your Microsoft Azure Web Sites. The SDK uses Microsoft Azure Storage, triggering a function in your program when items are added to Queues and Blobs. A dashboard provides rich monitoring and diagnostics for the programs that you write by using the SDK. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=320971
|
27 |
Microsoft.Azure.WebJobs.Core
This library simplifies the task of adding background processing to your Microsoft Azure Web Sites. The SDK uses Microsoft Azure Storage, triggering a function in your program when items are added to Queues and Blobs. A dashboard provides rich monitoring and diagnostics for the programs that you write by using the SDK. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=320971
|
43 |
System.ClientModel
Contains building blocks for clients that call cloud services.
|
29 |
System.ClientModel
Contains building blocks for clients that call cloud services.
|
34 |
System.ClientModel
Contains building blocks for clients that call cloud services.
|
36 |
.NET Framework 4.6.1
- System.Text.Encodings.Web (>= 4.7.2)
- System.Text.Json (>= 4.6.0)
.NET Standard 2.0
- System.Text.Encodings.Web (>= 4.7.2)
- System.Text.Json (>= 4.6.0)