-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add semantic version system.text.json converter.
- Loading branch information
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/Snap.Tests/Core/Json/SemanticVersionSystemTextJsonConverterTests.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,30 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using NuGet.Versioning; | ||
using Snap.Core.Json; | ||
using Xunit; | ||
|
||
namespace Snap.Tests.Core.Json; | ||
|
||
file sealed class SemanticVersionDto | ||
{ | ||
[JsonInclude, JsonConverter(typeof(SemanticVersionSystemTextJsonConverter))] | ||
public SemanticVersion Value { get; init; } | ||
} | ||
|
||
public sealed class SemanticVersionSystemTextJsonConverterTests | ||
{ | ||
[Fact] | ||
public void TestSerializeDeserialize() | ||
{ | ||
var dto = new SemanticVersionDto | ||
{ | ||
Value = new SemanticVersion(1, 0, 0, "abc", "metadata") | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(dto); | ||
var deserializedDto = JsonSerializer.Deserialize<SemanticVersionDto>(json); | ||
Assert.NotNull(deserializedDto); | ||
Assert.Equal(dto.Value, deserializedDto.Value); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Snap/Core/Json/SemanticVersionSystemTextJsonConverter.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,23 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using NuGet.Versioning; | ||
|
||
namespace Snap.Core.Json; | ||
|
||
public sealed class SemanticVersionSystemTextJsonConverter : JsonConverter<SemanticVersion> | ||
{ | ||
public override SemanticVersion Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var value = reader.GetString(); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return null; | ||
} | ||
_ = SemanticVersion.TryParse(value, out var semanticVersion); | ||
return semanticVersion; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, SemanticVersion value, JsonSerializerOptions options) => | ||
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