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
Comments
The serializer needs a constructor to call in order to create the |
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) { }
}
}
} There is also problem with converting boolean values to object with new Json. |
Hmm. What does Newtonsoft pass as the |
Related https://github.com/dotnet/corefx/issues/40399. As a simplification v1 of System.Text.Json only supports parameterless constructors. |
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
This code throws exception. It works when I add parameterless constructor
The text was updated successfully, but these errors were encountered: