question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Null serialization creates <Null /> node ignoring property name instead of using xsi:nil="true"

See original GitHub issue

When serializing property with null value, the serializer creates <Null xmlns="https://extendedxmlserializer.github.io/v2"/> node ignoring the property name.

Example output:

<?xml version="1.0" encoding="utf-8"?>
<ClassWithString xmlns="clr-namespace:ExtendedXmlSerializer.Tests.ReportedIssues;assembly=ExtendedXmlSerializer.Tests">
   <Null xmlns="https://extendedxmlserializer.github.io/v2" />
</ClassWithString>

Repro:

var config = new ConfigurationContainer();
config.Emit(EmitBehaviors.Always);
var serializer = config.Create();
string xml = serializer.Serialize(new ClassWithString());

public class ClassWithString
{
    public string Name { get; set; }
}

This leads us to situation where serializer does not preserve the null value if there is custom default value defined, even if it`s being forced to serialize it.

        [Fact]
        public void ShouldPreserveNullValueIfDefaultIsNotNull()
        {
            var config = new ConfigurationContainer();
            config.Emit(EmitBehaviors.Always); //no matter what
            var serializer = config.Create();

            string xml = serializer.Serialize(new ClassWithDefautString() { Name = null });
            var deserialized = serializer.Deserialize<ClassWithDefautString>(xml);

            deserialized.Name.Should().BeNull();    //fail
        }

        public class ClassWithDefautString
        {
            public string Name { get; set; } = "Unnamed";
        }

Newtonsoft.Json handles this using null keyword which has no equivalent in XML. I hope it could be emulated using syntax like <Name xsi:nil="true"/> or <Name><Null xmlns="https://extendedxmlserializer.github.io/v2"/></Name>. The issue will probably span to all reference types. I have no idea how nulls could be handled in attributes.

My particular use case is serialization of config objects that have default values for all the properties. I would want to skip unchanged ones for sake of readability, however saving explicit nulls is not preserved.

BTW: I would like to thank all the authors for their great job! This is the most feature rich XML serializer on the market 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
WojciechNagorskicommented, Jan 26, 2018

@Mike-EEE We can release v2.1 today, but I need release notes. Could you prepare it?

1reaction
Leszek-Kowalskicommented, Nov 22, 2017

Happy Thanksgiving! Actually we don`t celebrate it in Europe in Poland 😃 Do not rush. Thanksgiving is a time for family, not coding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Null serialization creates <Null /> node ignoring property ...
When serializing property with null value, the serializer creates node ignoring the property name. Example output:
Read more >
How to exclude null properties when using XmlSerializer
I want to exclude these null values because there will be several properties and this is getting stored in a database (yeah, thats...
Read more >
How to ignore properties with System.Text.Json
WhenWritingNull - The property is ignored on serialization if it's a reference type null , or a nullable value type null .
Read more >
Jackson not show null values. String}s, length () is called, * a
I would like to serialize a HashMap as a string through the Jackson JSON ... NON_NULL: Indicates that only properties with not null...
Read more >
JSON serialization: Ignore selective properties or null properties
This article is to explain how to ignore null values or some properties from being serialized to JSON objects for easy transfer of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found