Hello i have a project where i have a Json data deserialized to a object class then the object class is converter to a treeview, the tree view nodes are edited and i need to transform the treeview to json.
Object Class
public class CategoryLink
{
public string position { get; set; }
public string category_id { get; set; }
}
public class StockItem
{
public string item_id { get; set; }
public string product_id { get; set; }
public string stock_id { get; set; }
public string qty { get; set; }
public bool is_in_stock { get; set; }
public bool is_qty_decimal { get; set; }
public bool show_default_notification_message { get; set; }
public bool use_config_min_qty { get; set; }
public string min_qty { get; set; }
public string use_config_min_sale_qty { get; set; }
public string min_sale_qty { get; set; }
public bool use_config_max_sale_qty { get; set; }
public string max_sale_qty { get; set; }
public bool use_config_backorders { get; set; }
public string backorders { get; set; }
public bool use_config_notify_stock_qty { get; set; }
public string notify_stock_qty { get; set; }
public bool use_config_qty_increments { get; set; }
public string qty_increments { get; set; }
public bool use_config_enable_qty_inc { get; set; }
public bool enable_qty_increments { get; set; }
public bool use_config_manage_stock { get; set; }
public bool manage_stock { get; set; }
public object low_stock_date { get; set; }
public bool is_decimal_divided { get; set; }
public string stock_status_changed_auto { get; set; }
}
public class ExtensionAttributes
{
public IList<string> website_ids { get; set; }
public IList<CategoryLink> category_links { get; set; }
public StockItem stock_item { get; set; }
}
public class ProductLink
{
public string sku { get; set; }
public string link_type { get; set; }
public string linked_product_sku { get; set; }
public string linked_product_type { get; set; }
public string position { get; set; }
}
public class MediaGalleryEntry
{
public string id { get; set; }
public string media_type { get; set; }
public string label { get; set; }
public string position { get; set; }
public bool disabled { get; set; }
public IList<string> types { get; set; }
public string file { get; set; }
}
public class CustomAttribute
{
public string attribute_code { get; set; }
public object value { get; set; }
}
public class Product
{
public string id { get; set; }
public string sku { get; set; }
public string name { get; set; }
public string attribute_set_id { get; set; }
public string price { get; set; }
public string status { get; set; }
public string visibility { get; set; }
public string type_id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string weight { get; set; }
public ExtensionAttributes extension_attributes { get; set; }
public IList<ProductLink> product_links { get; set; }
public IList<object> options { get; set; }
public IList<MediaGalleryEntry> media_gallery_entries { get; set; }
public IList<object> tier_prices { get; set; }
public IList<CustomAttribute> custom_attributes { get; set; }
}
public class ProductCreateUpdate
{
public Product product { get; set; }
public bool saveOptions { get; set; }
}
//GET ALLL PRODUCTS
public class Item
{
public string id { get; set; }
public string sku { get; set; }
public string name { get; set; }
public string attribute_set_id { get; set; }
public string price { get; set; }
public string status { get; set; }
public string visibility { get; set; }
public string type_id { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public string weight { get; set; }
public ExtensionAttributes extension_attributes { get; set; }
public IList<ProductLink> product_links { get; set; }
public IList<object> options { get; set; }
public IList<MediaGalleryEntry> media_gallery_entries { get; set; }
public IList<object> tier_prices { get; set; }
public IList<CustomAttribute> custom_attributes { get; set; }
}
public class SearchCriteria
{
public IList<object> filter_groups { get; set; }
public string page_size { get; set; }
}
public class ProductGetAll
{
public IList<Item> items { get; set; }
public SearchCriteria search_criteria { get; set; }
public string total_count { get; set; }
}
Class Deserialized object to Treeview
public static class ObjectToTreeView
{
private sealed class IndexContainer
{
private int _n;
public int Inc() => _n++;
}
private static void FillTreeView(TreeNode node, JToken tok, Stack<IndexContainer> s)
{
if (tok.Type == JTokenType.Object)
{
TreeNode n = node;
if (tok.Parent != null)
{
if (tok.Parent.Type == JTokenType.Property)
{
n = node.Nodes.Add($"{((JProperty)tok.Parent).Name} <{tok.Type.ToString()}>");
}
else
{
n = node.Nodes.Add($"[{s.Peek().Inc()}] <{tok.Type.ToString()}>");
}
}
s.Push(new IndexContainer());
foreach (var p in tok.Children<JProperty>())
{
FillTreeView(n, p.Value, s);
}
s.Pop();
}
else if (tok.Type == JTokenType.Array)
{
TreeNode n = node;
if (tok.Parent != null)
{
if (tok.Parent.Type == JTokenType.Property)
{
n = node.Nodes.Add($"{((JProperty)tok.Parent).Name} <{tok.Type.ToString()}>");
}
else
{
n = node.Nodes.Add($"[{s.Peek().Inc()}] <{tok.Type.ToString()}>");
}
}
s.Push(new IndexContainer());
foreach (var p in tok)
{
FillTreeView(n, p, s);
}
s.Pop();
}
else
{
var name = string.Empty;
var value = JsonConvert.SerializeObject(((JValue)tok).Value);
if (tok.Parent.Type == JTokenType.Property)
{
name = $"{((JProperty)tok.Parent).Name} : {value}";
}
else
{
name = $"[{s.Peek().Inc()}] : {value}";
}
node.Nodes.Add(name);
}
}
public static void SetObjectAsJson<T>(this TreeView tv, T obj)
{
tv.BeginUpdate();
try
{
tv.Nodes.Clear();
var s = new Stack<IndexContainer>();
s.Push(new IndexContainer());
FillTreeView(tv.Nodes.Add("ROOT"), JsonConvert.DeserializeObject<JToken>(JsonConvert.SerializeObject(obj)), s);
s.Pop();
}
finally
{
tv.EndUpdate();
}
}
}
I edit the node of the treeview and then i need to passes to json again to update the data, here a screenshot