-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
STJ: Using default system converter does not work when writing and provided options #111429
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
I believe this is a duplicate of #50205 |
Hi @eiriktsarpalis I think the underlying cause is the same, although dunno if that is strictly duplicate.
The #50205 was reported quite a while ago... any insides on when the underlying issue could be fixed? |
It's a different symptom of the same underlying bug, namely that certain built-in converters cannot function independently of the
It's not something that is currently being prioritized, unfortunately. Please upvote that original issue so that it's most likely to be included in future planning iterations. |
Description
According to the documentation , to get the default converter we can just use
JsonSerializerOptions.Default.GetConverter(typeof(T))
.That works... kinda.
It will return the default converter, but the problem is if that converter derives from
JsonResumableConverter
then writing would not work.While writing, we will try to get
JsonTypeInfo
-runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonResumableConverterOfT.cs
Line 43 in 30834e4
There is small issue:
We will try to do that, from the
options
, which could have converter for the type T. In most cases that's perfectly fine, and desirable, but we just got the converter from the "Default" options... so I'm not expecting to have the converters there.The problem is, if we will do:
There is no converter for
Foo
, so default would beLargeObjectWithParameterizedConstructorConverter
(orSmallObjectWithParameterizedConstructorConverter
) which will haveruntime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/JsonObjectConverter.cs
Line 12 in 30834e4
And so,
JsonTypeInfo
will be populated with theProperties
But when we do:
Now, there is our converter defined... which derived from standard
JsonConverter<T>
which:runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs
Line 53 in 30834e4
So, the
JsonTypeInfo
will not have anyProperties
defined.So long story short:
even if we got the converter from the default options, while doing write, we will try to get list of properties from the current options, which would result in having empty list.
Reproduction Steps
Expected behavior
Regardless if we will define
or not, the output should be the same:
Actual behavior
Current output is broken:
Regression?
No.
In previous versions (<net9) that code would throw
System.NullReferenceException
inOnTryWrite
But the reason was the same -
Properties
was empty (to be exactPropertyCache
was null)Known Workarounds
None.
Configuration
NET9
Other information
This issue arise when we had to create a custom converter to handle "Read" in a custom manner, but we wanted to keep "Write" as a default behavior.
But we need to keep/pass the
options
as there are many other types that have to handled differently.The text was updated successfully, but these errors were encountered: