Click or drag to resize
Json.NETConfigure NamingStrategy dictionary serialiation
 

This sample configures a CamelCaseNamingStrategy to not camel case dictionary keys.

Sample
Types
1public class DailyHighScores
2{
3    public DateTime Date { get; set; }
4    public string Game { get; set; }
5    public Dictionary<string, int> UserPoints { get; set; }
6}
Usage
 1DailyHighScores dailyHighScores = new DailyHighScores
 2{
 3    Date = new DateTime(2016, 6, 27, 0, 0, 0, DateTimeKind.Utc),
 4    Game = "Donkey Kong",
 5    UserPoints = new Dictionary<string, int>
 6    {
 7        ["JamesNK"] = 9001,
 8        ["JoC"] = 1337,
 9        ["JessicN"] = 1000
10    }
11};
12
13DefaultContractResolver contractResolver = new DefaultContractResolver
14{
15    NamingStrategy = new CamelCaseNamingStrategy
16    {
17        ProcessDictionaryKeys = false
18    }
19};
20
21string json = JsonConvert.SerializeObject(dailyHighScores, new JsonSerializerSettings
22{
23    ContractResolver = contractResolver,
24    Formatting = Formatting.Indented
25});
26
27Console.WriteLine(json);
28// {
29//   "date": "2016-06-27T00:00:00Z",
30//   "game": "Donkey Kong",
31//   "userPoints": {
32//     "JamesNK": 9001,
33//     "JoC": 1337,
34//     "JessicN": 1000
35//   }
36// }