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

[System.Text.Json] Deserializing object without parameterless ctor results in null-ref #29490

Closed
pranavkm opened this issue May 9, 2019 · 4 comments · Fixed by dotnet/corefx#38061
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@pranavkm
Copy link
Contributor

pranavkm commented May 9, 2019

I'm not sure if there are plans to support objects without parameterless ctors. However, attempting to deserialize an object without one results in a null ref rather than a meaningful error:

class Program
{
    static void Main(string[] args)
    {
        var instance = new NoParameterlessCtor(10);
        var json = JsonSerializer.ToString(instance);

        Console.WriteLine(JsonSerializer.Parse<NoParameterlessCtor>(json));
    }
}

class NoParameterlessCtor
{
    public NoParameterlessCtor(int id)
    {
        Id = id;
    }

    public int Id { get; set; }
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Text.Json.Serialization.JsonSerializer.HandleStartObject(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& state)
@ahsonkhan
Copy link
Member

I don't think custom ctors is supported atm. cc @steveharter

@pranavkm
Copy link
Contributor Author

pranavkm commented May 9, 2019

@ahsonkhan could we improve the error message for the current release? Seeing a null ref doesn't help the user understand that the scenario isn't supported.

@ahsonkhan
Copy link
Member

ahsonkhan commented May 9, 2019

could we improve the error message for the current release

Agreed. I am sure we can improve the error messaging for non-supported scenarios, overall. I'll let @steveharter chime-in regarding that.

@vcsjones
Copy link
Member

vcsjones commented May 9, 2019

Wrote dotnet/corefx#37545 to track improving error messaging.

@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 3.0 milestone Feb 1, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants