JSON formatting option for casing

Topics: Web Api
Oct 13, 2011 at 12:10 PM

Is there a way to control casing of JSON properties, both for formatting and binding of input data?  Our REST design rules state that JSON properties should be camelCase, but the default JSON formatter puts everything in PascalCase and doesn't bind input data that is camelCase (probably because .NET properties are Pascal).

I'm thinking about writing my own JSON formatter to fix this for now, but would prefer having this option on the standard JsonMediaTypeFormatter.

Oct 13, 2011 at 12:20 PM

Managed to find a workaround! I can use DataMemberAttribute to assign camelCase names to the properties.  At least I don't have to write a JSON formatter, but I'd prefer to having this as a formatter option.

Oct 13, 2011 at 1:04 PM

Using [DataMember] on the property (requires [DataContract] on its class) isn't a workaround, it's the official way.  I don't think a serializer should camelCase your properties of its own accord.

If you set this:-

      var config = new HttpConfiguration ();
      config.Formatters.XmlFormatter.UseDataContractSerializer = true;
then you can use the DataContractSerializer for the XML formatting of your model classes as well. I prefer this to the default XmlSerializer.
Oct 13, 2011 at 2:06 PM

IMO casing is a format thing, so enforcing camelCasing on all formats isn't very nice.  For example if I wanted PascalCasing on my XML and camel on JSON?

Oct 13, 2011 at 2:45 PM
Edited Oct 13, 2011 at 2:53 PM

If you wanted PascalCasing for XML and camel for JSON for a particular C#/VB property, then don't switch to the DCS for XML.  Stick with the default of the XmlSerializer for XML formatting, and (if necessary) apply the [XmlElement] attribute, and specify the "ElementName" arg to the attribute.  But it sounds like you wouldn't even need to, since the property name probably matches the XML element name you want.

I like to use the DCS for both XML and JSON, and to have the emitted JSON property name match the XML element name.  At least the Web API is giving us this fine-grained control - a good thing.

Oct 13, 2011 at 6:53 PM

But isn't this using the DataContract attributes for something else than they were intended for? What if I wanted to bind to a class and wanted different names of properties than I show externally?  For example to keep compatibility to a previously published REST contract.

Also I just mentioned XML as an example, it could have been a custom format.

Coordinator
Oct 13, 2011 at 8:35 PM

We do have an Issue Tracker item for being able to set convention used for all JSON properties:

http://wcf.codeplex.com/workitem/6

Please vote if this is important to you.

Daniel Roth

From: SiggiG [email removed]
Sent: Thursday, October 13, 2011 10:53 AM
To: Daniel Roth
Subject: Re: JSON formatting option for casing [wcf:275728]

From: SiggiG

But isn't this using the DataContract attributes for something else than they were intended for? What if I wanted to bind to a class and wanted different names of properties than I show externally? For example to keep compatibility to a previously published REST contract.

Also I just mentioned XML as an example, it could have been a custom format.

Oct 13, 2011 at 8:51 PM

Thank you :)