检索Elasticsearch文档时使用JsonSerializer时出现串行化问题



我目前正在尝试检索Elasticsearch文档,并将json响应反序列化为特定类型。

这是通用文档类

public class Document<T> where T : class
{
public int _Seq_No { get; set; }
public int _Primary_Term { get; set; }
public T _Source { get; set; }
}

我有一个订单类,代表T.

public class Order
{
public int OrderId { get; set; }
public DateTime OrderDate {get; set;}
//..other fields omitted...
}

我有以下方法从Elasticsearch获取文档。

public async Task<Document<T>> LoadDocument<T>(string orderId, CancellationToken 
cancellationToken = default) where T : class
{
var jsonOptions = new JsonSerializerOptions {PropertyNameCaseInsensitive = true};
//.....code to get the document (as json) from the service omitted ...
//deserialize
var document = JsonSerializer.Deserialize<Document<T>>(jsonDocument, jsonOptions);
return document;
}

我试过这样称呼它:

var document = await _service.LoadDocument<Order>(orderId)

当我调试代码时,我可以看到_Seq_No_Primary Term属性设置了我期望的实际值,但_Source属性(在本例中为Order(订单上的属性默认为其默认值,而不是实际值。

json文档/响应中的_source属性已经填充,所以我不确定为什么取消序列化不适用于这个特定的部分。

我需要用[JsonProperty(...)]装饰我的Order班的成员吗?

如果有人能帮我确定我在去婚姻化方面错过了什么,我将不胜感激。

提前谢谢。

最后我设法解决了双重嵌套问题。我多次重新构建了我的解决方案。我当地的发展环境有些不正确。

我还用[JsonProperty(...)]修饰了Order类中的属性,并适当地设置了值。

最新更新