CSharp · Console
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var source = new List<TreeModel>
{
new TreeModel
{
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamType = "mainine",
StreamName = "ArgOS_2_0",
Parent = null,
Compliance = "Released",
},
new TreeModel
{
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamType = "Release",
StreamName = "ArgOS_2_0_DHAL1",
Parent = "ArgOS_2_0",
Compliance = "Released",
},
new TreeModel
{
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamType = "Release",
StreamName = "ArgOS_2_0_child_DHAL2",
Parent = "ArgOS_2_0_DHAL1",
Compliance = "Released",
},
new TreeModel
{
ChangeFlowsFromParent = false,
ChangeFlowsToParent = true,
StreamType = "Release",
StreamName = "ArgOS_child_Gen2",
Parent = "ArgOS_2_0_child_DHAL2",
Compliance = "Released",
},
new TreeModel
{
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamType = "Release",
StreamName = "ArgOS_2_0_DHAL2",
Parent = "ArgOS_2_0",
Compliance = "Released",
},
new TreeModel
{
ChangeFlowsFromParent = true,
ChangeFlowsToParent = true,
StreamType = "Release",
StreamName = "ArgOS_2_0_DHAL3",
Parent = "ArgOS_2_0",
Compliance = "Released",
},
};
var output = MapToTreeModelJsonCollection(source);
var json = JsonConvert.SerializeObject(output,Formatting.Indented);
Console.WriteLine(json);
}
static ICollection<TreeModelJson> MapToTreeModelJsonCollection(ICollection<TreeModel> source)
{
// map all items
var allItems = source.Select(e => new TreeModelJson
{
ChangeFlowsFromParent = e.ChangeFlowsFromParent.ToString().ToLower(),
ChangeFlowsToParent = e.ChangeFlowsToParent.ToString().ToLower(),
Compliance = e.Compliance,
Parent = e.Parent ?? "none",
StreamName = e.StreamName,
StreamType = e.StreamType,
}).ToList();
// build tree structure
foreach (var item in allItems)
{
var children = allItems.Where(e => e.Parent == item.StreamName).ToList();
if (children.Any())
{
item.Children = children;
}
}
// return only root items
return allItems.Where(e => e.Parent == "none").ToList();
}
class TreeModel
{
public string StreamName { get; set; }
public string ParentName { get; set; }
public string StreamType { get; set; }
public bool ChangeFlowsFromParent { get; set; }
public bool ChangeFlowsToParent { get; set; }
public string Compliance { get; set; }
public string Parent { get; set; }
}
class TreeModelJson
{
[JsonProperty("ChangeFlowsFromParent")]
public string ChangeFlowsFromParent { get; set; }
[JsonProperty("ChangeFlowsToParent")]
public string ChangeFlowsToParent { get; set; }
[JsonProperty("StreamType")]
public string StreamType { get; set; }
[JsonProperty("streamName")]
public string StreamName { get; set; }
[JsonProperty("Parent")]
public string Parent { get; set; }
[JsonProperty("Compliance")]
public string Compliance { get; set; }
[JsonProperty("children", NullValueHandling = NullValueHandling.Ignore)]
public ICollection<TreeModelJson> Children { get; set; }
}
}