Click or drag to resize
Json.NET

Configure NamingStrategy dictionary serialization

 

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

Sample
Types
public class DailyHighScores
{
    public DateTime Date { get; set; }
    public string Game { get; set; }
    public Dictionary<string, int> UserPoints { get; set; }
}
Usage
DailyHighScores dailyHighScores = new DailyHighScores
{
    Date = new DateTime(2016, 6, 27, 0, 0, 0, DateTimeKind.Utc),
    Game = "Donkey Kong",
    UserPoints = new Dictionary<string, int>
    {
        ["JamesNK"] = 9001,
        ["JoC"] = 1337,
        ["JessicN"] = 1000
    }
};

DefaultContractResolver contractResolver = new DefaultContractResolver
{
    NamingStrategy = new CamelCaseNamingStrategy
    {
        ProcessDictionaryKeys = false
    }
};

string json = JsonConvert.SerializeObject(dailyHighScores, new JsonSerializerSettings
{
    ContractResolver = contractResolver,
    Formatting = Formatting.Indented
});

Console.WriteLine(json);
// {
//   "date": "2016-06-27T00:00:00Z",
//   "game": "Donkey Kong",
//   "userPoints": {
//     "JamesNK": 9001,
//     "JoC": 1337,
//     "JessicN": 1000
//   }
// }