From e22d10c00057b2b21a9eed860d1ccb77ed5db34a Mon Sep 17 00:00:00 2001 From: Sergio Escalada Date: Tue, 15 Feb 2022 17:24:43 +0100 Subject: [PATCH 1/2] Add option to serializer settings to ignore null values --- .../Serialization/SerializationTests2.cs | 30 +++++++++++++++++++ .../Serialization/SerializerSettings.cs | 5 ++++ .../Serializers/ObjectSerializer.cs | 5 +++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs index 27450a4a..08dc3ff2 100644 --- a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs +++ b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs @@ -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("Name: 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); diff --git a/src/SharpYaml/Serialization/SerializerSettings.cs b/src/SharpYaml/Serialization/SerializerSettings.cs index b5f8bce9..2ccd2936 100644 --- a/src/SharpYaml/Serialization/SerializerSettings.cs +++ b/src/SharpYaml/Serialization/SerializerSettings.cs @@ -195,6 +195,11 @@ public bool EmitJsonComptible /// true if to emit default value; otherwise, false. public bool RespectPrivateSetters { get; set; } + /// + /// Gets or sets a value indicating whether to ignore serialization of properties with null values. Default is false. + /// + public bool IgnoreNulls { get; set; } + /// /// Gets or sets the default key comparer used to sort members () or /// dictionary keys, when serializing objects as YAML mappings. Default is . diff --git a/src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs b/src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs index 57c51dd4..5b403be2 100644 --- a/src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs +++ b/src/SharpYaml/Serialization/Serializers/ObjectSerializer.cs @@ -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 From 895facf85ca3dab56de44452d2ca46c5745dc32c Mon Sep 17 00:00:00 2001 From: Sergio Escalada Date: Tue, 15 Feb 2022 20:55:30 +0100 Subject: [PATCH 2/2] Fix: forgot to also rename Assert property check --- src/SharpYaml.Tests/Serialization/SerializationTests2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs index 08dc3ff2..9ffc3954 100644 --- a/src/SharpYaml.Tests/Serialization/SerializationTests2.cs +++ b/src/SharpYaml.Tests/Serialization/SerializationTests2.cs @@ -1555,7 +1555,7 @@ public void TestIgnoreNulls() }; var text = serializer.Serialize(testObject); - Assert.False(text.Contains("Name: null")); + Assert.False(text.Contains("DontSerializeWhenNull: null")); var deserialized = serializer.Deserialize(new StringReader(text)) as ClassToIgnoreNulls; Assert.NotNull(deserialized);