0

When I directly return a dynamic object (obtained from Dapper) from a WebApi2 method, the casing of the properties is unmolested and exactly matches the column name casing defined in the query SQL.

The object in question is a row set, and if I operate on it with LINQ and return the resultant IEnumerable, suddenly the json is camel-cased.

This is tangentially relevant: Web API 2: how to return JSON with camelCased property names, on objects and their sub-objects

How can I configure serialisation to mind its own damn business and pass names through unchanged?

Not forced to camel case or Pascal case, just passed through.

Peter Wone
  • 17,965
  • 12
  • 82
  • 134

1 Answers1

0

An answer does not seem forthcoming, and my position on the best tactic has changed anyway, so I will answer my own question by describing how I dealt with the underlying problem of inconsistent serialisation behaviour.

In fact the business of forcing a casing pattern is all about consistency and predictability. It doesn't really matter what the behaviour is, as long as it's predictable.

Forcing improves predictability: it's one or the other, and you can change all the results from a web api with a single setting. So forcing is better, and the question changes from "How can I prevent forcing?" to "How can I cause dynamics to be forced like all other types?"

The answer to that lies here: .NET Core json serialization of properties on dynamic (ExpandoObject)

That answer is for Core 1, here's how to do the same with Core 2

// in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  . . .
  services.AddMvc().AddJsonOptions(options =>
  {
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  });
  . . .
}
Peter Wone
  • 17,965
  • 12
  • 82
  • 134