In Visual Studio, if I create a new project using the "Azure Functions" template, the JSON serialization used by that function will be different depending on which version of the template I use. I'm wondering how can I tell which JSON serialization will be used, solely from looking at the code/project.
Minimal complete and viable example
(you need to have the Visual Studio "Azure" workload installed to do this)
- Create a new project using the "Azure Functions" template, choose ".NET 6.0 (Long term support)" and use the default options for everything else.
- Replace the
string responseMessageandreturnlines with this code:
var content = new ResponseContent();
return new OkObjectResult(content);
- Add this class to the project:
public class ResponseContent
{
[Newtonsoft.Json.JsonProperty("CouldBeThis")]
public string Example { get; set; } = "MyValue";
}
- Create a new project using the "Azure Functions" template, choose ".NET 6.0 Isolated (Long Term Support)" and use the default options for everything else.
- Replace the
returnline with the same code as shown in 2. - Add this class to the project:
public class ResponseContent
{
[System.Text.Json.Serialization.JsonPropertyName("ButMaybeThis")]
public string Example { get; set; } = "MyValue";
}
- Run both projects, and use a browser to visit the URL shown for each project. The outputs are, respectively:
{
"CouldBeThis": "MyValue"
}
and
{
"ButMaybeThis": "MyValue"
}
The above is demonstrating that one project is using Newtonsoft.Json for Json serialization, but the other is using System.Text.Json. But if I had not yet put any attributes on that object, then how could I have determined which JSON serialization will be used, solely from looking at the code/project?
EDIT after request to add the complete function code
Here's the complete in-process function (steps 1-3 above):
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;
namespace FunctionAppA
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
ResponseContent content = new ResponseContent();
return new OkObjectResult(content);
}
}
public class ResponseContent
{
[Newtonsoft.Json.JsonProperty("CouldBeThis")]
public string Example { get; set; } = "MyValue";
}
}
and here's the complete isolated version
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
namespace FunctionAppI
{
public class Function1
{
private readonly ILogger<Function1> _logger;
public Function1(ILogger<Function1> logger)
{
_logger = logger;
}
[Function("Function1")]
public IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequest req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
ResponseContent content = new ResponseContent();
return new OkObjectResult(content);
}
public class ResponseContent
{
[System.Text.Json.Serialization.JsonPropertyName("ButMaybeThis")]
public string Example { get; set; } = "MyValue";
}
}
}
AFAIK in the latest versions, Azure Functions by default uses the venerable
Newtonsoft.Jsonpackage. This package is included as a dependency of theMicrosoft.NET.Sdk.Functionspackage in.csprojproject file.Though Azure Functions uses the same
OkObjectResultclass fromASP.NET Core framework, the serialization process in Azure Functions is handled differently due to its reliability onNewtonsoft.Json.using Newtonsoft.Jsonnamespace andMicrosoft.NET.Sdk.Functions,Microsoft.AspNetCore.Mvcpackages in your project, it indicates the use ofNewtonsoft.Json:using System.Text.Jsonnamespace, andMicrosoft.Azure.Functions.Extensions,Microsoft.AspNetCore.Mvc.Jsonpackages in yourcsprojfile, it indicates the use ofSystem.Text.Json.Note: The function code you have provided is using Newtonsoft.json.
References:
https://svrooij.io/2023/07/26/system-text-json-azure-functions/