Airo Global Software

Think Beyond Future !

enter image description here

ASP.NET Core Web APIs often need to serialize JSON data to JavaScript clients. On the server, your C# classes basically use Pascal Casing to name properties whereas JavaScript code uses Camel Casing for property names. Therefore it would be worthwhile to take a quick look at how ASP.NET Core serializes data in JSON form of Web API and MVC controllers.

There are three basic places from where C# objects are serialized in JSON format to the client:

  • Web API controllers
  • MVC controllers
  • Custom C# code using JsonSerializer

How camel casing and web API?

Let's first see the behavior of ASP.NET Core Web APIs when it comes to JSON serialization.

Analyze the resulting Web API controller:

[ApiController]
[Route("[controller]")]
public class EmployeeController : ControllerBase
{
[HttpGet]
public List<Employee> Get()
{
List<Employee> empList =
new List<Employee>();
empList.Add(new Employee()
{ EmployeeID=1,FirstName="Nancy",
LastName="Davolio" });
empList.Add(new Employee()
{ EmployeeID = 2, FirstName = "Andrew",
LastName = "Fuller" });
 return empList;
}
}

This code simply returns a List of Employee objects to the caller. The Employee class practiced in this system looks similar this:

public class Employee
{
public int EmpID { get; set; }
public string First { get; set; }
public string Last{ get; set; }
}

Begin the Startup class and modify the ConfigureServices() step as shown below:

public void ConfigureServices
(IServiceCollection services)
{
services.AddControllers()
 .AddJsonOptions(options =>
{
options.JsonSerializerOptions
.PropertyNamingPolicy = null;
});
}

As you can understand, the code does the AddJsonOptions() way to configure this behavior. The PropertyNamingPolicy section of JsonSerializerOptions is set to nothing.

If you need to explicitly set the casing to camel casing then you can print:

options.JsonSerializerOptions.
ropertyNamingPolicy = JsonNamingPolicy.CamelCase;

How camel casing and MVC controllers?

In ASP.NET Core MVC you can use Json() method to serialize data JSON format. Consider the following work that explains how this can be done.

public class HomeController: Controller
{
public IActionResult Index()
 {
List<Employee> empList =
new List<Employee>();
empList.Add(new Employee() 
{ EmployeeID = 1, FirstName = "Nancy",
LastName = "Davolio" });
empList.Add(new Employee()
{ EmployeeID = 2, FirstName = "Andrew",
LastName = "Fuller" });
return Json(empList);
 }
}

The Index() action made a List of Employee objects. The easiest is then serialized to the client handling Json() method.

In order to show MVC to pause using camel casing, you can type this code in the ConfigureServices().

services.AddControllersWithViews()
 .AddJsonOptions(options =>
{
options.JsonSerializerOptions
.PropertyNamingPolicy = null;
});

You run the AddJsonOptions() step on AddControllersWithViews().

You can also replace this behavior for single Json() calls as shown below:

var options = new JsonSerializerOptions()
{
 PropertyNamingPolicy = 
JsonNamingPolicy.CamelCase
};
return Json(empList, options);

How is custom code using JSON serializer?

At times you are required to serialize data yourself via custom code. Basically, you will use JsonSerializer to perform this task. Examine the following code:

public IActionResult Index()
{
List<Employee> empList =
new List<Employee>();
empList.Add(new Employee() 
{ EmployeeID = 1, FirstName = "Nancy", 
LastName = "Davolio" });
empList.Add(new Employee() 
{ EmployeeID = 2, FirstName = "Andrew", LastName = "Fuller" });
string json = JsonSerializer.Serialize(empList);
return Ok(json);
}

If you need JsonSerializer class to do camel casing you can do the serving:

var options = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
string json = JsonSerializer.Serialize(empList, options);
return Ok(json);

As you can see, the code makes a JsonSerializerOptions object and sets PropertyNamingPolicy property to JsonNamingPolicy.CamelCase. Next, the Serialize() system is called by giving empList and JsonSerializerOptions objects.

If you have any questions about the above topic or have to get services and consultations and get the best JSON services. Feel free to contact us. AIRO GLOBAL SOFTWARE will be your digital solution. E-mail id: [email protected]

enter image description here Author - Johnson Augustine
Chief Technical Director and Programmer
Founder: Airo Global Software Inc
LinkedIn Profile: www.linkedin.com/in/johnsontaugustine/