diff --git a/README.md b/README.md index ed6f120..8af82a4 100644 --- a/README.md +++ b/README.md @@ -124,5 +124,5 @@ public void Deserialization() target the .NET Standard 2.0 and can be used with various of .NET architecture (.NET Core, .NET Framework,...). - The [PosInformatique.FluentAssertions.Json](https://www.nuget.org/packages/PosInformatique.FluentAssertions.Json/) library -use the 4.6.0 version of the [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/) NuGet package +use the 5.0.0 version of the [System.Text.Json](https://www.nuget.org/packages/System.Text.Json/) NuGet package and can be used with old projects that target this library version and earlier. \ No newline at end of file diff --git a/src/FluentAssertions.Json/FluentAssertions.Json.csproj b/src/FluentAssertions.Json/FluentAssertions.Json.csproj index e17148c..a58679b 100644 --- a/src/FluentAssertions.Json/FluentAssertions.Json.csproj +++ b/src/FluentAssertions.Json/FluentAssertions.Json.csproj @@ -11,15 +11,18 @@ https://github.com/PosInformatique/PosInformatique.FluentAssertions.Json README.md + 1.0.4 + - Ignore the properties with the [JsonIgnore] attribute. + 1.0.3 - Target the .NET Standard 2.0 instead of .NET Core 6.0 - + 1.0.2 - Fix the README.md file. - + 1.0.1 - Various fixes for the NuGet package description. - + 1.0.0 - Initial version @@ -28,7 +31,7 @@ - + diff --git a/src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs b/src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs index 2fe63d8..10629c3 100644 --- a/src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs +++ b/src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs @@ -7,15 +7,17 @@ namespace FluentAssertions { using System.Diagnostics; + using System.Net; + using System.Reflection; using System.Text.Json; using System.Text.Json.Serialization; using FluentAssertions.Common; + using FluentAssertions.Equivalency; using FluentAssertions.Primitives; /// /// Contains extension methods to check if an object is serializable or deserializable to JSON object. /// - [DebuggerNonUserCode] public static class JsonFluentAssertionsExtensions { private static readonly JsonSerializerOptions JsonSerializationOptions = new JsonSerializerOptions() @@ -66,7 +68,27 @@ public static void BeJsonDeserializableInto(this ObjectAssertions assertions, var jsonText = JsonSerializer.Serialize(assertions.Subject, JsonSerializationOptions); var deserializedObject = JsonSerializer.Deserialize(jsonText, JsonSerializationOptions); - deserializedObject.Should().BeEquivalentTo(expectedObject); + deserializedObject.Should().BeEquivalentTo(expectedObject, opt => + { + return opt.Excluding(member => IsIgnoredProperty(member)); + }); + } + + private static bool IsIgnoredProperty(IMemberInfo member) + { + var property = member.SelectedMemberInfo.DeclaringType.GetProperty(member.SelectedMemberInfo.Name); + + var attribute = property.GetCustomAttribute(); + + if (attribute is not null) + { + if (attribute.Condition == JsonIgnoreCondition.Always) + { + return true; + } + } + + return false; } private static void Compare(JsonDocument document, JsonDocument expected) diff --git a/tests/FluentAssertions.Json.Tests/JsonFluentAssertionsExtensionsTest.cs b/tests/FluentAssertions.Json.Tests/JsonFluentAssertionsExtensionsTest.cs index 520e264..a360bdd 100644 --- a/tests/FluentAssertions.Json.Tests/JsonFluentAssertionsExtensionsTest.cs +++ b/tests/FluentAssertions.Json.Tests/JsonFluentAssertionsExtensionsTest.cs @@ -524,16 +524,32 @@ private class JsonSerializableClass public JsonSerializableClassInnerObject InnerObject { get; set; } [JsonPropertyName("collection_int")] + [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public List CollectionInt32 { get; set; } [JsonPropertyName("collection_object")] public List CollectionObjects { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] + public string IgnoredProperty + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } } private class JsonSerializableClassInnerObject { [JsonPropertyName("inner_string_property")] + [JsonIgnore(Condition = JsonIgnoreCondition.Never)] public string InnerStringProperty { get; set; } + + [JsonIgnore(Condition = JsonIgnoreCondition.Always)] + public string InnerIgnoredProperty + { + get => throw new NotImplementedException(); + set => throw new NotImplementedException(); + } } } } \ No newline at end of file