Skip to content

Instantly share code, notes, and snippets.

@shirshov
shirshov / gist:4a3a5e1e9e1acc93a1d5d91972ef49c1
Created May 19, 2021 19:43
Serialization of "Discriminated Unions" made of C# 9's records
using System;
using Newtonsoft.Json;

var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Objects };

var serialized = JsonConvert.SerializeObject(new NetworkAsset[] {
    new User("alexsh"),
    new Printer("192.168.1.2", "HP LaserJet")
}, settings: settings);