Skip to content

Commit c5b6725

Browse files
committed
Improve serializer extensions
1 parent ec5f19d commit c5b6725

File tree

1 file changed

+73
-2
lines changed

1 file changed

+73
-2
lines changed

src/Elastic.Transport/Components/Serialization/TransportSerializerExtensions.cs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ public static byte[] SerializeToBytes<T>(
4747
SerializationFormatting formatting = SerializationFormatting.None
4848
)
4949
{
50+
if (serializer is SystemTextJsonSerializer stjSerializer)
51+
{
52+
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations.
53+
return JsonSerializer.SerializeToUtf8Bytes(data, stjSerializer.GetJsonSerializerOptions(formatting));
54+
}
55+
5056
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
5157
using var ms = memoryStreamFactory.Create();
5258
serializer.Serialize(data, ms, formatting);
@@ -86,6 +92,12 @@ public static byte[] SerializeToBytes(
8692
SerializationFormatting formatting = SerializationFormatting.None
8793
)
8894
{
95+
if (serializer is SystemTextJsonSerializer stjSerializer)
96+
{
97+
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations.
98+
return JsonSerializer.SerializeToUtf8Bytes(data, type, stjSerializer.GetJsonSerializerOptions(formatting));
99+
}
100+
89101
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
90102
using var ms = memoryStreamFactory.Create();
91103
serializer.Serialize(data, ms, formatting);
@@ -304,6 +316,65 @@ public static void Serialize(
304316
#endif
305317
}
306318

319+
/// <summary>
320+
/// Extension method that deserializes from a UTF8 <see cref="string"/>.
321+
/// </summary>
322+
/// <typeparam name="T">The type of the data to be deserialized.</typeparam>
323+
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
324+
/// <param name="input">The source <see cref="string"/> that contains the JSON.</param>
325+
/// <param name="memoryStreamFactory">
326+
/// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
327+
/// that yields memory streams backed by pooled byte arrays.
328+
/// </param>
329+
/// <returns>The deserialized data.</returns>
330+
public static T? Deserialize<T>(
331+
this Serializer serializer,
332+
string input,
333+
MemoryStreamFactory? memoryStreamFactory = null)
334+
{
335+
if (serializer is SystemTextJsonSerializer stjSerializer)
336+
{
337+
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
338+
// deserialize straight from the span.
339+
return JsonSerializer.Deserialize<T>(input, stjSerializer.GetJsonSerializerOptions());
340+
}
341+
342+
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
343+
using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(input));
344+
345+
return serializer.Deserialize<T>(ms);
346+
}
347+
348+
/// <summary>
349+
/// Extension method that deserializes from a <see cref="string"/>.
350+
/// </summary>
351+
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
352+
/// <param name="input">The source <see cref="string"/> that contains the JSON.</param>
353+
/// <param name="type">The type of the data to be deserialized.</param>
354+
/// <param name="memoryStreamFactory">
355+
/// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
356+
/// that yields memory streams backed by pooled byte arrays.
357+
/// </param>
358+
/// <returns>The deserialized data.</returns>
359+
public static object? Deserialize(
360+
this Serializer serializer,
361+
string input,
362+
Type type,
363+
MemoryStreamFactory? memoryStreamFactory = null)
364+
{
365+
if (serializer is SystemTextJsonSerializer stjSerializer)
366+
{
367+
// When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and
368+
// deserialize straight from the span.
369+
return JsonSerializer.Deserialize(input, type, stjSerializer.GetJsonSerializerOptions());
370+
}
371+
372+
memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory;
373+
using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(input));
374+
375+
return serializer.Deserialize(type, ms);
376+
}
377+
307378
/// <summary>
308379
/// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>.
309380
/// </summary>
@@ -337,7 +408,7 @@ public static void Serialize(
337408
/// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>.
338409
/// </summary>
339410
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
340-
/// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON.</param>
411+
/// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON string.</param>
341412
/// <param name="type">The type of the data to be deserialized.</param>
342413
/// <param name="memoryStreamFactory">
343414
/// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>
@@ -396,7 +467,7 @@ public static void Serialize(
396467
/// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>.
397468
/// </summary>
398469
/// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param>
399-
/// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON.</param>
470+
/// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON string.</param>
400471
/// <param name="type">The type of the data to be deserialized.</param>
401472
/// <param name="memoryStreamFactory">
402473
/// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/>

0 commit comments

Comments
 (0)