19

Upgrading from ASP.NET WebAPI Beta to RC has provided some amount of excitement and a great deal of frustration. I have been able to work through the majority of the issues, but the one that is biting me right now is case-sensitivity of JSON request data.

The formatter used for JSON requests (which by default is a JSON.NET formatter) appears to be case sensitive while the formatter used for form-urlencoded data does not. Is there a way to configure the JSON requests to use a case-insensitive formatter?

Here is a simple example to illustrate the problem that I am having with JSON requests:

HTML / JavaScript

<button id="tester">Click here!</button>

<script type="text/javascript">
    $(function () {
        $("#tester").on("click", function() {
            $.ajax({
                type: "POST",
                url: "/Api/Test/Index/" + 168,
                data: ko.toJSON({ key: 123, value: "test value" }), // THIS FAILS
                               // Key: 123, Value: "test value" <- BUT THIS WORKS
                contentType: "application/json; charset=utf-8",
                statusCode: {
                    200: function () {
                        $("body").append("<p>Success</p>");
                    },
                    400: function () {
                        $("body").append("<p>Failure</p>");
                    }
                }
            }).always(function () {
                $("body").append("<hr />");
            });
        });
    });
</script>

C#

public class TestController : ApiController
{
    public HttpResponseMessage Index(int? id, KeyValuePair<int, string> test)
    {
        if (id != 168 || test.Key != 123 || test.Value != "test value")
            return Request.CreateResponse(HttpStatusCode.BadRequest);

        return Request.CreateResponse(HttpStatusCode.OK);
    }
}

I've provided a comment on the line where the JSON data is provided. I would rather not break convention with my JavaScript objects by proper casing property members and I certainly do not want to break convention by lower casing my C# properties. Any thoughts?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Phil Klein
  • 7,344
  • 3
  • 30
  • 33
  • I'm a bit confused here. You send your data within a POST request - in other words, within a request body. But in the HTTP documentation it's said that header fields (and values) are considered case-insensitive, and there's nothing about the body... Why do you think "it's not the behaviour for form-urlencoded data", I wonder? – raina77ow Jun 29 '12 at 14:52
  • Check [this discussion](http://groups.google.com/group/soundcloudapi/browse_thread/thread/c266c0d2efa88fbb/9ad3969dde919cba), for example. – raina77ow Jun 29 '12 at 14:53
  • 1
    @raina77ow This isn't really a question about the HTTP specification but the behavior of JSON.NET formatter used by ASP.NET WebAPI RC. The FormUrl formatter appears to be case-insensitive while the JSON.NET formatter *is* case-sensitive. – Phil Klein Jun 29 '12 at 15:33
  • I think case-sensitivity is correct. Why would you need case-insensitivity while C# and JavaScript are both case-sensitive? – Aliostad Jun 29 '12 at 15:56
  • I realize that they are both case sensitive, but the value provider or formatter used for parameter binding does not need to be case-sensitive as evidenced by the behavior of form-urlencoded data. I'll update the question to reflect this. Thanks. – Phil Klein Jun 29 '12 at 16:08

2 Answers2

25

The Json.NET formatter is case insensitive for deserialization (json -> CLR).

On serialization, you can get camel casing by using the CamelCasePropertyNamesContractResolver.

In your Global.asax:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Mike Wasson
  • 6,572
  • 2
  • 24
  • 20
  • I believed this to be the case, but I haven't been able to prove that behavior. Please see my related question: http://stackoverflow.com/questions/11266695/json-net-case-insensitive-property-deserialization – Phil Klein Jun 29 '12 at 17:58
  • Hmm ... I just did some experimenting with JsonConvert. Deserializing KeyValuePair is case-sensitive, like you say, but if I create a POCO, the behavior is definitely case insensitive. Seems to be related to KeyValuePair having getters but no setters, because I defined my own version of KeyValuePair with setters and that worked. – Mike Wasson Jun 29 '12 at 18:44
  • Ah, you might be on to something ... I'll do some further experimenting and see if I can determine how it is deserializing with matching cased keys. Thanks! – Phil Klein Jun 29 '12 at 20:21
  • Do you know whether the same is possible in XML? See http://stackoverflow.com/questions/16835100/asp-net-web-api-xml-in-camelcase – Schiavini Jun 13 '13 at 09:56
9

After digging into this issue in significant depth I came to realize that I was hitting a bug in the Json.NET KeyValuePair converter. James Newton-King was kind enough to answer my related question and provided a link to the fix:

Json.NET Case-insensitive Property Deserialization

Community
  • 1
  • 1
Phil Klein
  • 7,344
  • 3
  • 30
  • 33