generated from seclerp/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate
ArrayTypeConverter
and ObjectTypeConverter
to `System.Tex…
…t.Json`
- Loading branch information
Showing
19 changed files
with
293 additions
and
27 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
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.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
using Xunit; | ||
|
||
namespace ChromeProtocol.Core.Tests; | ||
|
||
[JsonConverter(typeof(ArrayTypeConverter))] | ||
public record TestableArrayType(IReadOnlyCollection<JsonNode> Items) : IArrayType; | ||
|
||
public sealed class ArrayTypeConverterTests | ||
{ | ||
[Fact] | ||
public void Should_deserialize_using_attribute_properly() | ||
{ | ||
// Arrange & Act | ||
var actual = JsonSerializer.Deserialize<TestableArrayType>("[1,2,3]") ?? new TestableArrayType(Array.Empty<JsonNode>()); | ||
|
||
// Assert | ||
Assert.Collection( | ||
actual.Items, | ||
jsonNode => Assert.Equal(expected: 1, jsonNode.GetValue<int>()), | ||
jsonNode => Assert.Equal(expected: 2, jsonNode.GetValue<int>()), | ||
jsonNode => Assert.Equal(expected: 3, jsonNode.GetValue<int>())); | ||
} | ||
|
||
[Fact] | ||
public void Should_serialize_using_attribute_properly() | ||
{ | ||
// Arrange | ||
var expected = new TestableArrayType(new JsonNode[] | ||
{ | ||
JsonValue.Create(1), | ||
JsonValue.Create(2), | ||
JsonValue.Create(3), | ||
}); | ||
|
||
// Act | ||
var actual = JsonSerializer.Serialize(expected); | ||
|
||
// Assert | ||
Assert.Equal("[1,2,3]", actual); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/ChromeProtocol.Core.Tests/ChromeProtocol.Core.Tests.csproj
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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
|
||
<RootNamespace>ChromeProtocol.Core.Tests</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" /> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="System.Text.Json" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector"> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
<PrivateAssets>all</PrivateAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ChromeProtocol.Tests.Extensions\ChromeProtocol.Tests.Extensions.csproj" /> | ||
<ProjectReference Include="..\ChromeProtocol.Core\ChromeProtocol.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,50 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
using Xunit; | ||
|
||
namespace ChromeProtocol.Core.Tests; | ||
|
||
[JsonConverter(typeof(ObjectTypeConverter))] | ||
public record TestableObjectType(IReadOnlyDictionary<string, JsonNode?> Properties) : IObjectType; | ||
|
||
public sealed class ObjectTypeConverterTests | ||
{ | ||
[Fact] | ||
public void Should_deserialize_using_attribute_properly() | ||
{ | ||
// Arrange | ||
var expectedJson = "{ \"foo\": 1, \"bar\": 2 }"; | ||
|
||
// Act | ||
var actual = JsonSerializer.Deserialize<TestableObjectType>(expectedJson) ?? new TestableObjectType(new Dictionary<string, JsonNode?>()); | ||
|
||
// Assert | ||
Assert.Collection( | ||
actual.Properties, | ||
pair => | ||
{ | ||
Assert.Equal("foo", pair.Key); | ||
Assert.Equal(expected: 1, pair.Value?.GetValue<int>()); | ||
}, | ||
pair => | ||
{ | ||
Assert.Equal("bar", pair.Key); | ||
Assert.Equal(expected: 2, pair.Value?.GetValue<int>()); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void Should_serialize_using_attribute_properly() | ||
{ | ||
var expected = new TestableObjectType(new Dictionary<string, JsonNode?> | ||
{ | ||
{ "foo", JsonValue.Create(1) }, | ||
{ "bar", JsonValue.Create(2) } | ||
}); | ||
|
||
var actual = JsonSerializer.Serialize(expected); | ||
|
||
Assert.Equal("{\"foo\":1,\"bar\":2}", actual); | ||
} | ||
} |
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,28 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChromeProtocol.Core; | ||
|
||
public class ArrayTypeConverter : JsonConverter<IArrayType?> | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(IArrayType).IsAssignableFrom(objectType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, IArrayType? value, JsonSerializerOptions options) | ||
{ | ||
var jsonArray = new JsonArray(value?.Items.ToArray() ?? Array.Empty<JsonNode>()); | ||
|
||
jsonArray.WriteTo(writer); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IArrayType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var jsonNode = JsonNode.Parse(ref reader); | ||
return Activator.CreateInstance(typeToConvert, jsonNode.Deserialize<IReadOnlyCollection<JsonNode>>()) as IArrayType; | ||
} | ||
} |
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,8 @@ | ||
using System.Text.Json.Nodes; | ||
|
||
namespace ChromeProtocol.Core; | ||
|
||
public interface IArrayType | ||
{ | ||
public IReadOnlyCollection<JsonNode?> Items { get; } | ||
} |
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,8 @@ | ||
using System.Text.Json.Nodes; | ||
|
||
namespace ChromeProtocol.Core; | ||
|
||
public interface IObjectType | ||
{ | ||
public IReadOnlyDictionary<string, JsonNode?> Properties { get; } | ||
} |
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 |
---|---|---|
|
@@ -3,5 +3,3 @@ | |
public interface IType | ||
{ | ||
} | ||
|
||
public record ExampleValue(string Value) : PrimitiveType<string>(Value); |
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,28 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Nodes; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace ChromeProtocol.Core; | ||
|
||
public class ObjectTypeConverter : JsonConverter<IObjectType?> | ||
{ | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return typeof(IObjectType).IsAssignableFrom(objectType); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, IObjectType? value, JsonSerializerOptions options) | ||
{ | ||
var jsonObject = new JsonObject(value?.Properties ?? Enumerable.Empty<KeyValuePair<string, JsonNode?>>()); | ||
|
||
jsonObject.WriteTo(writer); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override IObjectType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var jsonNode = JsonNode.Parse(ref reader); | ||
return Activator.CreateInstance(typeToConvert, jsonNode.Deserialize<IReadOnlyDictionary<string, JsonNode?>>()) as IObjectType; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.