public bool SaveTree <T>(LookupTree <T> tree) where T : class, IPayload { if (tree == null) { return(false); } try { string treePath = _pathGetPattern.Get <T>(); string treeData = JsonConvert.SerializeObject( tree, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Formatting = Formatting.Indented }); if (string.IsNullOrWhiteSpace(treeData)) { throw new NullReferenceException("Tree data is null."); } return(_dataSetPattern.Set <T>(treePath, treeData)); } catch (Exception ex) { Console.WriteLine($"Save tree failed. [{ex.GetType().Name}] {ex.Message}"); return(false); } }
public bool TryLoadTree <T>(out LookupTree <T> loadedTree) where T : class, IPayload { loadedTree = null; Type type = typeof(T); if (_cachedTrees.TryGetValue(type, out object obj) && obj is LookupTree <T> tree) { loadedTree = tree; return(true); } try { string treePath = _pathGetPattern.Get <T>(); string treeData = _dataGetPattern.Get <T>(treePath); if (string.IsNullOrWhiteSpace(treeData)) { throw new NullReferenceException("Tree data is null."); } loadedTree = JsonConvert.DeserializeObject <LookupTree <T> >( treeData, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto, DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate, Formatting = Formatting.Indented }); _cachedTrees[type] = loadedTree ?? throw new NullReferenceException("Loaded tree is null."); return(true); } catch (Exception ex) { Console.WriteLine($"Load tree failed. [{ex.GetType().Name}] {ex.Message}"); return(false); } }