I am using ASP.Net to serialize classes designed in C# to JSON. My Javascript application then requests those objects with AJAX. I have done this on a couple of projects now, but I run into the problem that the C# standard naming conventions use PascalCase for the public members, and in Javascript they are generally camelCase.
This particularly becomes an issue when I have some Javascript-only classes that use camelCase and some mixed-use classes that use PascalCase since I designed them in C# originally.
How does everyone else deal with this sort of problem? Do you just pick one or the other? If so which one is more broadly chosen? Or is there perhaps a clever way to user a JSON serializer to switch between the two naming conventions?
I just use what the server gives me.
C#
JS:
You could use JSON.net to serialize the data for you, and you can even tell it to use camelCase. This question asks something similar. Here's a code example for reference:
Don't worry about the performance of JSON.net either, as the performance of it versus native serialization is comparable (better in most cases).
If you are using the
DataContractJsonSerializer
, you can specify the name using theDataMemberAttribute.Name
property:will serialize to