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

Why System.Text.Json requires that class explicitly have parameterless constructor #31248

Closed
neman opened this issue Oct 21, 2019 · 5 comments
Closed

Comments

@neman
Copy link

neman commented Oct 21, 2019

I have a class containing one constructor with some parameters.
Why do I also have to explicitly declare default constructor?

I'm already using both constructors throughout the code, and System.Text.Json does not serialize if I do not explicitly write default constructor. Newtonsoft json did not required this intervention. Now I have to revert back to old code.

Example of code which does not work

string test = "{\"First\": {\"Name\": \"Nemanja\"}}";
var value = JsonSerializer.Deserialize<Dictionary<string, Person>>(test);

public class Person
{
   public string Name { get; set; }
   public Person(int x)   { }
}

This code throws exception. It works when I add parameterless constructor

@khellang
Copy link
Member

The serializer needs a constructor to call in order to create the Person object. Does the code you showed even work with Newtonsoft.Json? I thought it matched constructor argument names with JSON property names? Anyway, the feature you're talking about is probably already covered by either https://github.com/dotnet/corefx/issues/40399 or https://github.com/dotnet/corefx/issues/38569.

@neman
Copy link
Author

neman commented Oct 22, 2019

The same code works well with Newtonsoft.Json

using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            string test = "{\"First\": {\"Name\": \"Nemanja\"}}";
            var worked = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, Person>>(test);
            var failed = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, Person>>(test);
        }

        private class Person
        {
            public string Name { get; set; }

            public Person(int x) { }
        }
    }
}

image

There is also problem with converting boolean values to object with new Json.
I regret the day I started to migrate code from Newtonsoft.Json, who now what else will break my code. I'll stick with Newtonsoft.Json for now, although it's less performant, at least it works. I do not need premature optimization at this moment.

@khellang
Copy link
Member

Hmm. What does Newtonsoft pass as the x argument in that Person constructor? Or does it just not call a constructor at all?

@neman
Copy link
Author

neman commented Oct 22, 2019

It passes default for int, 0

image

@ericstj
Copy link
Member

ericstj commented Nov 4, 2019

Related https://github.com/dotnet/corefx/issues/40399. As a simplification v1 of System.Text.Json only supports parameterless constructors.

@ericstj ericstj closed this as completed Nov 4, 2019
@msftgits msftgits transferred this issue from dotnet/corefx Feb 1, 2020
@msftgits msftgits added this to the 5.0 milestone Feb 1, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants