-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow KeyValueStore serializer to be replaced (#354)
Modifies `KeyValueStore` so that serialization is delegated to an `IKeyValueStoreSerializer` instead of always using System.Text.Json (although STJ is still the default serializer). A custom serializer can now be specified via a new property on `KeyValueStoreOptions`.
- Loading branch information
1 parent
cf63865
commit e3bc27d
Showing
8 changed files
with
137 additions
and
213 deletions.
There are no files selected for viewing
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
44 changes: 44 additions & 0 deletions
44
src/DataCore.Adapter.Abstractions/Services/IKeyValueStoreSerializer.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,44 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataCore.Adapter.Services { | ||
|
||
/// <summary> | ||
/// A serializer for key/value store values. | ||
/// </summary> | ||
public interface IKeyValueStoreSerializer { | ||
|
||
/// <summary> | ||
/// Serializes a value to a stream. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The value type. | ||
/// </typeparam> | ||
/// <param name="stream"> | ||
/// The stream to write to. | ||
/// </param> | ||
/// <param name="value"> | ||
/// The value to serialize. | ||
/// </param> | ||
/// <returns> | ||
/// A <see cref="ValueTask"/> that will serialize the value. | ||
/// </returns> | ||
ValueTask SerializeAsync<T>(Stream stream, T value); | ||
|
||
/// <summary> | ||
/// Deserializes a value from a stream. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// The value type. | ||
/// </typeparam> | ||
/// <param name="stream"> | ||
/// The stream to read from. | ||
/// </param> | ||
/// <returns> | ||
/// A <see cref="ValueTask{TResult}"/> that will return the deserialized value. | ||
/// </returns> | ||
ValueTask<T?> DeserializeAsync<T>(Stream stream); | ||
|
||
} | ||
|
||
} |
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
52 changes: 52 additions & 0 deletions
52
src/DataCore.Adapter.Abstractions/Services/JsonKeyValueStoreSerializer.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,52 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace DataCore.Adapter.Services { | ||
|
||
/// <summary> | ||
/// <see cref="IKeyValueStoreSerializer"/> implementation that uses <see cref="JsonSerializer"/>. | ||
/// </summary> | ||
public sealed class JsonKeyValueStoreSerializer : IKeyValueStoreSerializer { | ||
|
||
/// <summary> | ||
/// The JSON serializer options. | ||
/// </summary> | ||
private readonly JsonSerializerOptions? _jsonOptions; | ||
|
||
|
||
/// <summary> | ||
/// The <see cref="JsonKeyValueStoreSerializer"/> instance. | ||
/// </summary> | ||
public static IKeyValueStoreSerializer Default { get; } = new JsonKeyValueStoreSerializer(null); | ||
|
||
|
||
/// <summary> | ||
/// Creates a new <see cref="JsonKeyValueStoreSerializer"/> instance. | ||
/// </summary> | ||
/// <param name="jsonOptions"></param> | ||
public JsonKeyValueStoreSerializer(JsonSerializerOptions? jsonOptions) { | ||
_jsonOptions = jsonOptions; | ||
} | ||
|
||
|
||
/// <inheritdoc/> | ||
public async ValueTask SerializeAsync<T>(Stream stream, T value) { | ||
if (stream == null) { | ||
throw new ArgumentNullException(nameof(stream)); | ||
} | ||
await JsonSerializer.SerializeAsync(stream, value, _jsonOptions).ConfigureAwait(false); | ||
} | ||
|
||
|
||
/// <inheritdoc/> | ||
public async ValueTask<T?> DeserializeAsync<T>(Stream stream) { | ||
if (stream == null) { | ||
throw new ArgumentNullException(nameof(stream)); | ||
} | ||
return await JsonSerializer.DeserializeAsync<T>(stream, _jsonOptions).ConfigureAwait(false); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.