Using the System.Text.Json.JsonSerializer with Azure Functions



What is System.Text.Json.JsonSerializerclass?

I’ve been using JSON.NET by Newtonsoft in .NET Framework v4, but I’m not familiar with the above class.
So, I try to use System.Text.Json.JsonSerializer class.


  • Azure Functions v3
    • Microsoft.NET.Sdk.Functions v3.0.13
    • netcoreapp3.1
  • C# 8
  • .NET Core v3.1
  • Visual Studio 2019
  • Windows 10 v21H1


A simply usage of System.Text.Json is as follows:

  1. Define a class as entity.
    • If you want to control property name of JSON, adding JsonPropertyName attribute. It will need to use System.Text.Json.Serialization namespace.
  2. Serialize a defined entity class with System.Text.Json.JsonSerializer.Serialize.

Entity sample

Example is here:

using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Sample
    public enum ProductType

    public class Product
        public Guid Id { get; set; }

        public ProductType Type { get; set; }

        public bool IsReady { get; set; }

        public string Name { get; set; }

        public int Revision { get; set; }

        public IList<string> Authors { get; set; }

        public DateTime RegistedAt { get; set; }

        public DateTime UpdatedAt { get; set; }

API (Function class)

The following sample api has convert the Product class to json text with JsonSerializer.Serialize. When you access this api by your web browser, you will see the JSON as text.

using System;
using System.Collections.Generic;
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 System.Text.Json;

namespace Sample
    public static class Function1
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] 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 = "羅生門",
                Revision = 1,
                Authors = new List<string> { "黒澤 明", "橋本 忍" },
                RegistedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            var json = JsonSerializer.Serialize(product);
            string responseMessage = json;
            return new OkObjectResult(responseMessage);


{"id":"943fd8e5-86a2-4211-8891-e54be8fba0ef","type":2,"is_ready":false,"name":"\u7F85\u751F\u9580","revision":1,"authors":["\u9ED2\u6FA4 \u660E","\u6A4B\u672C \u5FCD"],"registed_at":"2021-10-12T13:29:59.1298115Z","updated_at":"2021-10-12T13:29:59.1298456Z"}

Next, I’ll try to use theJsonSerializerOptionsclass. It may have useful features.


Let's share this post!