Class ObjectToByteArrayExtensions
- Namespace
- Plugin.ByteArrays
- Assembly
- Plugin.ByteArrays.dll
Provides extension methods for converting objects and strings to byte arrays using various encodings and formats.
public static class ObjectToByteArrayExtensions
- Inheritance
-
ObjectToByteArrayExtensions
- Inherited Members
Methods
AsciiStringToByteArray(string)
Creates a byte array from an ASCII-encoded string.
public static byte[] AsciiStringToByteArray(this string value)
Parameters
value
stringThe input string to convert to a byte array.
Returns
- byte[]
A new byte array containing the ASCII-encoded bytes of the input string, or an empty array if the input is null or empty.
Base64StringToByteArray(string)
Creates a byte array from a Base64-encoded string.
public static byte[] Base64StringToByteArray(this string base64String)
Parameters
base64String
stringThe input Base64 string to convert to a byte array.
Returns
- byte[]
A new byte array containing the decoded bytes of the Base64 string, or an empty array if the input is null or empty.
CreateCachedSerializer<T>(Func<T, byte[]>, int)
Creates a cacheable serializer that remembers serialization results for specific values.
public static Func<T, byte[]> CreateCachedSerializer<T>(Func<T, byte[]> baseSerializer, int maxCacheSize = 100) where T : notnull
Parameters
baseSerializer
Func<T, byte[]>The base serialization function.
maxCacheSize
intThe maximum number of items to cache (default 100).
Returns
Type Parameters
T
The type of objects to serialize.
FromByteArrayToList<T>(byte[], Func<byte[], T>)
Deserializes a byte array back to a collection of objects.
public static IList<T> FromByteArrayToList<T>(this byte[] data, Func<byte[], T> itemDeserializer)
Parameters
data
byte[]The byte array containing the serialized collection.
itemDeserializer
Func<byte[], T>A function to deserialize each item from bytes.
Returns
- IList<T>
The deserialized collection.
Type Parameters
T
The type of objects in the collection.
FromJsonByteArray<T>(byte[], JsonSerializerOptions?)
Deserializes a byte array from JSON into an object of the specified type.
public static T? FromJsonByteArray<T>(this byte[] jsonBytes, JsonSerializerOptions? options = null)
Parameters
jsonBytes
byte[]The JSON byte array to deserialize.
options
JsonSerializerOptionsOptional JsonSerializerOptions for customizing deserialization.
Returns
- T
The deserialized object.
Type Parameters
T
The type to deserialize to.
HexStringToByteArray(string)
Creates a byte array from a hexadecimal string.
public static byte[] HexStringToByteArray(this string hexString)
Parameters
hexString
stringThe input hexadecimal string to convert to a byte array.
Returns
- byte[]
A new byte array containing the bytes represented by the hexadecimal string, or an empty array if the input is null or empty.
Remarks
The input string must have an even number of characters, as each pair of characters represents a byte. If the string is empty or null, an empty byte array is returned.
Exceptions
- ArgumentException
Thrown if the input string is not a valid hexadecimal string.
ReturnRentedBuffer(byte[], bool)
Returns a rented buffer to the ArrayPool.
public static void ReturnRentedBuffer(byte[] buffer, bool clearArray = false)
Parameters
ToByteArray(IByteSerializable)
Serializes an object that implements IByteSerializable.
public static byte[] ToByteArray(this IByteSerializable serializable)
Parameters
serializable
IByteSerializableThe object to serialize.
Returns
- byte[]
The serialized byte array.
ToByteArray(ReadOnlyMemory<byte>)
Converts a memory segment to a new byte array.
public static byte[] ToByteArray(this ReadOnlyMemory<byte> memory)
Parameters
memory
ReadOnlyMemory<byte>The memory segment to convert.
Returns
- byte[]
A new byte array containing the memory data.
ToByteArray(ReadOnlySpan<byte>)
Converts a span of bytes to a new byte array.
public static byte[] ToByteArray(this ReadOnlySpan<byte> span)
Parameters
span
ReadOnlySpan<byte>The span to convert.
Returns
- byte[]
A new byte array containing the span data.
ToByteArray<T>(IEnumerable<T>, Func<T, byte[]>)
Converts a collection of objects to a byte array where each object is serialized and prefixed with its length.
public static byte[] ToByteArray<T>(this IEnumerable<T> collection, Func<T, byte[]> itemSerializer)
Parameters
collection
IEnumerable<T>The collection to serialize.
itemSerializer
Func<T, byte[]>A function to serialize each item to bytes.
Returns
- byte[]
A byte array containing the serialized collection.
Type Parameters
T
The type of objects in the collection.
ToByteArray<T>(T)
Creates a byte array from an object of any supported type.
public static byte[] ToByteArray<T>(this T value)
Parameters
value
TThe input value to convert to a byte array.
Returns
- byte[]
A new byte array representing the input value, or an empty array if the input is null.
Type Parameters
T
The type of the input value.
ToByteArray<T>(T, Func<T, byte[]>)
Serializes an object using a provided custom serialization function.
public static byte[] ToByteArray<T>(this T value, Func<T, byte[]> customSerializer)
Parameters
value
TThe object to serialize.
customSerializer
Func<T, byte[]>The custom serialization function.
Returns
- byte[]
The serialized byte array.
Type Parameters
T
The type of the object to serialize.
ToByteArray<TKey, TValue>(IDictionary<TKey, TValue>, Func<TKey, byte[]>, Func<TValue, byte[]>)
Converts a dictionary to a byte array with key-value pairs serialized.
public static byte[] ToByteArray<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, byte[]> keySerializer, Func<TValue, byte[]> valueSerializer)
Parameters
dictionary
IDictionary<TKey, TValue>The dictionary to serialize.
keySerializer
Func<TKey, byte[]>A function to serialize keys to bytes.
valueSerializer
Func<TValue, byte[]>A function to serialize values to bytes.
Returns
- byte[]
A byte array containing the serialized dictionary.
Type Parameters
TKey
The type of dictionary keys.
TValue
The type of dictionary values.
ToJsonByteArray<T>(T, JsonSerializerOptions?)
Serializes an object to a byte array using System.Text.Json.
public static byte[] ToJsonByteArray<T>(this T value, JsonSerializerOptions? options = null)
Parameters
value
TThe object to serialize.
options
JsonSerializerOptionsOptional JsonSerializerOptions for customizing serialization.
Returns
- byte[]
A byte array containing the JSON representation of the object.
Type Parameters
T
The type of the object to serialize.
ToJsonMemory<T>(T, JsonSerializerOptions?)
Serializes an object to a byte array using System.Text.Json with ReadOnlySpan support.
public static ReadOnlyMemory<byte> ToJsonMemory<T>(this T value, JsonSerializerOptions? options = null)
Parameters
value
TThe object to serialize.
options
JsonSerializerOptionsOptional JsonSerializerOptions for customizing serialization.
Returns
- ReadOnlyMemory<byte>
A ReadOnlyMemory containing the JSON representation of the object.
Type Parameters
T
The type of the object to serialize.
ToRentedBuffer<T>(T, Func<T, byte[]>, int)
Rents a buffer from ArrayPool, writes data to it, and returns the relevant portion.
public static (byte[] buffer, int length) ToRentedBuffer<T>(this T value, Func<T, byte[]> serializer, int minimumLength = 1024)
Parameters
value
TThe value to serialize.
serializer
Func<T, byte[]>A function that serializes the object and returns the byte array.
minimumLength
intThe minimum buffer length to rent.
Returns
Type Parameters
T
The type of the object to serialize.
TryWriteToSpan<T>(T, Span<byte>, Func<T, Span<byte>, int>)
Writes an object to a provided span using a custom serializer.
public static int TryWriteToSpan<T>(this T value, Span<byte> destination, Func<T, Span<byte>, int> serializer)
Parameters
value
TThe value to serialize.
destination
Span<byte>The span to write to.
serializer
Func<T, Span<byte>, int>A function that writes the object to a span and returns bytes written.
Returns
- int
The number of bytes written to the span.
Type Parameters
T
The type of the object to serialize.
Utf8StringToByteArray(string)
Creates a byte array from a UTF-8 encoded string.
public static byte[] Utf8StringToByteArray(this string value)
Parameters
value
stringThe input string to convert to a byte array.
Returns
- byte[]
A new byte array containing the UTF-8 encoded bytes of the input string, or an empty array if the input is null or empty.