Skip to content

Commit

Permalink
Merge pull request #94 from sescalada/ignore-nulls
Browse files Browse the repository at this point in the history
Add option to serializer settings to ignore null values
  • Loading branch information
xoofx authored Mar 6, 2022
2 parents d41ce9b + 895facf commit 3dbe70b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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

0 comments on commit 3dbe70b

Please sign in to comment.