skip to Main Content

I’m currently facing difficulties in converting object properties to lower camel case using Newtonsoft JSON serialization in C#. Here’s a simplified version of the code I’m using:

[JsonObject(Title = "booking")]
public class BookingJsonModel
{
    [JsonProperty("id")]
    public Guid Id { get; set; }

    [JsonProperty("reference")]
    public string Reference { get; set; } = null!;
    //...
}

and I have also tried to declare a property:

[JsonProperty(PropertyName = "reference")]
public string Reference { get; set; } = null!;

During serialization, I’m attempting to convert property names to lower camel case using the following code:

JsonSerializerSettings settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();

string body = JsonConvert.SerializeObject(data, settings);

Despite setting the ContractResolver to CamelCasePropertyNamesContractResolver(), the properties remain unchanged and still start with an upper case letter. It seems like both the ContractResolver and JsonProperty attributes are being ignored during serialization.

Could someone provide guidance or suggest an alternative approach to ensure the serialization of object properties to lower camel case using Newtonsoft JSON library in C#? Any insights or resolutions to this issue would be greatly appreciated. Thank you!

2

Answers


  1. The

    [JsonProperty("some name")]
    public string SomeName {get;set;}
    

    maps the property name in the raw JSON string, "some name" to the SomeName property in your class definition.

    If you have defined your class to use the Property name "SomeName" the upper case on your model is what JSON maps the string INTO.

    if you want your own model to have lower case words, simply define your model as such.

    so it becomes

    [JsonProperty = "reference"]
    public string reference {get;set;}
    

    This however violates normal naming convention that states Properties should start with upper case letters.

    or, use the

    [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
    

    Annotation instead, that should actually resolve what you want.

    Login or Signup to reply.
  2. If you’re using Newtonsoft.Json in C# and want to convert property names to lower camel case (e.g., myProperty instead of MyProperty), you can achieve this by configuring the JsonSerializerSettings with the ContractResolver property.

    Here’s an example of how to configure Newtonsoft.Json to serialize objects with lower camel case property names:

       using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    
    class Program
    {
        static void Main()
        {
            // Your object to serialize
            var myObject = new MyClass { MyProperty = "SomeValue", AnotherProperty = 42 };
    
            // Configure JsonSerializerSettings with CamelCasePropertyNamesContractResolver
            var settings = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting = Formatting.Indented // Optional: for pretty-printing the JSON
            };
    
            // Serialize the object to JSON with lower camel case property names
            string json = JsonConvert.SerializeObject(myObject, settings);
    
            // Output the JSON
            Console.WriteLine(json);
        }
    }
    
    public class MyClass
    {
        public string MyProperty { get; set; }
        public int AnotherProperty { get; set; }
    }
    

    See the reference output

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search