Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Mar 10, 2025
1 parent 69bc719 commit ca0db71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/Verify/Serialization/SerializationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public void AddExtraSettings(Action<JsonSerializerSettings> action)
List<Action<JsonSerializerSettings>> extraSettings = [];
JsonSerializer? serializer;

internal bool IgnoreNulls => Serializer.NullValueHandling.GetValueOrDefault(NullValueHandling.Ignore) == NullValueHandling.Ignore;

internal JsonSerializer Serializer
{
get
Expand Down
58 changes: 26 additions & 32 deletions src/Verify/Serialization/VerifyJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,7 @@ public void WriteMember(object target, object? value, string name)
{
if (value is null)
{
if (serialization.TryGetScrubOrIgnoreByName(name, out var scrubOrIgnoreByName))
{
if (scrubOrIgnoreByName == ScrubOrIgnore.Ignore)
{
return;
}

WritePropertyName(name);
WriteRawValueIfNoStrict("Scrubbed");

return;
}

if (serialization.Serializer.NullValueHandling.GetValueOrDefault(NullValueHandling.Ignore) == NullValueHandling.Ignore)
{
return;
}

WritePropertyName(name);
WriteNull();
WriteNullMember(name);
return;
}

Expand Down Expand Up @@ -281,36 +262,49 @@ public void WriteMember(object target, object? value, string name)
var converter = VerifierSettings.GetMemberConverter(declaringType, name);
if (converter is not null)
{
var converted = converter(target, value);
if (converted is null)
value = converter(target, value);
if (value is null)
{
return;
}

WritePropertyName(name);
WriteOrSerialize(converted);

return;
}

WritePropertyName(name);
WriteOrSerialize(value);
}

void WriteNullMember(string name)
{
if (serialization.TryGetScrubOrIgnoreByName(name, out var scrubOrIgnoreByName))
{
if (scrubOrIgnoreByName != ScrubOrIgnore.Ignore)
{
WritePropertyName(name);
WriteRawValueIfNoStrict("Scrubbed");
}
}
else if (!serialization.IgnoreNulls)
{
WritePropertyName(name);
WriteNull();
}
}

void WriteOrSerialize(object converted)
{
if (converted is string convertedString)
{
WriteValue(convertedString);
return;
}
else if (converted.GetType().IsPrimitive)

if (converted.GetType().IsPrimitive)
{
WriteValue(converted);
return;
}
else
{
settings.Serializer.Serialize(this, converted);
}

settings.Serializer.Serialize(this, converted);
}

/// <summary>
Expand Down

0 comments on commit ca0db71

Please sign in to comment.