无法使用 System.Text.Json 反序列化 DateTime、Guid 或 Enum
Unable to Deserialize DateTime, Guid, or Enum with System.Text.Json
我提交了一个错误,但这似乎是一个基本的基本场景,我一定遗漏了一些东西:
错误:https://github.com/dotnet/runtime/issues/48202
我无法使用 System.Text.Json 反序列化 DateTime、Guid 或 Enum。
这是一个非常简单的 XUnit 测试,用于重现我期望它如何工作,但失败了。
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
}
}
使用 System.Text.Json 有什么我遗漏的吗?
我的猜测是 internal
JSON library/assembly 无法访问您使用 internal
设置的属性。只需将它们更改为 public
(删除 internal
访问修饰符)设置器并重试。
JSON DTO 应该很简单 类 并且总是有一个默认构造函数:
public class Post
{
public Guid AuthorId { get; set; }
public string Title { get; set; }
public string Path { get; set; }
public string Description { get; set; }
public PostStatus PostStatus { get; set; }
public DateTime Created { get; set; }
}
我提交了一个错误,但这似乎是一个基本的基本场景,我一定遗漏了一些东西: 错误:https://github.com/dotnet/runtime/issues/48202
我无法使用 System.Text.Json 反序列化 DateTime、Guid 或 Enum。
这是一个非常简单的 XUnit 测试,用于重现我期望它如何工作,但失败了。
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
}
}
使用 System.Text.Json 有什么我遗漏的吗?
我的猜测是 internal
JSON library/assembly 无法访问您使用 internal
设置的属性。只需将它们更改为 public
(删除 internal
访问修饰符)设置器并重试。
JSON DTO 应该很简单 类 并且总是有一个默认构造函数:
public class Post
{
public Guid AuthorId { get; set; }
public string Title { get; set; }
public string Path { get; set; }
public string Description { get; set; }
public PostStatus PostStatus { get; set; }
public DateTime Created { get; set; }
}