-
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
NullRefereceException when using a JsonConverter without a parameterless constructor #399
Comments
I thought, does this could be fixed, by using another constructor, to which would be given, parameters, which all, are null? |
@piotrsz1234 It's perfectly fine that the |
@hillin Right. |
Here's a repro of the issue. The null ref only occurs when the converter is used within the [Fact]
public static void ConverterWithoutDefaultCtor()
{
string json = @"{""MyType"":""ABC""}";
Foo obj = JsonSerializer.Deserialize<Foo>(json);
Assert.Null(obj.MyType);
}
[JsonConverter(typeof(MyConverter))]
public class Foo
{
public string MyType { get; set; }
}
internal class MyConverter : JsonConverter<Foo>
{
public MyConverter(int x)
{
}
public override Foo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, Foo value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
Ideally, such issues will get fixed by annotating nulls: #114 (comment) |
@piotrsz1234 are you working on this? |
But it isn't hard fix. I guess some work with finding class using Assembly. |
I think that the fix is simply throw a clear exception right @ahsonkhan? |
Yes, I believe this is being fixed here: See @buyaa-n's comment: #528 (comment) |
I see |
Fixed in #528. |
https://github.com/dotnet/corefx/blob/dfe3ab2f0205436e23b93c369ee0b2e41a4973e0/src/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializerOptions.Converters.cs#L208-L209
ctor
will benull
iftype
does not have a parameterless constructor.The text was updated successfully, but these errors were encountered: