Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to serializer settings to ignore null values #94

Merged
merged 2 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/SharpYaml.Tests/Serialization/SerializationTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1543,6 +1543,36 @@ public class ClassWithImplicitMemberTypeInner
public string String { get; set; }
}

[Test]
public void TestIgnoreNulls()
{
var serializer = new Serializer();
serializer.Settings.IgnoreNulls = true;
var testObject = new ClassToIgnoreNulls()
{
Id = 10,
Nullable = 3,
};

var text = serializer.Serialize(testObject);
Assert.False(text.Contains("DontSerializeWhenNull: null"));

var deserialized = serializer.Deserialize(new StringReader(text)) as ClassToIgnoreNulls;
Assert.NotNull(deserialized);
Assert.AreEqual(testObject.Id, deserialized.Id);
Assert.Null(deserialized.DontSerializeWhenNull);
Assert.AreEqual(testObject.Nullable, deserialized.Nullable);
}

public class ClassToIgnoreNulls
{
public int Id { get; set; }

public string DontSerializeWhenNull { get; set; }

public int? Nullable { get; set; }
}

private void SerialRoundTrip(SerializerSettings settings, string text, Type serializedType = null)
{
var serializer = new Serializer(settings);
Expand Down
5 changes: 5 additions & 0 deletions src/SharpYaml/Serialization/SerializerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public bool EmitJsonComptible
/// <value><c>true</c> if to emit default value; otherwise, <c>false</c>.</value>
public bool RespectPrivateSetters { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to ignore serialization of properties with null values. Default is false.
/// </summary>
public bool IgnoreNulls { get; set; }

/// <summary>
/// Gets or sets the default key comparer used to sort members (<see cref="IMemberDescriptor"/>) or
/// dictionary keys, when serializing objects as YAML mappings. Default is <see cref="DefaultKeyComparer"/>.
Expand Down
5 changes: 4 additions & 1 deletion src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,13 @@ protected virtual void WriteMember(ref ObjectContext objectContext, IMemberDescr
if (!member.ShouldSerialize(objectContext.Instance))
return;

var memberValue = member.Get(objectContext.Instance);
if (memberValue == null && objectContext.Settings.IgnoreNulls)
return;

// Emit the key name
WriteMemberName(ref objectContext, member, member.Name);

var memberValue = member.Get(objectContext.Instance);
var memberType = member.Type;

// In case of serializing a property/field which is not writeable
Expand Down