Null serialization creates <Null /> node ignoring property name instead of using xsi:nil="true"
See original GitHub issueWhen 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:
- Created 6 years ago
- Comments:20 (14 by maintainers)
Top GitHub Comments
@Mike-EEE We can release v2.1 today, but I need release notes. Could you prepare it?
Happy Thanksgiving! Actually we don`t celebrate it in Europe in Poland 😃 Do not rush. Thanksgiving is a time for family, not coding.