diff --git a/src/Elastic.Transport/Components/Serialization/TransportSerializerExtensions.cs b/src/Elastic.Transport/Components/Serialization/TransportSerializerExtensions.cs
index 22c065c..cfc6a87 100644
--- a/src/Elastic.Transport/Components/Serialization/TransportSerializerExtensions.cs
+++ b/src/Elastic.Transport/Components/Serialization/TransportSerializerExtensions.cs
@@ -18,25 +18,69 @@ public static class TransportSerializerExtensions
///
/// Extension method that serializes an instance of to a byte array.
///
+ /// The type of the data to be serialized.
+ ///
+ ///
+ ///
public static byte[] SerializeToBytes(
this Serializer serializer,
- T data,
+ T? data,
SerializationFormatting formatting = SerializationFormatting.None) =>
SerializeToBytes(serializer, data, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
///
/// Extension method that serializes an instance of to a byte array.
///
+ /// The type of the data to be serialized.
+ ///
///
///
/// A factory yielding MemoryStream instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- ///
///
public static byte[] SerializeToBytes(
this Serializer serializer,
- T data,
+ T? data,
+ MemoryStreamFactory? memoryStreamFactory = null,
+ SerializationFormatting formatting = SerializationFormatting.None
+ )
+ {
+ memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
+ using var ms = memoryStreamFactory.Create();
+ serializer.Serialize(data, ms, formatting);
+ return ms.ToArray();
+ }
+
+ ///
+ /// Extension method that serializes the given to a byte array.
+ ///
+ ///
+ ///
+ /// The type of the data to serialize.
+ ///
+ public static byte[] SerializeToBytes(
+ this Serializer serializer,
+ object? data,
+ Type type,
+ SerializationFormatting formatting = SerializationFormatting.None) =>
+ SerializeToBytes(serializer, data, type, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
+
+ ///
+ /// Extension method that serializes the given to a byte array.
+ ///
+ ///
+ ///
+ /// The type of the data to serialize.
+ ///
+ /// A factory yielding MemoryStream instances, defaults to
+ /// that yields memory streams backed by pooled byte arrays.
+ ///
+ ///
+ public static byte[] SerializeToBytes(
+ this Serializer serializer,
+ object? data,
+ Type type,
MemoryStreamFactory? memoryStreamFactory = null,
SerializationFormatting formatting = SerializationFormatting.None
)
@@ -50,94 +94,215 @@ public static byte[] SerializeToBytes(
///
/// Extension method that serializes an instance of to a string.
///
+ /// The type of the data to be serialized.
+ ///
+ /// The data to serialize.
+ ///
public static string SerializeToString(
this Serializer serializer,
- T data,
+ T? data,
SerializationFormatting formatting = SerializationFormatting.None) =>
SerializeToString(serializer, data, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
///
/// Extension method that serializes an instance of to a string.
///
- ///
+ /// The type of the data to be serialized.
+ ///
+ /// The data to serialize.
///
/// A factory yielding MemoryStream instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- ///
///
public static string SerializeToString(
this Serializer serializer,
- T data,
+ T? data,
MemoryStreamFactory? memoryStreamFactory = null,
SerializationFormatting formatting = SerializationFormatting.None
)
{
+ if (serializer is SystemTextJsonSerializer stjSerializer)
+ {
+ // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
+ // serialize straight into string.
+ return JsonSerializer.Serialize(data, stjSerializer.GetJsonSerializerOptions(formatting));
+ }
+
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
using var ms = memoryStreamFactory.Create();
+
serializer.Serialize(data, ms, formatting);
+
+ return ms.Utf8String();
+ }
+
+ ///
+ /// Extension method that serializes the given to a string.
+ ///
+ ///
+ /// The data to serialize.
+ /// The type of the data to serialize.
+ ///
+ public static string SerializeToString(
+ this Serializer serializer,
+ object? data,
+ Type type,
+ SerializationFormatting formatting = SerializationFormatting.None) =>
+ SerializeToString(serializer, data, type, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
+
+ ///
+ /// Extension method that serializes the given to a string.
+ ///
+ ///
+ /// The data to serialize.
+ /// The type of the data to serialize.
+ ///
+ /// A factory yielding MemoryStream instances, defaults to
+ /// that yields memory streams backed by pooled byte arrays.
+ ///
+ ///
+ public static string SerializeToString(
+ this Serializer serializer,
+ object? data,
+ Type type,
+ MemoryStreamFactory? memoryStreamFactory = null,
+ SerializationFormatting formatting = SerializationFormatting.None
+ )
+ {
+ if (serializer is SystemTextJsonSerializer stjSerializer)
+ {
+ // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
+ // serialize straight into string.
+ return JsonSerializer.Serialize(data, type, stjSerializer.GetJsonSerializerOptions(formatting));
+ }
+
+ memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
+ using var ms = memoryStreamFactory.Create();
+
+ serializer.Serialize(data, ms, formatting);
+
return ms.Utf8String();
}
#region STJ Extensions
///
- /// Extension method that deserializes from a UTF8 .
+ /// Extension method that writes the serialized representation of an instance of to a
+ /// .
///
- /// The type of the data to be deserialized.
+ /// The type of the data to be serialized.
///
- /// The source that contains the UTF8 encoded JSON string.
+ /// The data to serialize.
+ /// The destination .
+ ///
+ public static void Serialize(
+ this Serializer serializer,
+ T? data,
+ Utf8JsonWriter writer,
+ SerializationFormatting formatting = SerializationFormatting.None
+ ) => Serialize(serializer, data, writer, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
+
+ ///
+ /// Extension method that writes the serialized representation of an instance of to a
+ /// .
+ ///
+ /// The type of the data to be serialized.
+ ///
+ /// The data to serialize.
+ /// The destination .
///
/// A factory yielding instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- /// The deserialized data.
- public static T? Deserialize(
+ ///
+ public static void Serialize(
this Serializer serializer,
- ReadOnlySpan span,
- MemoryStreamFactory? memoryStreamFactory = null)
+ T? data,
+ Utf8JsonWriter writer,
+ MemoryStreamFactory? memoryStreamFactory = null,
+ SerializationFormatting formatting = SerializationFormatting.None)
{
if (serializer is SystemTextJsonSerializer stjSerializer)
{
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
- // deserialize straight from the span.
- return JsonSerializer.Deserialize(span, stjSerializer.GetJsonSerializerOptions());
+ // serialize straight into the writer.
+ JsonSerializer.Serialize(writer, data, stjSerializer.GetJsonSerializerOptions(formatting));
+ return;
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create(span.ToArray());
+ using var ms = memoryStreamFactory.Create();
- return serializer.Deserialize(ms);
+ serializer.Serialize(data, ms);
+ ms.Position = 0;
+
+#if NET6_0_OR_GREATER
+ writer.WriteRawValue(ms.GetBuffer().AsSpan()[..(int)ms.Length], true);
+#else
+ using var document = JsonDocument.Parse(ms);
+ document.RootElement.WriteTo(writer);
+#endif
}
///
- /// Extension method that deserializes from a UTF8 .
+ /// Extension method that writes the serialized representation of the given to a
+ /// .
///
///
- /// The source that contains the UTF8 encoded JSON.
- /// The type of the data to be deserialized.
+ /// The data to serialize.
+ /// The type of the data to serialize.
+ /// The destination .
+ ///
+ public static void Serialize(
+ this Serializer serializer,
+ object? data,
+ Type type,
+ Utf8JsonWriter writer,
+ SerializationFormatting formatting = SerializationFormatting.None
+ ) => Serialize(serializer, data, type, writer, TransportConfiguration.DefaultMemoryStreamFactory, formatting);
+
+ ///
+ /// Extension method that writes the serialized representation of the given to a
+ /// .
+ ///
+ ///
+ /// The data to serialize.
+ /// The type of the data to serialize.
+ /// The destination .
///
/// A factory yielding instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- /// The deserialized data.
- public static object? Deserialize(
+ ///
+ public static void Serialize(
this Serializer serializer,
- ReadOnlySpan span,
+ object? data,
Type type,
- MemoryStreamFactory? memoryStreamFactory = null)
+ Utf8JsonWriter writer,
+ MemoryStreamFactory? memoryStreamFactory = null,
+ SerializationFormatting formatting = SerializationFormatting.None)
{
if (serializer is SystemTextJsonSerializer stjSerializer)
{
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
- // deserialize straight from the span.
- return JsonSerializer.Deserialize(span, type, stjSerializer.GetJsonSerializerOptions());
+ // serialize straight into the writer.
+ JsonSerializer.Serialize(writer, data, type, stjSerializer.GetJsonSerializerOptions(formatting));
+ return;
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create(span.ToArray());
+ using var ms = memoryStreamFactory.Create();
- return serializer.Deserialize(type, ms);
+ serializer.Serialize(data, ms);
+ ms.Position = 0;
+
+#if NET6_0_OR_GREATER
+ writer.WriteRawValue(ms.GetBuffer().AsSpan()[..(int)ms.Length], true);
+#else
+ using var document = JsonDocument.Parse(ms);
+ document.RootElement.WriteTo(writer);
+#endif
}
///
@@ -153,7 +318,7 @@ public static string SerializeToString(
/// The deserialized data.
public static T? Deserialize(
this Serializer serializer,
- ReadOnlySpan span,
+ ReadOnlySpan span,
MemoryStreamFactory? memoryStreamFactory = null)
{
if (serializer is SystemTextJsonSerializer stjSerializer)
@@ -164,7 +329,7 @@ public static string SerializeToString(
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray()));
+ using var ms = memoryStreamFactory.Create(span.ToArray());
return serializer.Deserialize(ms);
}
@@ -182,7 +347,7 @@ public static string SerializeToString(
/// The deserialized data.
public static object? Deserialize(
this Serializer serializer,
- ReadOnlySpan span,
+ ReadOnlySpan span,
Type type,
MemoryStreamFactory? memoryStreamFactory = null)
{
@@ -194,94 +359,68 @@ public static string SerializeToString(
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray()));
+ using var ms = memoryStreamFactory.Create(span.ToArray());
return serializer.Deserialize(type, ms);
}
///
- /// Extension method that writes the serialized representation of an instance of to a
- /// .
+ /// Extension method that deserializes from a UTF8 .
///
- /// The type of the data to be serialized.
+ /// The type of the data to be deserialized.
///
- /// The data to serialize.
- /// The destination .
+ /// The source that contains the UTF8 encoded JSON string.
///
/// A factory yielding instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- ///
- public static void Serialize(
+ /// The deserialized data.
+ public static T? Deserialize(
this Serializer serializer,
- T? data,
- Utf8JsonWriter writer,
- MemoryStreamFactory? memoryStreamFactory = null,
- SerializationFormatting formatting = SerializationFormatting.None)
+ ReadOnlySpan span,
+ MemoryStreamFactory? memoryStreamFactory = null)
{
if (serializer is SystemTextJsonSerializer stjSerializer)
{
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
- // serialize straight into the writer.
- JsonSerializer.Serialize(writer, data, stjSerializer.GetJsonSerializerOptions(formatting));
- return;
+ // deserialize straight from the span.
+ return JsonSerializer.Deserialize(span, stjSerializer.GetJsonSerializerOptions());
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create();
-
- serializer.Serialize(data, ms);
- ms.Position = 0;
+ using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray()));
-#if NET6_0_OR_GREATER
- writer.WriteRawValue(ms.GetBuffer().AsSpan()[..(int)ms.Length], true);
-#else
- using var document = JsonDocument.Parse(ms);
- document.RootElement.WriteTo(writer);
-#endif
+ return serializer.Deserialize(ms);
}
///
- /// Extension method that writes the serialized representation of the given to a
- /// .
+ /// Extension method that deserializes from a UTF8 .
///
///
- /// The data to serialize.
- /// The type of the data to serialize.
- /// The destination .
+ /// The source that contains the UTF8 encoded JSON.
+ /// The type of the data to be deserialized.
///
/// A factory yielding instances, defaults to
/// that yields memory streams backed by pooled byte arrays.
///
- ///
- public static void Serialize(
+ /// The deserialized data.
+ public static object? Deserialize(
this Serializer serializer,
- object? data,
+ ReadOnlySpan span,
Type type,
- Utf8JsonWriter writer,
- MemoryStreamFactory? memoryStreamFactory = null,
- SerializationFormatting formatting = SerializationFormatting.None)
+ MemoryStreamFactory? memoryStreamFactory = null)
{
if (serializer is SystemTextJsonSerializer stjSerializer)
{
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
- // serialize straight into the writer.
- JsonSerializer.Serialize(writer, data, type, stjSerializer.GetJsonSerializerOptions(formatting));
- return;
+ // deserialize straight from the span.
+ return JsonSerializer.Deserialize(span, type, stjSerializer.GetJsonSerializerOptions());
}
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
- using var ms = memoryStreamFactory.Create();
-
- serializer.Serialize(data, ms);
- ms.Position = 0;
+ using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray()));
-#if NET6_0_OR_GREATER
- writer.WriteRawValue(ms.GetBuffer().AsSpan()[..(int)ms.Length], true);
-#else
- using var document = JsonDocument.Parse(ms);
- document.RootElement.WriteTo(writer);
-#endif
+ return serializer.Deserialize(type, ms);
}
///