System.Text.Json deserializes all properties to null
I have a class that looks like this
public class ApartmentCreatedEvent
{
protected ApartmentCreatedEvent() { }
public ApartmentCreatedEvent(string creatorId, string name, DateTimeOffset dateTime)
{
Name = name;
CreatorId = creatorId;
DateTime = dateTime;
}
public string CreatorId { get; }
public DateTimeOffset DateTime { get; }
public string Name { get; }
}
I had to create a parameterless constructor because I was getting an exception that it couldn't deserialize without it
Running this code:
var event = new ApartmentCreatedEvent("creatorId", "name", DateTimeOffset.UtcNow);
var ser = JsonSerializer.Serialize(event );
var des = JsonSerializer.Deserialize(ser, typeof(ApartmentCreatedEvent));
gives me back an object with all the properties set to default (null, and DateTimeOffset.MinValue)
The first version of S.T.Json only supports public setters. In other worlds, the data object must lose read/write control for properties. There are abstractions to support other type of accesses to member, but not implemented yet.
This is happening to me too in 3.0.100, and I'm using POCOs that perfectly match JSON strings. No explicit constructor, just {get;set;} properties. Json.NET handled them fine. When inspected in the debugger, I see that the loaded strings contain escaped whitespace characters, such as tabs. What bothers me the most is that no error is returned, it just creates the target object and doesn't populate its properties silently. I believe mine is the most basic use-case possible. How on Earth is that not working?
Edit: the issue is that this new library is case-sensitive by default. "title" in JSON is not mapped to "Title" in the object.
@huoyaoyuan is correct; constructor-based deserialization is not supported in 3.0. Closing as duplicate of dotnet/runtime#30615.
@Corys9 please open a new issue with a small sample to reproduce the problem.