Project · Data

Task summary

Deserialize a prepared task document, validate its rows, and summarize status.

All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.

Program.cs

using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

var json = """[{"Title":"Read","Done":true},{"Title":"Build","Done":false}]""";
var items = JsonSerializer.Deserialize<TaskItem[]>(json) ?? Array.Empty<TaskItem>();
if (items.Any(x => string.IsNullOrWhiteSpace(x.Title))) Console.WriteLine("Invalid title");
else Console.WriteLine(TaskSummary.Render(items));

Models/TaskItem.cs

public sealed record TaskItem(string Title, bool Done);

Reports/TaskSummary.cs

using System.Collections.Generic;
using System.Linq;
public static class TaskSummary
{
    public static string Render(IEnumerable<TaskItem> source)
    {
        var rows = source.ToArray();
        return $"{rows.Count(x => x.Done)} done / {rows.Length} total";
    }
}

Tasksummary.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>