C# 无法使用System.Text.Json反序列化日期时间、Guid或枚举
我提出了一个bug,但这似乎是一个基本的场景,我肯定错过了一些东西: 错误: 我无法使用System.Text.Json反序列化日期时间、Guid或枚举 这里有一个非常简单的XUnit测试,可以重新说明我期望它如何工作,但是失败了C# 无法使用System.Text.Json反序列化日期时间、Guid或枚举,c#,.net-core,system.text.json,C#,.net Core,System.text.json,我提出了一个bug,但这似乎是一个基本的场景,我肯定错过了一些东西: 错误: 我无法使用System.Text.Json反序列化日期时间、Guid或枚举 这里有一个非常简单的XUnit测试,可以重新说明我期望它如何工作,但是失败了 using System; using System.Text.Json; using Xunit; namespace Example.Tests { public class UnitTest1 { [Fact]
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;
使用Xunit;
名称空间示例
{
公共类UnitTest1
{
[事实]
public void GuidsAndDateTimesNotSerialized()
{
var post=新职位{
AuthorId=Guid.NewGuid(),
Created=DateTime.UtcNow,
Title=“测试标题”,
Path=“测试标题”,
PostStatus=PostStatus.Published,
Description=“这是一个测试”,
};
var json=JsonSerializer.Serialize(post);
var result=JsonSerializer.Deserialize(json);
Assert.Equal(post.Created、result.Created);
Assert.Equal(post.authord、result.authord);
Assert.Equal(post.PostStatus、result.PostStatus);
}
}
公营职位
{
公共Guid AuthorId{get;内部集合;}
公共字符串标题{get;set;}
公共字符串路径{get;set;}
公共字符串说明{get;set;}
公共PostStatus PostStatus{get;内部集合;}
已创建公共日期时间{get;internal set;}
}
公共枚举后状态
{
草案,
出版
}
}
使用System.Text.Json是否有我遗漏的地方?我猜您使用
internal
设置的属性无法被Json库/程序集访问。只需将它们更改为public
(删除内部访问修饰符)设置器,然后重试
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; }
}
我猜您用internal
设置的属性对于JSON库/程序集是不可访问的。只需将它们更改为public
(删除内部访问修饰符)设置器,然后重试
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; }
}
经过测试,它确实是属性集的内部修饰符。谢谢,现在看来很明显P我通常不会在我的模型上设置内部设置,但我忘了我做了。我试图为我的POST和GET API使用相同的模型,而内部API在创建我的swagger文档时忽略了内部集合。现在回到绘图板上,如果我想在Api和Blazor项目之间共享这些模型:/Tested,它确实是属性集的内部修饰符。谢谢,现在看来很明显P我通常不会在我的模型上设置内部设置,但我忘了我做了。我试图为我的POST和GET API使用相同的模型,而内部API在创建我的swagger文档时忽略了内部集合。现在,如果我想在Api和Blazor项目之间共享这些模型,或许可以回到绘图板上:/