C# JSON Serialization
Use System.Text.Json for syntax and escaping, with a separate acceptance check for the parsed object.
How it works
A JSON serializer understands JSON syntax, not the workshop’s business invariants. Deserialization can produce missing or default property values and can return null for a null document. Check the resulting object and its fields before treating it as an accepted booking. Pick an explicit property-name policy and decide how to handle extra fields, duplicate names and version changes for a real protocol. This fixture uses only built-in libraries and no external service.
Run it locally
Use the .NET 10 SDK with C# 14 in a separate console lab. Create an empty project once with dotnet new console --framework net10.0, then replace Program.cs with this complete source and add any supporting files shown. Keep only one lesson entry point in the project. C# does not currently run in Bhylu’s browser playground. Apache, PHP and MySQL are not needed.
dotnet build -p:LangVersion=14.0 -p:Nullable=enable -p:TreatWarningsAsErrors=true
dotnet run --no-buildUse the SDK’s project workflow; the review compiles against its matching reference assemblies and executes the results without external NuGet packages. File-writing examples belong in a private empty lab directory.
Example
using System;
using System.Text.Json;
internal static class Program
{
private static void Main(string[] args)
{
const string json = "{\"Code\":\"W-17\",\"Places\":3}";
BookingDto? booking = JsonSerializer.Deserialize<BookingDto>(json);
if (booking is null || string.IsNullOrWhiteSpace(booking.Code) || booking.Places is < 1 or > 20)
throw new InvalidOperationException("Invalid booking data");
Console.WriteLine($"{booking.Code}: {booking.Places}");
Console.WriteLine(JsonSerializer.Serialize(booking));
}
}
internal sealed class BookingDto
{
public string? Code { get; set; }
public int Places { get; set; }
}
Expected result
W-17: 3
{"Code":"W-17","Places":3}
The nullable code property makes a missing code visible to analysis and validation. The chosen fixture deserializes successfully and serializes under the default property names. This does not prove every input is accepted safely; the advanced boundary tests supply malformed and invalid domain cases. Never manually concatenate untrusted text into JSON and hope quotes remain correct.
Consult Microsoft’s C# documentation for language and library contracts, including target-framework and version requirements.