diff --git a/InterlockLedger.Commons/Extensions/System/VersionJsonConverter.cs b/InterlockLedger.Commons/Extensions/System/VersionJsonConverter.cs new file mode 100644 index 0000000..317c0c2 --- /dev/null +++ b/InterlockLedger.Commons/Extensions/System/VersionJsonConverter.cs @@ -0,0 +1,32 @@ +// ****************************************************************************************************************************** +// **** +// **** Copyright (c) 2017-2024 InterlockLedger Network +// **** +// ****************************************************************************************************************************** + +using System.Text.Json; + +namespace System; + +public class VersionJsonConverter : JsonConverter +{ +#pragma warning disable IDE0072 // Add missing cases + public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => + reader.TokenType switch { + JsonTokenType.String => Version.Parse(reader.GetString()!), + JsonTokenType.Null => null, + JsonTokenType.None => null, + _ => throw new NotSupportedException(), + }; +#pragma warning restore IDE0072 // Add missing cases + + public override void Write(Utf8JsonWriter writer, Version value, JsonSerializerOptions options) => + WriteVersion(writer.Required(), value); + + private static void WriteVersion(Utf8JsonWriter writer, Version value) { + if (value is null) + writer.WriteNullValue(); + else + writer.WriteStringValue(value.ToString()); + } +} diff --git a/InterlockLedger.Commons/InterlockLedger.Commons.csproj b/InterlockLedger.Commons/InterlockLedger.Commons.csproj index c98ce78..e7a8974 100644 --- a/InterlockLedger.Commons/InterlockLedger.Commons.csproj +++ b/InterlockLedger.Commons/InterlockLedger.Commons.csproj @@ -11,9 +11,9 @@ InterlockLedger git https://github.com/interlockledger/interlockledger-commons.git - 18.2.1 + 18.3.0 InterlockLedger.Commons - Json deserialization fails for PageOf<T> as a record + Json converter for System.Version il2.png true true diff --git a/InterlockLedger.Commons/Types/System/Result.cs b/InterlockLedger.Commons/Types/System/Result.cs index 97c56d4..668451c 100644 --- a/InterlockLedger.Commons/Types/System/Result.cs +++ b/InterlockLedger.Commons/Types/System/Result.cs @@ -114,3 +114,17 @@ private Error(Exception? error, string? errorMessage = null, int errorType = IEr ErrorMessage = Exception?.Message ?? errorMessage ?? string.Empty; } } +public static class ResultExtensions +{ + public static Error ToConvertedError(this IError err) => + err.Exception is not null + ? new Error(err.Exception, err.ErrorType) + : new Error(err.ErrorMessage, err.ErrorType); + + public static Result ToConvertedResult(this Result result, Func convert) => + result.Required() switch { + IError error => error.ToConvertedError(), + _ => new Result(convert.Required()(result.Value)!) + }; +} +