Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Function Cosmos DB Output Binding - Custom JsonSerializerSettings

I have an Azure Function with a CosmosDB output binding, like this:

public static class ComponentDesignHttpTrigger
{
    [FunctionName("ComponentDesignInserter-Http-From-ComponentDesign")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "fromComponentDesign")] HttpRequest request,
        [CosmosDB(
            databaseName: StorageFramework.CosmosDb.DatabaseId,
            collectionName: Storage.ComponentDesignCollectionId,
            ConnectionStringSetting = "CosmosDBConnection")] out ComponentDesign componentDesignToInsert,
        ILogger log)
    {
        var requestBody = new StreamReader(request.Body).ReadToEnd();
        componentDesignToInsert = JsonConvert.DeserializeObject<ComponentDesign>(requestBody);

        return new OkObjectResult(componentDesignToInsert);
    }
}

In this function componentDesignToInsert is automatically serialized and put into CosmosDB after the function finishes executing. But the default serialization does not put things in camelCase. For this Json.NET lets you provide custom serializer settings, like this:

var settings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var json = JsonConvert.SerializeObject(yourObject, settings);

but I'm unsure how I can integrate this with my output binding. How can I accomplish this?

like image 537
Scotty H Avatar asked Mar 08 '19 04:03

Scotty H


People also ask

Can you choose output bound as a cosmos DB in Azure function?

The Azure Cosmos DB output binding lets you write a new document to an Azure Cosmos DB database using the SQL API. For information on setup and configuration details, see the overview.

How do you write to Cosmos DB from Azure function?

In the Azure portal, navigate to and select the function app you created previously. Select Functions, and then select the HttpTrigger function. Select Integration and + Add output. Name of the binding type to select to create the output binding to Azure Cosmos DB.

What is replica set in Cosmos DB?

Azure Cosmos DB's global distribution relies on two key abstractions – replica-sets and partition-sets. A replica-set is a modular Lego block for coordination, and a partition-set is a dynamic overlay of one or more geographically distributed physical partitions.

What is change feed in Cosmos DB?

Change feed in Azure Cosmos DB is a persistent record of changes to a container in the order they occur. Change feed support in Azure Cosmos DB works by listening to an Azure Cosmos DB container for any changes. It then outputs the sorted list of documents that were changed in the order in which they were modified.


1 Answers

Output binding does not expose the serializer settings at this moment.

One thing you can do though, is leverage your own custom DocumentClient for the operation.

One important thing though is that the DocumentClient instance needs to be static (more details on https://github.com/Azure/azure-functions-host/wiki/Managing-Connections).

private static Lazy<DocumentClient> lazyClient = new Lazy<DocumentClient>(InitializeDocumentClient);
private static DocumentClient documentClient => lazyClient.Value;

private static DocumentClient InitializeDocumentClient()
{
    // Perform any initialization here
    var uri = new Uri("example");
    var authKey = "authKey";

    var settings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    };
    return new DocumentClient(uri, authKey, settings);
}

[FunctionName("ComponentDesignInserter-Http-From-ComponentDesign")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "fromComponentDesign")] HttpRequest request,
    ILogger log)
{
    var requestBody = new StreamReader(request.Body).ReadToEnd();
    var componentDesignToInsert = JsonConvert.DeserializeObject<ComponentDesign>(requestBody);

    var collectionUri = UriFactory.GetDocumentCollectionUri(StorageFramework.CosmosDb.DatabaseId, Storage.ComponentDesignCollectionId);
    await documentClient.UpsertDocumentAsync(collectionUri, componentDesignToInsert);
    return new OkObjectResult(componentDesignToInsert);
}

Another option is to decorate the class with JsonProperty if that suits your scenario.

like image 102
Matias Quaranta Avatar answered Oct 13 '22 01:10

Matias Quaranta