This project has moved. For the latest updates, please go here.

Case insensitive serializing

Feb 27, 2009 at 7:26 PM

Is it possible to deserialize a Json string with its property names starting with lower case:

    "firstName" : "Paul",
    "lastName" : "Harrington"

into a .NET object where its properties start with upper case:

public class Person
    public string FirstName { get; set;}
    public string LastName { get; set; }

One way I can think of now is to decorate all members with [JsonProperty("firstName")] attributes. However, I would prefer not to litter attributes all over the class considering how it affects readability and can become a maintenance nightmare as the number of Json objects and properties increases.

The other way I thought is perhaps I can create a custom JsonConverter that serialize .NET objects to Json string where property names are converted from upper to lower case (i.e public string FirstName to "firstName" : "..."). And vice versa for deserializing.

This will be useful since convention for Javascript is to have property names in camelCasing where as .NET classes like their members in  PascalCasing.

Is this possible?

Mar 2, 2009 at 11:33 PM

I'm going to make the serializer a bit more forgiving when it matches properties. It will try for an exact match first, then it will attempt a case insensitive match. That should give you what you are looking for.

I'm making lots of changes at the moment so I don't know when I will check it in. I'll post here when I do.

~ James
Mar 2, 2009 at 11:43 PM
Thanks .. appreciated
Mar 14, 2009 at 12:43 AM
Source code is in CodePlex if you want to grab it and build a copy.