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

SerializerOptions.Default doesn't work? #380

Open
markjerz opened this issue Oct 6, 2023 · 0 comments
Open

SerializerOptions.Default doesn't work? #380

markjerz opened this issue Oct 6, 2023 · 0 comments

Comments

@markjerz
Copy link
Contributor

markjerz commented Oct 6, 2023

Version Information
Hyperion 0.12.2

Describe the bug
Using SerializerOptions.Default doesn't work if that's how you create Serializers.

I almost can't believe this to be the case (or I'm losing the plot) because I would have thought others would have hit this straight away as well.

In C#/.Net static variables are instantiated in the order they appear in the source code. (See https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors#remarks) So, as the Default static instance is at the top of SerializerOptions it gets called first.

image

This means that when that constructor is called all the static references are currently null. For example, DefaultValueSerializerFactories is null at the point the constructor is run, resulting in SerializerOptions.ValueSerializerFactories also being null.

This then results in a null ref exception in Serializer.GetSerializerByType when the code attempts to iterate through the value serializer factories during a call to Serialize()

image

It seems like you need to move the Default field down to the bottom of the source code? Or, use a static constructor to explicitly control instantiation?

To Reproduce

var serializer = new Serializer(SerializerOptions.Default);
using var memoryStream = new MemoryStream();
serializer.Serialize(obj, memoryStream);

Expected behavior
The object is serialized

Actual behavior
Null reference exception

Environment
.Net 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant