Skip to content

Commit

Permalink
Ignore the properties with the JsonIgnore attribute. (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
GillesTourreau authored Oct 15, 2023
1 parent f26ce3c commit f061c4a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 7 additions & 4 deletions src/FluentAssertions.Json/FluentAssertions.Json.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
<PackageProjectUrl>https://github.com/PosInformatique/PosInformatique.FluentAssertions.Json</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageReleaseNotes>
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
</PackageReleaseNotes>
Expand All @@ -28,7 +31,7 @@

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="4.6.0" />
<PackageReference Include="System.Text.Json" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
26 changes: 24 additions & 2 deletions src/FluentAssertions.Json/JsonFluentAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Contains extension methods to check if an object is serializable or deserializable to JSON object.
/// </summary>
[DebuggerNonUserCode]
public static class JsonFluentAssertionsExtensions
{
private static readonly JsonSerializerOptions JsonSerializationOptions = new JsonSerializerOptions()
Expand Down Expand Up @@ -66,7 +68,27 @@ public static void BeJsonDeserializableInto<T>(this ObjectAssertions assertions,
var jsonText = JsonSerializer.Serialize(assertions.Subject, JsonSerializationOptions);
var deserializedObject = JsonSerializer.Deserialize<T>(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<JsonIgnoreAttribute>();

if (attribute is not null)
{
if (attribute.Condition == JsonIgnoreCondition.Always)
{
return true;
}
}

return false;
}

private static void Compare(JsonDocument document, JsonDocument expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,16 +524,32 @@ private class JsonSerializableClass
public JsonSerializableClassInnerObject InnerObject { get; set; }

[JsonPropertyName("collection_int")]
[JsonIgnore(Condition = JsonIgnoreCondition.Never)]
public List<int> CollectionInt32 { get; set; }

[JsonPropertyName("collection_object")]
public List<JsonSerializableClassInnerObject> 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();
}
}
}
}

0 comments on commit f061c4a

Please sign in to comment.