-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SystemTextJsonSerializer
base class and relevant extensions met…
…hods (#119) Add `SystemTextJsonSerializer` base class and relevant extensions methods. Closes #117 --------- Co-authored-by: Martijn Laarman <[email protected]>
- Loading branch information
Showing
5 changed files
with
745 additions
and
119 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Elastic.Transport/Components/Serialization/IJsonSerializerOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Elastic.Transport; | ||
|
||
/// <summary> | ||
/// Provides an instance of <see cref="JsonSerializerOptions"/> to <see cref="SystemTextJsonSerializer"/> | ||
/// </summary> | ||
public interface IJsonSerializerOptionsProvider | ||
{ | ||
/// <inheritdoc cref="IJsonSerializerOptionsProvider"/> | ||
JsonSerializerOptions CreateJsonSerializerOptions(); | ||
} | ||
|
||
/// <summary> | ||
/// Default implementation of <see cref="IJsonSerializerOptionsProvider"/> specialized in providing more converters and | ||
/// altering the shared <see cref="JsonSerializerOptions"/> used by <see cref="SystemTextJsonSerializer"/> and its derived classes | ||
/// </summary> | ||
public class TransportSerializerOptionsProvider : IJsonSerializerOptionsProvider | ||
{ | ||
private readonly IReadOnlyCollection<JsonConverter>? _bakedInConverters; | ||
private readonly IReadOnlyCollection<JsonConverter>? _userProvidedConverters; | ||
private readonly Action<JsonSerializerOptions>? _mutateOptions; | ||
|
||
/// <inheritdoc cref="IJsonSerializerOptionsProvider"/> | ||
public JsonSerializerOptions? CreateJsonSerializerOptions() | ||
{ | ||
var options = new JsonSerializerOptions(); | ||
|
||
foreach (var converter in _bakedInConverters ?? []) | ||
options.Converters.Add(converter); | ||
|
||
foreach (var converter in _userProvidedConverters ?? []) | ||
options.Converters.Add(converter); | ||
|
||
_mutateOptions?.Invoke(options); | ||
|
||
return options; | ||
} | ||
|
||
/// <inheritdoc cref="TransportSerializerOptionsProvider"/> | ||
public TransportSerializerOptionsProvider() { } | ||
|
||
/// <inheritdoc cref="TransportSerializerOptionsProvider"/> | ||
public TransportSerializerOptionsProvider( | ||
IReadOnlyCollection<JsonConverter> bakedInConverters, | ||
IReadOnlyCollection<JsonConverter>? userProvidedConverters, | ||
Action<JsonSerializerOptions>? mutateOptions = null | ||
) | ||
{ | ||
_bakedInConverters = bakedInConverters; | ||
_userProvidedConverters = userProvidedConverters; | ||
_mutateOptions = mutateOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/Elastic.Transport/Components/Serialization/SystemTextJsonSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Licensed to Elasticsearch B.V under one or more agreements. | ||
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
// See the LICENSE file in the project root for more information | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Elastic.Transport; | ||
|
||
/// <summary> | ||
/// An abstract implementation of a transport <see cref="Serializer"/> which serializes using the Microsoft | ||
/// <c>System.Text.Json</c> library. | ||
/// </summary> | ||
public abstract class SystemTextJsonSerializer : Serializer | ||
{ | ||
private readonly JsonSerializerOptions? _options; | ||
private readonly JsonSerializerOptions? _indentedOptions; | ||
|
||
/// <summary> | ||
/// An abstract implementation of a transport <see cref="Serializer"/> which serializes using the Microsoft | ||
/// <c>System.Text.Json</c> library. | ||
/// </summary> | ||
protected SystemTextJsonSerializer(IJsonSerializerOptionsProvider? provider = null) | ||
{ | ||
provider ??= new TransportSerializerOptionsProvider(); | ||
_options = provider.CreateJsonSerializerOptions(); | ||
_indentedOptions = new JsonSerializerOptions(_options) | ||
{ | ||
WriteIndented = true | ||
}; | ||
} | ||
|
||
#region Serializer | ||
|
||
/// <inheritdoc /> | ||
public override T Deserialize<T>(Stream stream) | ||
{ | ||
if (TryReturnDefault(stream, out T deserialize)) | ||
return deserialize; | ||
|
||
return JsonSerializer.Deserialize<T>(stream, GetJsonSerializerOptions()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override object? Deserialize(Type type, Stream stream) | ||
{ | ||
if (TryReturnDefault(stream, out object deserialize)) | ||
return deserialize; | ||
|
||
return JsonSerializer.Deserialize(stream, type, GetJsonSerializerOptions()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ValueTask<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) | ||
{ | ||
if (TryReturnDefault(stream, out T deserialize)) | ||
return new ValueTask<T>(deserialize); | ||
|
||
return JsonSerializer.DeserializeAsync<T>(stream, GetJsonSerializerOptions(), cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ValueTask<object?> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default) | ||
{ | ||
if (TryReturnDefault(stream, out object deserialize)) | ||
return new ValueTask<object?>(deserialize); | ||
|
||
return JsonSerializer.DeserializeAsync(stream, type, GetJsonSerializerOptions(), cancellationToken); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Serialize<T>(T data, Stream writableStream, | ||
SerializationFormatting formatting = SerializationFormatting.None) => | ||
JsonSerializer.Serialize(writableStream, data, GetJsonSerializerOptions(formatting)); | ||
|
||
/// <inheritdoc /> | ||
public override Task SerializeAsync<T>(T data, Stream stream, | ||
SerializationFormatting formatting = SerializationFormatting.None, | ||
CancellationToken cancellationToken = default) => | ||
JsonSerializer.SerializeAsync(stream, data, GetJsonSerializerOptions(formatting), cancellationToken); | ||
|
||
#endregion Serializer | ||
|
||
/// <summary> | ||
/// Returns the <see cref="JsonSerializerOptions"/> for this serializer, based on the given <paramref name="formatting"/>. | ||
/// </summary> | ||
/// <param name="formatting">The serialization formatting.</param> | ||
/// <returns>The requested <see cref="JsonSerializerOptions"/> or <c>null</c>, if the serializer is not initialized yet.</returns> | ||
protected internal JsonSerializerOptions? GetJsonSerializerOptions(SerializationFormatting formatting = SerializationFormatting.None) => | ||
formatting is SerializationFormatting.None ? _options : _indentedOptions; | ||
|
||
private static bool TryReturnDefault<T>(Stream? stream, out T deserialize) | ||
{ | ||
deserialize = default; | ||
return (stream is null) || stream == Stream.Null || (stream.CanSeek && stream.Length == 0); | ||
} | ||
} |
Oops, something went wrong.