-
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
NullReferenceException when composing a custom converter with a default converter #57280
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsRunning the console app using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Repro
{
public class Program
{
public static void Main()
{
var options = new JsonSerializerOptions { Converters = { new MyCustomConverter() } };
var value = new Dictionary<int, string>();
JsonSerializer.Serialize(value, options);
}
public class MyCustomConverter : JsonConverter<Dictionary<int, string>>
{
// Need to customize deserialization only; delegating serialization to the default converter of the same type.
private readonly JsonConverter<Dictionary<int, string>> _defaultConverter =
(JsonConverter<Dictionary<int, string>>)new JsonSerializerOptions().GetConverter(typeof(Dictionary<int, string>));
public override void Write(Utf8JsonWriter writer, Dictionary<int, string> value, JsonSerializerOptions options)
=> _defaultConverter.Write(writer, value, options);
public override Dictionary<int, string>? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> throw new NotImplementedException("custom converter logic goes here");
}
}
} Results in the following exception:
Root cause is the bridging logic for resumable converters, which populates the root stackframe using metadata from the converter resolved via the ambient Composing with converters that are not part of the current This is not a regression from .NET 5. @steveharter @layomia is this something we should attempt to address in .NET 6?
|
Should likely be addressed in conjunction with #63791. |
Moving to future, as this will likely be addressed via #63795 |
Running the console app
Results in the following exception:
Exception is thrown from this location. Root cause is the bridging logic for resumable converters, which populates the root stackframe using metadata from the converter resolved via the ambient
JsonSerializerOptions
instance, rather than the one being currently called.Composing with converters that are not part of the current
JsonSerializerOptions
instance should be a supported scenario (I anticipate this might become a more frequent scenario as users start availing of the newJsonMetadataServices
APIs). I could not come up with a good workaround for the above.This is not a regression from .NET 5.
@steveharter @layomia is this something we should attempt to address in .NET 6?
The text was updated successfully, but these errors were encountered: