Skip to content

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

Closed
dotnet/corefx
#38061

Description

@pranavkm

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)

Activity

  1. ahsonkhan commented on May 9, 2019

    @ahsonkhan
    Contributor

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

  2. pranavkm commented on May 9, 2019

    @pranavkm
    ContributorAuthor

    @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.

  3. ahsonkhan commented on May 9, 2019

    @ahsonkhan
    Contributor

    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.

  4. vcsjones commented on May 9, 2019

    @vcsjones
    Member

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

  5. transferred this issue fromdotnet/corefxon Feb 1, 2020
  6. added this to the 3.0 milestone on Feb 1, 2020
  7. ghost locked as resolved and limited conversation to collaborators on Dec 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Text.Jsonenhancementhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions