Serializing in C#
Object to JSON in one call with System.Text.Json.
using System.Text.Json;
var config = new { Name = "typestar", Port = 8080, Debug = false };
// serialization is one call; options shape the output
Console.WriteLine(JsonSerializer.Serialize(config));
var pretty = new JsonSerializerOptions { WriteIndented = true };
Console.WriteLine(JsonSerializer.Serialize(config, pretty));
How it works
JsonSerializer.Serializehandles anonymous types too.- Options are their own object;
WriteIndentedpretty-prints. - The default output is compact, made for wires not eyes.
Keywords and builtins used here
falsenewtrueusingvar
The run, in numbers
- Lines
- 9
- Characters to type
- 328
- Tokens
- 59
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 39 seconds.
Step 1 of 3 in JSON, step 4 of 17 in The .NET library.