I've the following code (.Net Core 3.1):
var dictionary = new Dictionary<string, object>()
{
{"Key1", 5},
{"Key2", "aaa"},
{"Key3", new Dictionary<string, object>(){ {"Key4", 123}}}
};
var serialized = JsonSerializer.Serialize(dictionary, new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
});
I'd expect that serialized variable has a string that looks like:
{"key1":5,"key2":"aaa","key3":{"key4":123}}
However what I get is:
{"key1":5,"key2":"aaa","Key3":{"key4":123}}
I don't really understand why some properties are camelcased and some not. What I was able to figure out is that when a dictionary contains key-value where the value is "primitive type" (int, string, DateTime), it works ok. However, when the value is a complex type (another dicionary, a custom class) it does not work. Any suggestions of how to make the serializer produce keys as camelcase ?