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
valuestringThe 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
base64StringstringThe 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
baseSerializerFunc<T, byte[]>The base serialization function.
maxCacheSizeintThe maximum number of items to cache (default 100).
Returns
Type Parameters
TThe 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
databyte[]The byte array containing the serialized collection.
itemDeserializerFunc<byte[], T>A function to deserialize each item from bytes.
Returns
- IList<T>
The deserialized collection.
Type Parameters
TThe 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
jsonBytesbyte[]The JSON byte array to deserialize.
optionsJsonSerializerOptionsOptional JsonSerializerOptions for customizing deserialization.
Returns
- T
The deserialized object.
Type Parameters
TThe type to deserialize to.
HexStringToByteArray(string)
Creates a byte array from a hexadecimal string.
public static byte[] HexStringToByteArray(this string hexString)
Parameters
hexStringstringThe 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
serializableIByteSerializableThe 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
memoryReadOnlyMemory<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
spanReadOnlySpan<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
collectionIEnumerable<T>The collection to serialize.
itemSerializerFunc<T, byte[]>A function to serialize each item to bytes.
Returns
- byte[]
A byte array containing the serialized collection.
Type Parameters
TThe 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
valueTThe 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
TThe 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
valueTThe object to serialize.
customSerializerFunc<T, byte[]>The custom serialization function.
Returns
- byte[]
The serialized byte array.
Type Parameters
TThe 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
dictionaryIDictionary<TKey, TValue>The dictionary to serialize.
keySerializerFunc<TKey, byte[]>A function to serialize keys to bytes.
valueSerializerFunc<TValue, byte[]>A function to serialize values to bytes.
Returns
- byte[]
A byte array containing the serialized dictionary.
Type Parameters
TKeyThe type of dictionary keys.
TValueThe 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
valueTThe object to serialize.
optionsJsonSerializerOptionsOptional JsonSerializerOptions for customizing serialization.
Returns
- byte[]
A byte array containing the JSON representation of the object.
Type Parameters
TThe 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
valueTThe object to serialize.
optionsJsonSerializerOptionsOptional JsonSerializerOptions for customizing serialization.
Returns
- ReadOnlyMemory<byte>
A ReadOnlyMemory containing the JSON representation of the object.
Type Parameters
TThe 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
valueTThe value to serialize.
serializerFunc<T, byte[]>A function that serializes the object and returns the byte array.
minimumLengthintThe minimum buffer length to rent.
Returns
Type Parameters
TThe 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
valueTThe value to serialize.
destinationSpan<byte>The span to write to.
serializerFunc<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
TThe 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
valuestringThe 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.