gpt4 book ai didi

c# - JSON.NET 部分更新 Rest API 客户端

转载 作者:行者123 更新时间:2023-11-30 17:48:40 25 4
gpt4 key购买 nike

我正在使用 JSON.NET 为 REST API 构建 C#/.NET 4.5 客户端。 API支持部分更新;因此,更新时 json 中是否存在某个属性是有意义的。如果属性在 json 中,服务器将相应地设置值;该属性未传递服务器不会更新它。这也适用于空值。每个模型都有 .NET 类;具有每个 JSON 属性的属性(非常标准)。

举个例子,假设我有一个已经存在于服务器上的帐户对象(名称、注释):

{
'name':'craig',
'notes:'these are notes'
}

如果我传入此 json 进行更新,它将更新名称,但会将注释设置为“这些是注释”:

var account = api.GetAccount();
account.Name = "bob";
api.UpdateAccount(account);

{
'name':'bob'
}

如果我传递这个 json 进行更新,它会在服务器上将名称和注释设置为空:

var account = api.GetAccount();
account.Name = "bob";
account.Notes = null;
api.UpdateAccount(account);

{
'name':'bob',
'notes':null
}

到目前为止一切都很好。

我的问题是如何让 JSON.NET 与此配合得很好。 JSON.NET 允许控制 NullValueHandling,它基本上说明是否应该序列化 null 值。然而,在这种情况下这还不够。我需要能够确定调用代码是否显式地将值设置为 null。有推荐的方法来处理这个吗?

我试过使用模型内部的字典来存储要通过 JSON 序列化的属性。这使我可以通过字典中是否存在键来判断属性是否已设置为任何值(包括 null)。我发现这种方法有一些困难,我最终重写了很多 JSON.NET 标准代码(类型序列化、泛型、可空值、枚举...)。

注意:我确实意识到上面的例子有点做作。实际上,从服务器返回的帐户对象会同时填充姓名和注释,并且当更新发生时,它会发回这两个对象。

另一种适用的情况是在创建对象和处理服务器生成的默认值期间。例如,假设服务器在创建帐户时将帐户的注释默认为“在此处放置注释”。如果我传入空值的 Notes 属性,服务器会认为客户端要将其设置为空。但实际情况是,客户端并没有尝试将 Notes 设置为空,在这种情况下会希望设置默认值。

var account = new Account();
account.Name = "bob";
api.CreateAccount(account);

{
'name':'bob',
'notes':null
}

最佳答案

我总是对 JSON.NET 印象深刻...

这是我最终得到的。我结合使用了 ContractResolver、ShouldSerialize 谓词和 NullValueHandling 属性。这link非常有用。这些属性存储在基类 ApiModel 的字典中;该代码很简单。

账户模型

[JsonProperty("name")]
public string Name
{
get { return this.GetAttributeValue<string>("name"); }
set { this.SetAttributeValue<string>("name", value); }
}

Json序列化

ApiModel.JsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
ApiModel.JsonSerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Include;
ApiModel.JsonSerializerSettings.ContractResolver = ApiModel.JsonContractResolver;

internal class ApiModelContractResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override Newtonsoft.Json.Serialization.JsonProperty CreateProperty(System.Reflection.MemberInfo member, Newtonsoft.Json.MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);

property.ShouldSerialize =
instance =>
{
var apiModel = instance as ApiModel;

var hasAttribute = apiModel.HasAttribute(property.PropertyName);

property.NullValueHandling = hasAttribute ? Newtonsoft.Json.NullValueHandling.Include : Newtonsoft.Json.NullValueHandling.Ignore;

return hasAttribute;
};

return property;
}
}

关于c# - JSON.NET 部分更新 Rest API 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22596472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com