Keep casing when serializing dictionaries

后端 未结 4 1982
故里飘歌
故里飘歌 2020-11-28 08:07

I have a Web Api project being configured like this:

config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContract         


        
4条回答
  •  眼角桃花
    2020-11-28 09:08

    The selected answer is perfect but I guess by the time I'm typing this, the contract resolver must change to something like this because DictionaryKeyResolver doesn't exists anymore :)

    public class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
        {
            protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
            {
                JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
                contract.PropertyNameResolver = propertyName => propertyName;
                return contract;
            }
        }
    

提交回复
热议问题