Opinion on the code i've written

Hello, I have a discord bot im working on, the code i am about to show reads a JSON Config file. I want to acess specific parts of the config across the entire program.
using Newtonsoft.Json;

namespace TeleSaini.Config
{
class JsonConfig
{
public string BotToken { get; set; }
public string BotPrefix { get; set; }
public string DbUser { get; set; }
public string DbPassword { get; set; }
public long HomeserverGuildId { get; set; }
}

public class TeleConfig
{
private const string TELECONFIG_PATH = "config.json";
private string fileContents;
private JsonConfig settings;

private TeleConfig()
{
CheckDirectories(); // if the config file does not exist, don't even bother.
ReadAll();
}

public static TeleConfig GetInstance()
{
return new TeleConfig();
}

private void CheckDirectories()
{
if (!File.Exists(TELECONFIG_PATH))
{
Console.WriteLine("Config.json does NOT exist. Please create one based off the sample provided.");
Console.ReadKey();
Environment.Exit(1);
}

Console.WriteLine("Config.json exists! Hurray!");
}

private void ReadAll()
{
fileContents = File.ReadAllText(TELECONFIG_PATH);
settings = JsonConvert.DeserializeObject<JsonConfig>(fileContents);
}

public string BotToken { get => settings.BotToken; }
public string BotPrefix { get => settings.BotPrefix; }
public string DbUser { get => settings.DbUser; }
public string DbPassword { get => settings.DbPassword; }
public long HomeserverGuildId { get => settings.HomeserverGuildId; }
}

}
using Newtonsoft.Json;

namespace TeleSaini.Config
{
class JsonConfig
{
public string BotToken { get; set; }
public string BotPrefix { get; set; }
public string DbUser { get; set; }
public string DbPassword { get; set; }
public long HomeserverGuildId { get; set; }
}

public class TeleConfig
{
private const string TELECONFIG_PATH = "config.json";
private string fileContents;
private JsonConfig settings;

private TeleConfig()
{
CheckDirectories(); // if the config file does not exist, don't even bother.
ReadAll();
}

public static TeleConfig GetInstance()
{
return new TeleConfig();
}

private void CheckDirectories()
{
if (!File.Exists(TELECONFIG_PATH))
{
Console.WriteLine("Config.json does NOT exist. Please create one based off the sample provided.");
Console.ReadKey();
Environment.Exit(1);
}

Console.WriteLine("Config.json exists! Hurray!");
}

private void ReadAll()
{
fileContents = File.ReadAllText(TELECONFIG_PATH);
settings = JsonConvert.DeserializeObject<JsonConfig>(fileContents);
}

public string BotToken { get => settings.BotToken; }
public string BotPrefix { get => settings.BotPrefix; }
public string DbUser { get => settings.DbUser; }
public string DbPassword { get => settings.DbPassword; }
public long HomeserverGuildId { get => settings.HomeserverGuildId; }
}

}
1 Reply
Angius
Angius7mo ago
Why Newtonsoft? C# has native json support, no need for 3rd party libraries
Want results from more Discord servers?
Add your server