home icon indicating copy to clipboard operation
home copied to clipboard

DateOnly type not serialized

Open moshekar opened this issue 1 year ago • 7 comments
trafficstars

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.

moshekar avatar Jan 22 '24 21:01 moshekar

Branch issues/other/i608 created!

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.

Mike-E-angelo avatar Jan 23 '24 10:01 Mike-E-angelo

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:

https://github.com/ExtendedXmlSerializer/home/blob/ed2abb115fcff12c9187ad1bd798ae239da1bd4c/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue608Tests.cs#L19

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

Mike-E-angelo avatar Jan 23 '24 10:01 Mike-E-angelo

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 ...

Mike-E-angelo avatar Jan 23 '24 10:01 Mike-E-angelo

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. 👍

Mike-E-angelo avatar Jan 23 '24 10:01 Mike-E-angelo

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:

https://github.com/ExtendedXmlSerializer/home/blob/ed2abb115fcff12c9187ad1bd798ae239da1bd4c/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue608Tests.cs#L19

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!

moshekar avatar Jan 23 '24 19:01 moshekar

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

Mike-E-angelo avatar Jan 24 '24 09:01 Mike-E-angelo

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.

Mike-E-angelo avatar Mar 23 '24 08:03 Mike-E-angelo