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 } }