Reputation: 858
I am trying to find good code examples of using System.Text.Json with Azure Functions
I understand it's still a fairly new package but it's performance benefits are compelling, what I can't find is a simple example to import it in an Azure Function and do a basic JSON serialise and deserialise call.
E.g for a httpclient postasync call using Functions v4
Upvotes: 3
Views: 3544
Reputation:
One of the workaround I did of basic JSON Serialize and deserialize call in Azure Functions v4 Http Request with both Get and Post Methods using async operator:
.csproj
code:<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
<!--<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />-->
</ItemGroup>
<ItemGroup>
<None Update="host.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="local.settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>Never</CopyToPublishDirectory>
</None>
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace KrishAzFuncNet6Http
{
internal class Entity
{
public enum ProductType
{
Book,
Album,
Cinema
}
public class Product
{
[JsonPropertyName("id")]
public Guid Id { get; set; }
[JsonPropertyName("type")]
public ProductType Type { get; set; }
[JsonPropertyName("is_ready")]
public bool IsReady { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("revision")]
public int Revision { get; set; }
[JsonPropertyName("authors")]
public IList<string> Authors { get; set; }
[JsonPropertyName("registed_at")]
public DateTime RegistedAt { get; set; }
[JsonPropertyName("updated_at")]
public DateTime UpdatedAt { get; set; }
}
}
}
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Collections.Generic;
using static KrishAzFuncNet6Http.Entity;
using System.Text.Json;
namespace Company.Function
{
public static class HttpTrigger1
{
[FunctionName("HttpTrigger1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
var product = new Product
{
Id = Guid.NewGuid(),
Type = ProductType.Cinema,
IsReady = false,
Name = "Hari",
Revision = 1,
Authors = new List<string> { "Krishna", "Rajoli" },
RegistedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
};
var json = System.Text.Json.JsonSerializer.Serialize(product);
string responseMessage = json;
return new OkObjectResult(responseMessage);
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
GET Request:
POST Request call:
Upvotes: 0