Search code examples
javascriptc#jsoncamelcasingpascalcasing

API standard for JS / C# interface - Camel vs. Pascal case


We have a system where a server written in C# implements a REST interface and the client is written in JS, most of the data is passed as JSON.

In a way there is here a collision between the camelCase and PascalCase worlds.

The parameters mostly derive from names within the server and are all in PascalCase.

The JS front end is written using camelCase and is expecting that the server will accept parameters like this since JSON comes from the JS world.

What would constitute an acceptable solution, and why?


Solution

  • For Web API method's parameter use "camelCase", I think this suggested name convention for method's parameters in the .NET

    For the properties use JSON serialization attributes

    public class Person
    {
        [JsonProperty("id")]
        public int Id { get; set; }
    
        [JsonProperty("name")]
        public string Name { get; set; }
    }
    

    Serialized object will looks like:

    { "id": 10, "name": "My Name" }
    

    With this approach your C# code stay with PascalCase and client side(Javascript) will use camelCase.