typestar

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

  1. JsonSerializer.Serialize handles anonymous types too.
  2. Options are their own object; WriteIndented pretty-prints.
  3. The default output is compact, made for wires not eyes.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in JSON, step 4 of 17 in The .NET library.

← Previous Next →