이를 수행하는 속성은 없지만 리졸버를 사용자 정의하여 수행 할 수 있습니다.
이미 CamelCasePropertyNamesContractResolver. 여기에서 새 리졸버 클래스를 파생하고 CreateDictionaryContract()메서드를 재정의 DictionaryKeyResolver하면 키 이름을 변경하지 않는 대체 함수를 제공 할 수 있습니다 .
다음은 필요한 코드입니다.
class CamelCaseExceptDictionaryKeysResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonDictionaryContract CreateDictionaryContract(Type objectType)
{
JsonDictionaryContract contract = base.CreateDictionaryContract(objectType);
contract.DictionaryKeyResolver = propertyName => propertyName;
return contract;
}
}
데모:
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo
{
AnIntegerProperty = 42,
HTMLString = "<html></html>",
Dictionary = new Dictionary<string, string>
{
{ "WHIZbang", "1" },
{ "FOO", "2" },
{ "Bar", "3" },
}
};
JsonSerializerSettings settings = new JsonSerializerSettings
{
ContractResolver = new CamelCaseExceptDictionaryKeysResolver(),
Formatting = Formatting.Indented
};
string json = JsonConvert.SerializeObject(foo, settings);
Console.WriteLine(json);
}
}
class Foo
{
public int AnIntegerProperty { get; set; }
public string HTMLString { get; set; }
public Dictionary<string, string> Dictionary { get; set; }
}
다음은 위의 출력입니다. 모든 클래스 속성 이름은 카멜 케이스이지만 사전 키는 원래 케이스를 유지합니다.
{
"anIntegerProperty": 42,
"htmlString": "<html></html>",
"dictionary": {
"WHIZbang": "1",
"FOO": "2",
"Bar": "3"
}
}