private static T DeserializeJson <T>(string url) { var json = new WebClient().DownloadString(url); var serializer = new JsonExSerializer.Serializer(typeof(T)); return((T)serializer.Deserialize(json)); }
public void Start() { //Load ConfigSections from file ConfigSections.Clear(); using (Stream stream = _fileSystem.OpenFile(_fileReferance)) { if (stream != null) { //deserialize config manager JsonExSerializer.Serializer serializer = new JsonExSerializer.Serializer(typeof(ConfigManager)); ConfigManager manager = null; try { manager = serializer.Deserialize(stream) as ConfigManager; if (manager == null) throw new System.Exception("Failed to deserialize ConfigManager"); } catch (Exception e) { _logger.Error("Could not deserialize config. The most likely cause is that the config is malformed. A new config will be created. The old config has been renamed with a .old extension appended."); stream.Close(); _fileSystem.Copy(_fileReferance, _fileReferance + ".old", true); } //Add deserialized sections to list if (manager != null) foreach (var configSection in manager.ConfigSections) ConfigSections.Add(configSection); } else _logger.Info("Could not open config. A new config will be created."); } //Create ConfigSections that were not in file foreach (var type in ReflectionHelper.GetDecoratedTypes<ConfigSectionAttribute>()) { if (ConfigSections.FirstOrDefault(c => c.GetType() == type) == null) { ConfigSections.Add(_kernel.Get(type)); } } //Register Ninject bindings for ConfigSection types foreach (var configSection in ConfigSections) { _kernel.Bind(configSection.GetType()).ToConstant(configSection); } }