|
5 | 5 | using System.IO; |
6 | 6 | using System.Text.Json; |
7 | 7 | using System; |
| 8 | +using System.Text; |
8 | 9 | using System.Text.Json.Nodes; |
9 | 10 |
|
10 | 11 | namespace Elastic.Transport.Extensions; |
@@ -80,6 +81,124 @@ public static string SerializeToString<T>( |
80 | 81 |
|
81 | 82 | #region STJ Extensions |
82 | 83 |
|
| 84 | + /// <summary> |
| 85 | + /// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>. |
| 86 | + /// </summary> |
| 87 | + /// <typeparam name="T">The type of the data to be deserialized.</typeparam> |
| 88 | + /// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param> |
| 89 | + /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON string.</param> |
| 90 | + /// <param name="memoryStreamFactory"> |
| 91 | + /// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/> |
| 92 | + /// that yields memory streams backed by pooled byte arrays. |
| 93 | + /// </param> |
| 94 | + /// <returns>The deserialized data.</returns> |
| 95 | + public static T? Deserialize<T>( |
| 96 | + this Serializer serializer, |
| 97 | + ReadOnlySpan<byte> span, |
| 98 | + MemoryStreamFactory? memoryStreamFactory = null) |
| 99 | + { |
| 100 | + if (serializer is SystemTextJsonSerializer stjSerializer) |
| 101 | + { |
| 102 | + // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
| 103 | + // deserialize straight from the span. |
| 104 | + return JsonSerializer.Deserialize<T>(span, stjSerializer.GetJsonSerializerOptions()); |
| 105 | + } |
| 106 | + |
| 107 | + memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
| 108 | + using var ms = memoryStreamFactory.Create(span.ToArray()); |
| 109 | + |
| 110 | + return serializer.Deserialize<T>(ms); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>. |
| 115 | + /// </summary> |
| 116 | + /// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param> |
| 117 | + /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON.</param> |
| 118 | + /// <param name="type">The type of the data to be deserialized.</param> |
| 119 | + /// <param name="memoryStreamFactory"> |
| 120 | + /// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/> |
| 121 | + /// that yields memory streams backed by pooled byte arrays. |
| 122 | + /// </param> |
| 123 | + /// <returns>The deserialized data.</returns> |
| 124 | + public static object? Deserialize( |
| 125 | + this Serializer serializer, |
| 126 | + ReadOnlySpan<byte> span, |
| 127 | + Type type, |
| 128 | + MemoryStreamFactory? memoryStreamFactory = null) |
| 129 | + { |
| 130 | + if (serializer is SystemTextJsonSerializer stjSerializer) |
| 131 | + { |
| 132 | + // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
| 133 | + // deserialize straight from the span. |
| 134 | + return JsonSerializer.Deserialize(span, type, stjSerializer.GetJsonSerializerOptions()); |
| 135 | + } |
| 136 | + |
| 137 | + memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
| 138 | + using var ms = memoryStreamFactory.Create(span.ToArray()); |
| 139 | + |
| 140 | + return serializer.Deserialize(type, ms); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>. |
| 145 | + /// </summary> |
| 146 | + /// <typeparam name="T">The type of the data to be deserialized.</typeparam> |
| 147 | + /// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param> |
| 148 | + /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON string.</param> |
| 149 | + /// <param name="memoryStreamFactory"> |
| 150 | + /// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/> |
| 151 | + /// that yields memory streams backed by pooled byte arrays. |
| 152 | + /// </param> |
| 153 | + /// <returns>The deserialized data.</returns> |
| 154 | + public static T? Deserialize<T>( |
| 155 | + this Serializer serializer, |
| 156 | + ReadOnlySpan<char> span, |
| 157 | + MemoryStreamFactory? memoryStreamFactory = null) |
| 158 | + { |
| 159 | + if (serializer is SystemTextJsonSerializer stjSerializer) |
| 160 | + { |
| 161 | + // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
| 162 | + // deserialize straight from the span. |
| 163 | + return JsonSerializer.Deserialize<T>(span, stjSerializer.GetJsonSerializerOptions()); |
| 164 | + } |
| 165 | + |
| 166 | + memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
| 167 | + using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray())); |
| 168 | + |
| 169 | + return serializer.Deserialize<T>(ms); |
| 170 | + } |
| 171 | + |
| 172 | + /// <summary> |
| 173 | + /// Extension method that deserializes from a UTF8 <see cref="ReadOnlySpan{T}"/>. |
| 174 | + /// </summary> |
| 175 | + /// <param name="serializer"><inheritdoc cref="Serializer" path="/summary"/></param> |
| 176 | + /// <param name="span">The source <see cref="ReadOnlySpan{T}"/> that contains the UTF8 encoded JSON.</param> |
| 177 | + /// <param name="type">The type of the data to be deserialized.</param> |
| 178 | + /// <param name="memoryStreamFactory"> |
| 179 | + /// A factory yielding <see cref="MemoryStream"/> instances, defaults to <see cref="RecyclableMemoryStreamFactory"/> |
| 180 | + /// that yields memory streams backed by pooled byte arrays. |
| 181 | + /// </param> |
| 182 | + /// <returns>The deserialized data.</returns> |
| 183 | + public static object? Deserialize( |
| 184 | + this Serializer serializer, |
| 185 | + ReadOnlySpan<char> span, |
| 186 | + Type type, |
| 187 | + MemoryStreamFactory? memoryStreamFactory = null) |
| 188 | + { |
| 189 | + if (serializer is SystemTextJsonSerializer stjSerializer) |
| 190 | + { |
| 191 | + // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
| 192 | + // deserialize straight from the span. |
| 193 | + return JsonSerializer.Deserialize(span, type, stjSerializer.GetJsonSerializerOptions()); |
| 194 | + } |
| 195 | + |
| 196 | + memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
| 197 | + using var ms = memoryStreamFactory.Create(Encoding.UTF8.GetBytes(span.ToArray())); |
| 198 | + |
| 199 | + return serializer.Deserialize(type, ms); |
| 200 | + } |
| 201 | + |
83 | 202 | /// <summary> |
84 | 203 | /// Extension method that writes the serialized representation of an instance of <typeparamref name="T"/> to a |
85 | 204 | /// <see cref="Utf8JsonWriter"/>. |
@@ -185,7 +304,7 @@ public static void Serialize( |
185 | 304 | { |
186 | 305 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
187 | 306 | // deserialize straight from the reader. |
188 | | - return JsonSerializer.Deserialize<T>(ref reader, stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 307 | + return JsonSerializer.Deserialize<T>(ref reader, stjSerializer.GetJsonSerializerOptions()); |
189 | 308 | } |
190 | 309 |
|
191 | 310 | using var jsonDoc = JsonSerializer.Deserialize<JsonDocument>(ref reader); |
@@ -222,7 +341,7 @@ public static void Serialize( |
222 | 341 | { |
223 | 342 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
224 | 343 | // deserialize straight from the reader. |
225 | | - return JsonSerializer.Deserialize(ref reader, type, stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 344 | + return JsonSerializer.Deserialize(ref reader, type, stjSerializer.GetJsonSerializerOptions()); |
226 | 345 | } |
227 | 346 |
|
228 | 347 | using var jsonDoc = JsonSerializer.Deserialize<JsonDocument>(ref reader); |
@@ -258,7 +377,7 @@ public static void Serialize( |
258 | 377 | { |
259 | 378 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
260 | 379 | // deserialize straight from the node. |
261 | | - return node.Deserialize<T>(stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 380 | + return node.Deserialize<T>(stjSerializer.GetJsonSerializerOptions()); |
262 | 381 | } |
263 | 382 |
|
264 | 383 | memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
@@ -293,7 +412,7 @@ public static void Serialize( |
293 | 412 | { |
294 | 413 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
295 | 414 | // deserialize straight from the node. |
296 | | - return node.Deserialize(type, stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 415 | + return node.Deserialize(type, stjSerializer.GetJsonSerializerOptions()); |
297 | 416 | } |
298 | 417 |
|
299 | 418 | memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
@@ -327,7 +446,7 @@ public static void Serialize( |
327 | 446 | { |
328 | 447 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
329 | 448 | // deserialize straight from the node. |
330 | | - return node.Deserialize<T>(stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 449 | + return node.Deserialize<T>(stjSerializer.GetJsonSerializerOptions()); |
331 | 450 | } |
332 | 451 |
|
333 | 452 | memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
@@ -362,7 +481,7 @@ public static void Serialize( |
362 | 481 | { |
363 | 482 | // When the serializer derives from `SystemTextJsonSerializer` we can avoid unnecessary allocations and |
364 | 483 | // deserialize straight from the node. |
365 | | - return node.Deserialize(type, stjSerializer.GetJsonSerializerOptions(SerializationFormatting.None)); |
| 484 | + return node.Deserialize(type, stjSerializer.GetJsonSerializerOptions()); |
366 | 485 | } |
367 | 486 |
|
368 | 487 | memoryStreamFactory ??= TransportConfiguration.DefaultMemoryStreamFactory; |
|
0 commit comments