Skip to content

Commit

Permalink
v18.3.0 - Json converter for System.Version
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Jul 3, 2024
1 parent 86c7d93 commit 45290e1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
32 changes: 32 additions & 0 deletions InterlockLedger.Commons/Extensions/System/VersionJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// ******************************************************************************************************************************
// ****
// **** Copyright (c) 2017-2024 InterlockLedger Network
// ****
// ******************************************************************************************************************************

using System.Text.Json;

namespace System;

public class VersionJsonConverter : JsonConverter<Version>
{
#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());
}
}
4 changes: 2 additions & 2 deletions InterlockLedger.Commons/InterlockLedger.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<Product>InterlockLedger</Product>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/interlockledger/interlockledger-commons.git</RepositoryUrl>
<Version>18.2.1</Version>
<Version>18.3.0</Version>
<PackageId>InterlockLedger.Commons</PackageId>
<PackageReleaseNotes>Json deserialization fails for PageOf&lt;T&gt; as a record</PackageReleaseNotes>
<PackageReleaseNotes>Json converter for System.Version</PackageReleaseNotes>
<PackageIcon>il2.png</PackageIcon>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
Expand Down
14 changes: 14 additions & 0 deletions InterlockLedger.Commons/Types/System/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TOut> ToConvertedError<TOut>(this IError err) =>
err.Exception is not null
? new Error<TOut>(err.Exception, err.ErrorType)
: new Error<TOut>(err.ErrorMessage, err.ErrorType);

public static Result<TOut> ToConvertedResult<TOut, TIn>(this Result<TIn> result, Func<TIn?, TOut?> convert) =>
result.Required() switch {
IError error => error.ToConvertedError<TOut>(),
_ => new Result<TOut>(convert.Required()(result.Value)!)
};
}

0 comments on commit 45290e1

Please sign in to comment.