ExtendedXmlSerializer / home

A configurable and eXtensible Xml serializer for .NET.

Home Page:https://extendedxmlserializer.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DateOnly type not serialized

moshekar opened this issue Β· comments

Hi,

First, thank you for this amazing project.

I found that the DateOnly type do not serialize and produces an empy tag:

public class TestClass
{
	public DateOnly DateOfBirth { get; set; }
}

TestClass test = new()
{
	DateOfBirth = DateOnly.Parse("2024-01-22")
};

IExtendedXmlSerializer serializer = new ConfigurationContainer()
	.UseAutoFormatting()
	.UseOptimizedNamespaces()
	.Create();

serializer.Serialize(new XmlWriterSettings { Indent = false }, test);

Produces:
<?xml version="1.0" encoding="utf-8"?><TestClass xmlns="clr-namespace:..."><DateOfBirth /></TestClass>

How to fix this?

Thanks.

Hey there @moshekar thank you for the kind words and for writing in. This is a good point, we do not currently support DateOnly types as they are newer. I will see if I can get this in there for you.

Well this is a pickle. It appears that DateOnly is supported in .NET6+ and not netstandard2.0 which is what we use to be compatible with .NET Framework. We do not have an easy way to account for this at the moment. The best recommendation I can provide is to use an extension which I have provided for you here:

Please let me know how that looks to you and/or any further questions around this and I will further assist. πŸ‘

Nevermind, I feel silly now! This is possible w/ .NET Standard after all and only thought to search after posting the above πŸ˜…

https://stackoverflow.com/a/70961343

I will see if I can get that incorporated using the converter from above.

Stack Overflow
I't doesn't matter what code I post because .NET 6 has TimeOnly and DateOnly. Is there a way I can use it in .NET Framework 4.8? Can I add a reference somewhere or is it not possible to be able to ...

Ah I spoke too soon, this library is not official and I am hesitant to add a new dependency at this stage if it's not directly from Microsoft. However, if there are others that ask for this going forward (and some time passes) I will consider this. I feel having an extension as the above for .NET6+ scenarios is the best path for now. I am open to further dialogue around this. πŸ‘

Well this is a pickle. It appears that DateOnly is supported in .NET6+ and not netstandard2.0 which is what we use to be compatible with .NET Framework. We do not have an easy way to account for this at the moment. The best recommendation I can provide is to use an extension which I have provided for you here:

Please let me know how that looks to you and/or any further questions around this and I will further assist. πŸ‘

Thank you very much @Mike-E-angelo .
Your extension is working well and solved the problem for me. I'll use it for now, unless you find a way to solve it permanently in the project.

If anyone needs, I created a short extension class to use with your solution easily:

public static class ExtendedXmlSerializerExtensions
{
	public static IConfigurationContainer AddDateOnlySupport(
		this IConfigurationContainer container)
	{
		return container
			.Type<DateOnly>()
			.Register()
			.Converter()
			.Using(DateOnlyConverter.Default);
	}
}

Then just add to the serializer:

IExtendedXmlSerializer serializer = new ConfigurationContainer()
	...
	.AddDateOnlySupport()
	.Create();

Thanks again!

Sure thing @moshekar happy to get a win here :)

Sounds like we resolved this issue as best we could considering the circumstances. As we cannot viably add DateOnly support to .NET Standard 2.0 projects that limits how we can proceed. If someone has any better approaches (preferably a PR 😊) I can further explore this. Closing for now.