Skip to main content
added 34 characters in body
Source Link
dynamiclynk
  • 2.3k
  • 1
  • 28
  • 31

Serializer

 public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite, new JsonSerializerSettings { Formatting = Formatting.Indented, }); using (var writer = new StreamWriter(filePath, append)) { writer.Write(contentsToWriteToFile); } } 

Object

namespace MyConfig { public class AppConfigurationSettings { public AppConfigurationSettings() { //* initialize the object if you want to output a new document // * for use as a template ofor default settings possibly when  * an app is started. / */launched if (AppSettings == null) { AppSettings=new AppSettings();} } public AppSettings AppSettings { get; set; } } public class AppSettings { public bool DebugMode { get; set; } = false; } } 

Implementation

var jsonObject = new AppConfigurationSettings(); WriteToJsonFile<AppConfigurationSettings>(file.FullName, jsonObject); 

Output

{ "AppSettings": { "DebugMode": false } } 

Serializer

 public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite, new JsonSerializerSettings { Formatting = Formatting.Indented, }); using (var writer = new StreamWriter(filePath, append)) { writer.Write(contentsToWriteToFile); } } 

Object

namespace MyConfig { public class AppConfigurationSettings { public AppConfigurationSettings() { //initialize the object if you want to output a new document //for use as a template of default settings when an app is //launched if (AppSettings == null) { AppSettings=new AppSettings();} } public AppSettings AppSettings { get; set; } } public class AppSettings { public bool DebugMode { get; set; } = false; } } 

Implementation

var jsonObject = new AppConfigurationSettings(); WriteToJsonFile<AppConfigurationSettings>(file.FullName, jsonObject); 

Output

{ "AppSettings": { "DebugMode": false } } 

Serializer

 public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite, new JsonSerializerSettings { Formatting = Formatting.Indented, }); using (var writer = new StreamWriter(filePath, append)) { writer.Write(contentsToWriteToFile); } } 

Object

namespace MyConfig { public class AppConfigurationSettings { public AppConfigurationSettings() { /* initialize the object if you want to output a new document  * for use as a template or default settings possibly when  * an app is started.  */ if (AppSettings == null) { AppSettings=new AppSettings();} } public AppSettings AppSettings { get; set; } } public class AppSettings { public bool DebugMode { get; set; } = false; } } 

Implementation

var jsonObject = new AppConfigurationSettings(); WriteToJsonFile<AppConfigurationSettings>(file.FullName, jsonObject); 

Output

{ "AppSettings": { "DebugMode": false } } 
Source Link
dynamiclynk
  • 2.3k
  • 1
  • 28
  • 31

Serializer

 public static void WriteToJsonFile<T>(string filePath, T objectToWrite, bool append = false) where T : new() { var contentsToWriteToFile = JsonConvert.SerializeObject(objectToWrite, new JsonSerializerSettings { Formatting = Formatting.Indented, }); using (var writer = new StreamWriter(filePath, append)) { writer.Write(contentsToWriteToFile); } } 

Object

namespace MyConfig { public class AppConfigurationSettings { public AppConfigurationSettings() { //initialize the object if you want to output a new document //for use as a template of default settings when an app is //launched if (AppSettings == null) { AppSettings=new AppSettings();} } public AppSettings AppSettings { get; set; } } public class AppSettings { public bool DebugMode { get; set; } = false; } } 

Implementation

var jsonObject = new AppConfigurationSettings(); WriteToJsonFile<AppConfigurationSettings>(file.FullName, jsonObject); 

Output

{ "AppSettings": { "DebugMode": false } }