Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pranavkm/0df1f7330bae60a008fceb0d1327754a to your computer and use it in GitHub Desktop.
Save pranavkm/0df1f7330bae60a008fceb0d1327754a to your computer and use it in GitHub Desktop.
public class Person
{
static readonly string[] Clearances = new[] { "Alpha", "Beta", "Gamma", "Delta", "Epsilon" };
public string Name { get; set; }
public int Salary { get; set; }
public bool IsAdmin { get; set; }
public List<Person> Subordinates { get; set; }
public Dictionary<string, object> SecurityClearances { get; set; }
public static Person GenerateOrgChart(int totalDepth, int numDescendantsPerNode, int thisDepth = 0, string namePrefix = null, int siblingIndex = 0)
{
var name = $"{namePrefix ?? "CEO"} - Subordinate {siblingIndex}";
var rng = new Random(0);
return new Person
{
Name = name,
IsAdmin = siblingIndex % 2 == 0,
Salary = 10000000 / (thisDepth + 1),
SecurityClearances = Clearances
.ToDictionary(c => c, _ => (object)(rng.Next(0, 2) == 0)),
Subordinates = Enumerable.Range(0, thisDepth < totalDepth ? numDescendantsPerNode : 0)
.Select(index => GenerateOrgChart(totalDepth, numDescendantsPerNode, thisDepth + 1, name, index))
.ToList()
};
}
}
var largeOrgChart = Person.GenerateOrgChart(5, 4);
var largeOrgChart = JsonSerializer.Serialize(largeOrgChart);
var json = JsonSerializer.Serialize(largeOrgChart);
var deserialized = JsonSerializer.Deserialize<Person>(json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment