This is a follow up to this question:
Configure Json.NET serialization settings on a class level
I have a class with properties of other classes
internal class Program
{
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Foo
{
public string Bar;
public NestedFoo NestedFoo { get; set; }
}
public class NestedFoo
{
public string NestedBar;
}
static void Main(string[] args)
{
Foo c = new();
c.NestedFoo = new();
c.Bar = "Bar";
c.NestedFoo.NestedBar = "NestedBar ";
string output = JsonConvert.SerializeObject(c, Formatting.Indented);
Console.WriteLine(output);
}
}
The output result is:
{
"bar": "Bar",
"nestedFoo": {
"NestedBar": "NestedBar "
}
}
With the above example, the NamingStrategyType
apply to the main class members only. The members inside the NestedFoo did not follow the NamingStrategyType
Means NestedBar
should be serialized as nestedBar
I know I have the following options, but I prefer to get the intended result using an attribute at the top level class:
- apply
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
to the every class inside. - Supply the naming strategy to the
JsonConvert.SerializeObject
.
Is there any way to have the attribute [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
apply to a top level class and all its nested properties?