JSON with camel case in MVC

70fdb0e6-22bd-11e6-82c0-9eee1813955a

 

Yes, the wait is over.

I have heard it manytimes why MVC does not support Camel Case serialization. As workaround people used to use ServiceStack or Json.NET.

But as per recent announcement, MVC 1.0.0 now serializes JSON with camel case names by default.

How?

Before it was something like below:

public class Person
{
  public int Id { get; set; }
  public string FullName { get; set; }
}

Would serialize to:

{“Id”:1,”Name”:”Neel Bhatt”}

Now after changes, it would automatically convert it to camel case as below:

{“id”:1,”fullName”:”Neel Bhatt”} //// Not the first letter here

How to add in MVC?

Well MVC 1.0.0 gifts you this functionality  within services.AddMvc()

And in case if You want the old PascalCase behavior then you just need to add below lines into your services.AddMvc() of startup.cs class:

services

.AddMvc()

.AddJsonOptions(options =>

options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Please note that it is purely configurable and if you do not wish to use this then just write above line of code in startup.cs.

Happy coding!

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s