JsonSerializerSettings 更改属性名称的大小写,但不更改属性的名称

Posted

技术标签:

【中文标题】JsonSerializerSettings 更改属性名称的大小写,但不更改属性的名称【英文标题】:JsonSerializerSettings to change case of property name but not name of property's property 【发布时间】:2017-06-24 20:17:19 【问题描述】:

我将以下设置用于我的类属性的驼峰式外壳。

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

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

例如:

Public Class CaseToChange

    public string StringPropertyget;set; //Change to camelCase
    public SomeOtherType OtherTypePropertyget;set; //Change name of this to camelCase but not property name of "SomeOtherType"


如何使用 JsonSerializerSettings 实现这一点?

【问题讨论】:

JsonSerializerSettings 将默认设置应用于所有被序列化的类型。如果你只是想把CaseToChange的大小写改成驼峰式,你可以申请[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] 您是否需要在设置中,或者您可以修改类型吗? 【参考方案1】:

如果您可以修改您的类型以添加​​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"

(或者,如果您使用的是早于 9.0.1 的 Json.NET 版本,请将 [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。

【讨论】:

【参考方案2】:

只需添加如下所示的 JsonProperty 属性,如果您不希望整个对象树使用驼峰式命名,请不要使用 CamelCasePropertyNamesContractResolver

public class CaseToChange

    [JsonProperty("stringProperty")]
    public string StringProperty  get; set;  //Change to camelCase

    [JsonProperty("otherTypeProperty")]
    public SomeOtherType OtherTypeProperty  get; set;  //Change name of this to camelCase but not property name of "SomeOtherType"

【讨论】:

以上是关于JsonSerializerSettings 更改属性名称的大小写,但不更改属性的名称的主要内容,如果未能解决你的问题,请参考以下文章

IsoDateTimeConverter 和 JsonSerializerSettings:区分 DateTime 和 DateTimeOffset

C# Newtonsoft.Json JsonSerializerSettings配置

.Net C# Newtonsoft.Json JsonSerializerSettings配置

如何在 ASP.NET Web API 中为 Json.NET 设置自定义 JsonSerializerSettings?

webapi 中的错误 - 无法从“System.Web.Mvc.JsonRequestBehavior”转换为“Newtonsoft.Json.JsonSerializerSettings”

在应用级别指定 NumberFormat