How to set camel case using IAsyncCollector with System.Text.Json in Azure Functions?

Viewed 402

I'm migrating Azure Functions v3 from Newtonsoft.Json to System.Text.Json and trying to get camelCase working globally.

For these:

  • SignalR
  • Service Bus output bindings
  • Cosmos DB

I was able to explicitly pass JsonSerializerOptions or set it globally (SignalR) but I'm not able to do so for IAsyncCollector.

Here is my code:

[FunctionName(nameof(SampleFunction))]
public async Task Run(
    [ServiceBusTrigger(ServiceBusQueue.QueueA)] string json,
    [ServiceBus(ServiceBusQueue.QueueB)] IAsyncCollector<Delivery> queueB,
    [ServiceBus(ServiceBusQueue.QueueC)] IAsyncCollector<Driver> queueC,
    ExecutionContext context)
{
        // ... do some work

        await queueB.AddAsync(objectB).ConfigureAwait(false);

        // ... do some more work

        await queueC.AddAsync(objectC).ConfigureAwait(false);
}

objectB and objectC ends up in service bus not with camel case. As a workaround I'm setting property names as case insensitive on the receiving function.

PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true

Any idea how can I get IAsyncCollector to serialize with camel case?

1 Answers

I recommend not using IAsyncCollector at all. The providers for IAsyncCollector don't have many knobs for configuration:

  • Serialization
  • Batching
  • Retries
  • Error handling

What's more, some implementations have changed these implementation details between releases without warning. For these reasons, I recommend never using IAsyncCollector and just using the APIs directly, where you have full control over all of these aspects. IAsyncCollector is a nice abstraction, but it's precisely that abstract nature that makes it unsuitable in the end.

Related