Reflection discovers information about types at runtime. A source generator can instead produce C# during compilation. The right choice depends on what must be known at runtime and what your deployment supports. This comparison uses the source generator included with System.Text.Json; it does not implement a custom generator.
Try both JSON paths
Create a .NET 10 console application and replace its program with the following complete example. It serializes the same record using the default reflection-based overload and then an explicitly supplied generated context.
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
var item = new Reading("sensor", 21);
string reflectionJson = JsonSerializer.Serialize(item);
string generatedJson = JsonSerializer.Serialize(item, ReadingJsonContext.Default.Reading);
Console.WriteLine(reflectionJson);
Console.WriteLine(generatedJson);
Console.WriteLine(reflectionJson == generatedJson);
public record Reading(string Name, int Value);
[JsonSerializable(typeof(Reading))]
internal partial class ReadingJsonContext : JsonSerializerContext { }
The output is two identical JSON lines, followed by True: each line contains the Name and Value properties. Equality here checks the produced strings for this specific input; it is not a general proof that all options and types behave identically.
Where the information comes from
By default, the serializer gathers and caches type metadata using reflection at runtime. The attribute requests generated serialization information for Reading; the partial context makes that information available via ReadingJsonContext.Default.Reading. Passing that property to the serializer selects the generated type information explicitly. The compiler runs the generator during the build and compiles its output with the application. See Microsoft’s comparison and source-generation guide.
Choose for your constraints
Reflection is convenient when types and their members must be discovered at runtime. Generated metadata requires you to register the types you need, but makes the contract explicit at build time. For trimming or Native AOT, check your serializer configuration and deployment warnings: some reflection APIs are unavailable in Native AOT. For a trimmed publish, reflection-based JSON serialization is disabled by default unless configured otherwise, so the first serialization call in this example would throw under that default. For System.Text.Json in Native AOT applications, use source generation because required reflection APIs are not available. This example does not test a trimmed or Native AOT publish. Our Native AOT and JIT comparison covers those deployment choices.
JSON source generation supports metadata and serialization-optimization modes. Some options and member visibility differ between modes and reflection; consult the mode documentation for your model. Generated code can change startup, memory use or throughput, but measure your actual workload and options rather than assuming a speedup. This example asserts output, not performance.
Next experiment
Add a second record type, register it with another JsonSerializable attribute, and use its generated type information. Compare serialization and deserialization behavior with the options your application actually uses before switching an existing contract.