Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateOnly type not serialized #608

Closed
moshekar opened this issue Jan 22, 2024 · 8 comments
Closed

DateOnly type not serialized #608

moshekar opened this issue Jan 22, 2024 · 8 comments
Assignees
Labels
enhancement (cc: feat) New feature or request

Comments

@moshekar
Copy link

moshekar commented Jan 22, 2024

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.

@Mike-E-angelo Mike-E-angelo self-assigned this Jan 23, 2024
Copy link

Branch issues/other/i608 created!

@Mike-E-angelo Mike-E-angelo added question Further information is requested enhancement (cc: feat) New feature or request and removed question Further information is requested labels Jan 23, 2024
@Mike-E-angelo
Copy link
Member

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
Copy link
Member

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

@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Jan 23, 2024

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
Copy link
Member

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

@moshekar
Copy link
Author

moshekar commented Jan 23, 2024

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!

@Mike-E-angelo
Copy link
Member

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

@Mike-E-angelo
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement (cc: feat) New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants