public static ItemTree FromFile(string aFileName) { string text = File.ReadAllText(aFileName); ItemTree root = (ItemTree)Newtonsoft.Json.JsonConvert.DeserializeObject(text, typeof(ItemTree)); return(root); }
static public void SaveToFile(ItemTree root, Dictionary <string, object> dataBindings, string filePath) { //Build json object from dataBindings var rootJsonObject = new JObject(); NameToJsonObject mapObjects = new NameToJsonObject(); root.FullName = ""; BuildTree(root, rootJsonObject, mapObjects); //update json object with data in databindings. foreach (var pair in dataBindings) { if (dataBindings[pair.Key] == null) { mapObjects[pair.Key].Value = null; } else { mapObjects[pair.Key].Value = JToken.FromObject(dataBindings[pair.Key]); } } //Serialize json string text = JsonConvert.SerializeObject(rootJsonObject, new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Formatting.Indented }); //write serialized json to disk File.WriteAllText(filePath, text); }
static private void BuildTree(ItemTree root, Newtonsoft.Json.Linq.JObject rootObject, NameToJsonObject mapObjects) { if (root.type == "root" || root.type == "menu") { if (root.type == "menu") { JObject childObject = new JObject(); rootObject.Add(root.name, childObject); rootObject = childObject; } foreach (ItemTree subitem in root.subitems) { BuildTree(subitem, rootObject, mapObjects); } } else { JProperty prop = new JProperty(root.name, null); rootObject.Add(prop); mapObjects.Add(root.FullName, prop); } }
public DataProvider(ItemTree aRoot) { fRootTemplate = aRoot; fBoundViews = new List <DataView>(); Initialize(); }