-
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
JsonTypeInfo.CreateObject is null after custom converter is added to JsonSerializerOptions #94674
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionAs the title says, after one adds a custom Reproduction Stepsinternal class Program
{
public static void Main()
{
var options = JsonSerializerOptions.Default;
var typeInfoBeforeAdd = options.GetTypeInfo(typeof(SomeStructure));
//This works fine
var structure1 = typeInfoBeforeAdd.CreateObject();
var optionsContainingConverter = new JsonSerializerOptions(options);
optionsContainingConverter.Converters.Add(new SomeStructureConverter());
var typeInfoAfterAdd = optionsContainingConverter.GetTypeInfo(typeof(SomeStructure));
//This throws a NullReferenceException
var structure2 = typeInfoAfterAdd.CreateObject();
}
}
public record SomeStructure;
public class SomeStructureConverter : JsonConverter<SomeStructure>
{
public override SomeStructure? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => throw new NotImplementedException();
public override void Write(Utf8JsonWriter writer, SomeStructure value, JsonSerializerOptions options) => throw new NotImplementedException();
} Expected behaviorI would have expected, that I could still use the corresponding Actual behaviorThe property value is Also there is an exception when one wants to pass the obtained typeInfo instance (or the JsonSerializerOptions instance) to DefaultJsonObjectConverter.Read, but that's just a side note. Regression?No response Known WorkaroundsNo response ConfigurationI tested it locally with .NET 7, but tried the same code online in a preview build for .NET 8 with the same result. Other informationNo response
|
This is by design -- the contract customization APIs on |
@eiriktsarpalis Hm, okay I see. Is there then another way to modify the behavior of the built in converter? Our use case is, that there is an API we need to consume, that responds with an empty JSON object in case of a C# equivalent null value. I could not find a way to tell the public class EmptyObjectToNullConverter<T> : JsonConverter<T> {
...
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var lookaheadReader = reader;
lookaheadReader.Read();
if (lookaheadReader.TokenType == JsonTokenType.EndObject)
{
reader.Skip();
return default;
}
var defaultConverter = (JsonConverter<T>)JsonSerializerOptions.Default.GetConverter(typeof(T));
return defaultConverter.Read(ref reader, typeToConvert, options);
} But the call to
The error message is a bit misleading, because a missing constructor or attribute is not the cause of the issue, but the |
Only indirectly via contract configuration. See https://learn.microsoft.com/En-Us/dotnet/standard/serialization/system-text-json/custom-contracts for more details.
Yes, this is a known issue :( See #50205 for more details. |
Alright I see. So for now, the workaround is passing the |
That's right. The |
Description
As the title says, after one adds a custom
JsonConverter<T>
to aJsonSerializerOptions
instance, theJsonTypeInfo
instance returned foroptions.GetTypeInfo(typeof(T))
contains a null-Value for propertyCreateObject
.Before adding the converter, the property held a value (see repro code for details).
Reproduction Steps
Expected behavior
I would have expected, that I could still use the corresponding
JsonTypeInfo
instance to create an instance of the converted type.Actual behavior
The property value is
null
, access to it throws an exception (obviously).Also there is an exception when one wants to pass the obtained typeInfo instance (or the JsonSerializerOptions instance) to DefaultJsonObjectConverter.Read, but that's just a side note.
Regression?
No response
Known Workarounds
No response
Configuration
I tested it locally with .NET 7, but tried the same code online in a preview build for .NET 8 with the same result.
Other information
No response
The text was updated successfully, but these errors were encountered: