gpt4 book ai didi

c# - JsonSerializerSettings 更改属性名称的大小写但不更改属性的属性名称

转载 作者:太空宇宙 更新时间:2023-11-03 18:57:16 25 4
gpt4 key购买 nike

我正在为我的类属性的驼峰式外壳使用以下设置。

JsonSerializerSettings settings = new JsonSerializerSettings()
{
ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()
};

对象中的某些属性属于其他类类型。我不想更改这些属性的大小写。

例如:

Public Class CaseToChange
{
public string StringProperty{get;set;} //Change to camelCase
public SomeOtherType OtherTypeProperty{get;set;} //Change name of this to camelCase but not property name of "SomeOtherType"

}

如何使用 JsonSerializerSettings 实现这一点?

最佳答案

如果你可以修改你的类型来添加Json.NET serialization attributes ,最简单的做法是添加 [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]到你的 CaseToChange 类型:

[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class CaseToChange
{
public string StringProperty { get; set; } //Change to camelCase
public SomeOtherType OtherTypeProperty { get; set; } //Change name of this to camelCase but not property name of "SomeOtherType"
}

(或者,如果您使用的 Json.NET 版本早于 9.0.1,请将 [JsonProperty("camelCaseName")] 添加到每个属性,如 this answer 中所示。)

如果您不能修改类型并且必须仅通过序列化程序设置对 CaseToChange 的属性进行驼峰式大小写,则可以创建一个 custom contract resolver返回 CaseToChange 的驼峰命名和其他类型的未修改名称。以下是诀窍:

public class OverrideContractResolver : ContractResolverDecorator
{
readonly Dictionary<Type, IContractResolver> overrides;

public OverrideContractResolver(IEnumerable<KeyValuePair<Type, IContractResolver>> overrides, IContractResolver baseResolver)
: base(baseResolver)
{
if (overrides == null)
throw new ArgumentNullException();
this.overrides = overrides.ToDictionary(p => p.Key, p => p.Value);
}

public override JsonContract ResolveContract(Type type)
{
IContractResolver resolver;
if (overrides.TryGetValue(type, out resolver))
return resolver.ResolveContract(type);
return base.ResolveContract(type);
}
}

public class ContractResolverDecorator : IContractResolver
{
readonly IContractResolver baseResolver;

public ContractResolverDecorator(IContractResolver baseResolver)
{
if (baseResolver == null)
throw new ArgumentNullException();
this.baseResolver = baseResolver;
}

#region IContractResolver Members

public virtual JsonContract ResolveContract(Type type)
{
return baseResolver.ResolveContract(type);
}

#endregion
}

然后序列化设置如下:

var settings = new JsonSerializerSettings
{
ContractResolver =
new OverrideContractResolver(
new Dictionary<Type, IContractResolver> { { typeof(CaseToChange), new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() } },
new Newtonsoft.Json.Serialization.DefaultContractResolver()),
};

生成的 JSON 如下所示:

{
"stringProperty": "string property",
"otherTypeProperty": {
"FooProperty": "foo",
"BarProperty": 101
}
}

样本 fiddle .

为了获得最佳性能,您可能需要 cache instances of the contract resolver .

关于c# - JsonSerializerSettings 更改属性名称的大小写但不更改属性的属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42092359/

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