CSharp · Console
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
public class Program
{
public static void Main()
{
var myClass = new MyClass
{
ThisPropertyShouldBeCamelCase = new Dictionary<string, ICollection<string>>
{
{ "ThisKeyShouldBeAllLowerCase", new List<string> { "FOO", "Bar" } },
{ "SameThingHere-LOWER", new List<string> { "Baz", "QuuX" } },
{ "LowerCaseDictionaryKey3", new List<string> { "Blah" } },
},
CamelCaseProperty2 = 1234
};
var settings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver { NamingStrategy = new CustomNamingStrategy() }
};
string json = JsonConvert.SerializeObject(myClass, Formatting.Indented, settings);
Console.WriteLine(json);
}
}
public class MyClass
{
public Dictionary<string, ICollection<string>> ThisPropertyShouldBeCamelCase { get; set; }
public int CamelCaseProperty2 { get; set; }
}
public class CustomNamingStrategy : CamelCaseNamingStrategy
{
public CustomNamingStrategy()
{
ProcessDictionaryKeys = true;
ProcessExtensionDataNames = true;
OverrideSpecifiedNames = true;
}
public override string GetDictionaryKey(string key)
{
return key.ToLower();
}
}