ऐसा करने के लिए कोई विशेषता नहीं है, लेकिन आप इसे रिज़ॉल्वर को अनुकूलित करके कर सकते हैं।
मैं देख रहा हूं कि आप पहले से ही एक का उपयोग कर रहे हैं 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"
}
}