I filed a bug, but this seems like such a basic fundamental scenario that I must be missing something: Bug: https://github.com/dotnet/runtime/issues/48202
I am unable to Deserialize DateTime, Guid, or Enum with System.Text.Json.
Here is a very minimal XUnit test to repro how I would expect it to work, but is failing.
using System;
using System.Text.Json;
using Xunit;
namespace Example.Tests
{
public class UnitTest1
{
[Fact]
public void GuidsAndDateTimesNotSerialized()
{
var post = new Post {
AuthorId = Guid.NewGuid(),
Created = DateTime.UtcNow,
Title = "test title",
Path = "test-title",
PostStatus = PostStatus.Published,
Description = "this is a test",
};
var json = JsonSerializer.Serialize(post);
var result = JsonSerializer.Deserialize<Post>(json);
Assert.Equal(post.Created, result.Created);
Assert.Equal(post.AuthorId, result.AuthorId);
Assert.Equal(post.PostStatus, result.PostStatus);
}
}
public class Post
{
public Guid AuthorId { get; internal set; }
public string Title { get; set; }
public string Path { get; set; }
public string Description { get; set; }
public PostStatus PostStatus { get; internal set; }
public DateTime Created { get; internal set; }
}
public enum PostStatus
{
Draft,
Published
}
}
Is there something with using System.Text.Json that I am missing?