-
Notifications
You must be signed in to change notification settings - Fork 16
Add SystemTextJsonSerializer base class and relevant extensions methods
#119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a3f1378
Add `SystemTextJsonSerializer` base class and relevant extensions met…
flobernd aaea31a
Add `ReadOnlySpan<byte>` and `ReadOnlySpan<char>` overloads to `Trans…
flobernd 1ce032c
Improve existing `SerializeToBytes()` and `SerializeToString()` exten…
flobernd 2871d17
Simplify code
flobernd 9fcd857
Introduce providing json options as its own construct
Mpdreamz e53e509
CreateJsonSerializerOptions needs to be pure because its called multi…
Mpdreamz f56f1a5
Only call `CreateJsonSerializerOptions` once
flobernd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
src/Elastic.Transport/Components/Serialization/IJsonSerializerOptionsProvider.cs
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.