I Want my JSON in PascalCase

January 23, 2018
reference dev js

Because I like PascalCase, how to ensure that's what ASP.NET returns.

I know that what I am about to write and what you're about to read is heresy. If we were in Salem surely I would be put to burn. But, like those poor girls, I cannot resist the urge to practice my...wait. Were they actually witches?

Moving on...

I've been a .NET programmer for so long that camelCase for everything bothers me. It makes no sense. Everything in JS is camelCase. It's like the whole world is wearing the same Brooks Brothers cardigan. Except, it's not Brooks Brothers and it's not a cardigan. It's a 22 year old Hanes (with a tag) undershirt. And it drives me bananas - [queue Fergie]. B A N A N A S. Bananas!

You see, in the .NET world there are certain norms, conventions and etiquette to help other developers have a feel for what the deal is. What to expect. Knowing that the milk you're about to open isn't bad is usually ascertainable by first analyzing the expiration date. It might be rotten, lumpy, curdling milk. Nothing  is ever guaranteed. But, if it doesn't expire for 14 more days - you're probably safe. Same goes for these norms.

For instance:

If you see something like DateOfBirth it's probs a property on your object. if you see _DateOfBirth, it's probs a field in your class/object. If it's dateOfBirth it's probably a local variable or parameter. Methods, they should look like GetMyBeer() not getMyBeer(). person.LastName is better than person.lastName.

I know it's silly. I also know that I am causing myself unnecessary pain and suffering by bucking the system. I also know that some poor sap out there is going to come across my JS and wonder what dip shit didn't camelCase everything like a good little programmer.

I know.

But I am not sorry. I will not bend and break like a weak palm tree during Hurricane Irma. No!

So, that being said - since the ASP.NET team caved and now all JSON returned from an IActionResult is camelCase. Yep, they went ahead and changed my .NET class to some shenanigan of an object. With disrespectful lowercase lettering. My Name isn't tonyBasallo. That's silly.

So, for the sake of posterity and my failing memory, this is how you "fix" ASP.NET Core 2 (and others I suppose):

services.AddMvc()
   .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());

Thanks, T.

You're welcome, T.

P.S. - No hard feelings. I get it. JS is not .NET and JS runs on little camels that don'e know how to spell.

P.S.S. - FYI - I will respect other's projects and libs and will not attempt to re-write other projects using the superior PascalCase. And I will not add new code in the aforementioned superior casing.

Reference: https://github.com/aspnet/Mvc/issues/4283