-
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.
v18.3.0 - Json converter for System.Version
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
InterlockLedger.Commons/Extensions/System/VersionJsonConverter.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,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()); | ||
} | ||
} |
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