C
C#4mo ago
Mekasu0124

✅ Writing a dictionary to json file

public Dictionary<string, string> CheckForStyleFile(string username)
{
StyleModel defaultStyle = new()
{
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
};

Dictionary<string, string> defaultDict = new();
defaultDict.Add("background", defaultStyle.BackgroundColor);
defaultDict.Add("border", defaultStyle.BorderColor);
defaultDict.Add("font", defaultStyle.FontColor);

try
{
if (!File.Exists(_stylesFile))
{
var data = JsonSerializer.Serialize(defaultDict);
File.AppendAllText(_stylesFile, data);
return defaultDict;
}
else
{
if (username.Length == 0)
{
return defaultDict;
}
}
}
}
public Dictionary<string, string> CheckForStyleFile(string username)
{
StyleModel defaultStyle = new()
{
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
};

Dictionary<string, string> defaultDict = new();
defaultDict.Add("background", defaultStyle.BackgroundColor);
defaultDict.Add("border", defaultStyle.BorderColor);
defaultDict.Add("font", defaultStyle.FontColor);

try
{
if (!File.Exists(_stylesFile))
{
var data = JsonSerializer.Serialize(defaultDict);
File.AppendAllText(_stylesFile, data);
return defaultDict;
}
else
{
if (username.Length == 0)
{
return defaultDict;
}
}
}
}
I'm writing a helper function that is executed at run-time to check if the styles.json file exists and if it doesn't then it creates the file and writes in the default style dictionary. The wanted end result looks like this
{
"default": {
"background": "#000000",
"border": "#42fe45",
"font": "#CDCDCD"
}
}
{
"default": {
"background": "#000000",
"border": "#42fe45",
"font": "#CDCDCD"
}
}
I've looked at a couple of different stack overflows, but they have some weird code as their solutions that I've never seen before.
27 Replies
MODiX
MODiX4mo ago
leowest
REPL Result: Success
var data = new UserStyle
{
Default = new Dictionary<string, string>
{
{ "background", "#000000" },
{ "border", "#24de45" },
{ "font", "#CDCDCD" }
}
};

var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserStyle
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public Dictionary<string, string> Default {get;set;}
}
var data = new UserStyle
{
Default = new Dictionary<string, string>
{
{ "background", "#000000" },
{ "border", "#24de45" },
{ "font", "#CDCDCD" }
}
};

var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserStyle
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public Dictionary<string, string> Default {get;set;}
}
Console Output
{"default":{"background":"#000000","border":"#24de45","font":"#CDCDCD"}}
{"default":{"background":"#000000","border":"#24de45","font":"#CDCDCD"}}
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 524.933ms | Execution: 85.996ms | React with ❌ to remove this embed.
Mekasu0124
Mekasu01244mo ago
tyvm
leowest
leowest4mo ago
if you have control over the format it would be much easier if u just use a simple class
Mekasu0124
Mekasu01244mo ago
well I use a style model
public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
but since you gave that code example, and I started implementing it, the model is not longer correct I need to make it be a dictionary of those items so that it looks like
{
"default": {...},
"mek": {...}
}
{
"default": {...},
"mek": {...}
}
leowest
leowest4mo ago
well your example was from a dictionary
Mekasu0124
Mekasu01244mo ago
so that after the user logs in, it can use their username to pull their desired styles from the styles file
leowest
leowest4mo ago
without a dictionary I would use say
Mekasu0124
Mekasu01244mo ago
I want a dictionary I prefer that honestly
leowest
leowest4mo ago
calm down
Mekasu0124
Mekasu01244mo ago
calm what down? I'm not upset?
MODiX
MODiX4mo ago
leowest
REPL Result: Success
var data = new UserSetting
{
StyleModels = new List<StyleModel>
{
new StyleModel
{
Username = "Mekasu0124",
BackgroundColor = "#000",
BorderColor = "#FFF",
FontColor = "#FFF"
}
}
};
var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserSetting
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public List<StyleModel> StyleModels {get;set;}
}

public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
var data = new UserSetting
{
StyleModels = new List<StyleModel>
{
new StyleModel
{
Username = "Mekasu0124",
BackgroundColor = "#000",
BorderColor = "#FFF",
FontColor = "#FFF"
}
}
};
var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserSetting
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public List<StyleModel> StyleModels {get;set;}
}

public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
Console Output
{"default":[{"Username":"Mekasu0124","BackgroundColor":"#000","BorderColor":"#FFF","FontColor":"#FFF"}]}
{"default":[{"Username":"Mekasu0124","BackgroundColor":"#000","BorderColor":"#FFF","FontColor":"#FFF"}]}
Compile: 547.783ms | Execution: 51.423ms | React with ❌ to remove this embed.
leowest
leowest4mo ago
only difference is that it makes default a list with the [] but imo it looks better if u dont make it a list then would be the same as the dictionary
MODiX
MODiX4mo ago
leowest
REPL Result: Success
var data = new UserSetting
{
StyleModel = new StyleModel
{
Username = "Mekasu0124",
BackgroundColor = "#000",
BorderColor = "#FFF",
FontColor = "#FFF"
}
};
var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserSetting
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public StyleModel StyleModel {get;set;}
}

public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
var data = new UserSetting
{
StyleModel = new StyleModel
{
Username = "Mekasu0124",
BackgroundColor = "#000",
BorderColor = "#FFF",
FontColor = "#FFF"
}
};
var json = System.Text.Json.JsonSerializer.Serialize(data);
Console.WriteLine(json);

public class UserSetting
{
[System.Text.Json.Serialization.JsonPropertyName("default")]
public StyleModel StyleModel {get;set;}
}

public class StyleModel
{
public string Username { get; set; }
public string BackgroundColor { get; set; }
public string BorderColor { get; set; }
public string FontColor { get; set; }
}
Console Output
{"default":{"Username":"Mekasu0124","BackgroundColor":"#000","BorderColor":"#FFF","FontColor":"#FFF"}}
{"default":{"Username":"Mekasu0124","BackgroundColor":"#000","BorderColor":"#FFF","FontColor":"#FFF"}}
Compile: 508.001ms | Execution: 45.180ms | React with ❌ to remove this embed.
leowest
leowest4mo ago
I just prefer representing it as classes since it makes more sense over the dictionary but feel free to choose your poison :catlaugh: just wanted to show u it can be the same
Mekasu0124
Mekasu01244mo ago
lol I appreciated it. so I decided to go with your list example. here's where I'm at
public List<StyleModel> CheckForStyleFile(string username)
{
username = username ? username : "default";

var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

try
{
if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
return data.StyleModel;
}
else
{
var jsonObject = File.ReadAllText(_stylesFile);
var jsonData = JsonSerializer.Deserialize<List<StyleModel>>(jsonObject);

foreach (StyleModel style in jsonData)
{
if (style.Username == username)
{
return jsonData[style.Username];
}
}
}
}
}
public List<StyleModel> CheckForStyleFile(string username)
{
username = username ? username : "default";

var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

try
{
if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
return data.StyleModel;
}
else
{
var jsonObject = File.ReadAllText(_stylesFile);
var jsonData = JsonSerializer.Deserialize<List<StyleModel>>(jsonObject);

foreach (StyleModel style in jsonData)
{
if (style.Username == username)
{
return jsonData[style.Username];
}
}
}
}
}
leowest
leowest4mo ago
mmm u have a wild try lost in there but aside from that just do Deserialize<UserSetting> you can't skip it as its your root
public StyleModel CheckForStyleFile(string username = "default")
{
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
}
else
{
var json = File.ReadAllText(_stylesFile);
data = JsonSerializer.Deserialize<UserSetting>(json);
foreach (var style in data.StyleModel)
{
if (style.Username.Equals(username, StringComparison.InvariantCultureIgnoreCase))
{
return style;
}
}
}
// return here instead of inside the if in case no username was found on the foreach
return data.StyleModel;
}
public StyleModel CheckForStyleFile(string username = "default")
{
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
}
else
{
var json = File.ReadAllText(_stylesFile);
data = JsonSerializer.Deserialize<UserSetting>(json);
foreach (var style in data.StyleModel)
{
if (style.Username.Equals(username, StringComparison.InvariantCultureIgnoreCase))
{
return style;
}
}
}
// return here instead of inside the if in case no username was found on the foreach
return data.StyleModel;
}
u prob want to use Equals for that username comparison too
Mekasu0124
Mekasu01244mo ago
inside the foreach's if statement condition, I get the error Cannot implicitly convert 'Diary.Models.StyleModel' to 'System.Collections.Generic.List<Diary.Models.StyleModel>' on return style; it's because the function wants a list to be returned
foreach (var style in data.StyleModel)
{
if (style.Username.Equals(username, StringComparison.InvariantCultureIgnoreCase))
{
return new List<StyleModel>()
{
new StyleModel
{
Username = username,
BackgroundColor = style.BackgroundColor,
BorderColor = style.BorderColor,
FontColor = style.FontColor,
}
};
}
}
foreach (var style in data.StyleModel)
{
if (style.Username.Equals(username, StringComparison.InvariantCultureIgnoreCase))
{
return new List<StyleModel>()
{
new StyleModel
{
Username = username,
BackgroundColor = style.BackgroundColor,
BorderColor = style.BorderColor,
FontColor = style.FontColor,
}
};
}
}
I went around it by doing this. Thoughts?
leowest
leowest4mo ago
ah yeah if u have multiple entries for the same username that would make sense I thou u have only 1 entry per username
Mekasu0124
Mekasu01244mo ago
later on down the road, I'm going to give the user's the option of using local storage to store their entires on their personal computer, or use online storage. something like one drive or whatever. If they choose the online storage option, then it's just going to be one json file that houses all of the user's style information that they chose and saved. The only reason I'm doing that like that is because if the user changes devices or something, then not only will their entries be backed up, but their style selections will be as well
ah yea if you have multiple entries for the same username that would make sense
I want to clarify thought that only 1 user gets 1 color scheme to create and save. If they make another one, it overwrites the existing, so if I have something wrong in my code, then please tell me
leowest
leowest4mo ago
well then u shouldn't be returning List<StyleModel> just StyleModel and then the previous code should work as intended and since u wont be saving UserSettings to that file U might want to change it to perhaps UserStyle or something as in the class name to make more sense I assume the file can contain multiple users because of the login? as in different users can login and have their own custom style which is fine
Mekasu0124
Mekasu01244mo ago
I think I'm actually going to return the user like it is. I like a challenge and so I'm going to create a settings screen to where the user can edit their font size, font color, and everything else that way and it'll all save under a UserSettings model which will be returned when they log in again. that's a great idea. ty 🙂
leowest
leowest4mo ago
what I was going to say is that perhaps it would be better to save per file rather than per user but then u would need a identifier for the user that is safe to write to disk for example i.e.: var filename = Path.Combine(_userSettingsFolder, $"{userId}_style.json"); it would make it easier to upload, download over a file with all users so u only download/access the needed users when required but that is beyond the scope here now
Mekasu0124
Mekasu01244mo ago
I think I'm just going to start it off with local host storage. I can work my way up from there
public List<StyleModel> CheckForStyleFile(string username = "default")
{
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
}
else
{
var jsonObject = File.ReadAllText(_stylesFile);
data = JsonSerializer.Deserialize<UserSetting>(jsonObject);
}

return data.StyleModel;
}
public List<StyleModel> CheckForStyleFile(string username = "default")
{
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
BackgroundColor = "#000000",
BorderColor = "#24de45",
FontColor = "#CDCDCD"
}
}
};

if (!File.Exists(_stylesFile))
{
var json = JsonSerializer.Serialize(data);
File.AppendAllText(_stylesFile, json);
}
else
{
var jsonObject = File.ReadAllText(_stylesFile);
data = JsonSerializer.Deserialize<UserSetting>(jsonObject);
}

return data.StyleModel;
}
So I think this will suffice for now so with this current code block, I believe I have a change that I need help implementing. Since I'm going to allow other custom user settings, I now have a desired result for my json file. For example:
{
"styles": {
"Username": {
"BackgroundColor": "",
"BorderColor": "",
"FontColor": "",
"FontSize": ""
}
},
"settings": {
"Username": {
"autocorrect": false
}
}
}
{
"styles": {
"Username": {
"BackgroundColor": "",
"BorderColor": "",
"FontColor": "",
"FontSize": ""
}
},
"settings": {
"Username": {
"autocorrect": false
}
}
}
so the various parts of the application that have custom user settings will be categorized oh wait. nvm. by using the code I have now, I just have to add the extra stuff categorized how I want like
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
FontName = "Times New Roman",
FontStyle = "regular",
FontSize = "12",
FontColor = "#CDCDCD",
FontUnderline = "none",
TextAlign = "left",
BackgroundColor = "#000000",
BorderColor = "#24de45",
}
},
SettingsModel = new List<SettingsModel>
{
new SettingsModel
{
Username = username,
AutoCorrect = false
}
}
};
var data = new UserSetting
{
StyleModel = new List<StyleModel>
{
new StyleModel
{
Username = username,
FontName = "Times New Roman",
FontStyle = "regular",
FontSize = "12",
FontColor = "#CDCDCD",
FontUnderline = "none",
TextAlign = "left",
BackgroundColor = "#000000",
BorderColor = "#24de45",
}
},
SettingsModel = new List<SettingsModel>
{
new SettingsModel
{
Username = username,
AutoCorrect = false
}
}
};
leowest
leowest4mo ago
unless u plan on have multiple users listed there I dont really suggest that layout on styles aside from that you're on the right track basically I would not be very attached to the resulting json as much as u should be attached to a good class relationship because the resulting json will be readable and structured if that is one concern if u get a good feeling about serializing and deserializing this data and easily accessing and reusing it, u should go for it
Mekasu0124
Mekasu01244mo ago
I do for now. I could also use the database too and create a config table.
leowest
leowest4mo ago
you certainly could use SQLite and use it instead of json and using EntityFramework that would be the same talk u dont even need to worry about the db as EF would take care of all of it for u including creating tables, relationships and what not ofc its good to have understanding of database, SQL syntax and whatnot or you could write an API and just have the data come from it from your own server possibilities are endless
Mekasu0124
Mekasu01244mo ago
I thought about doing a FastAPI with user authentication, but that requires hosting and bunch of other stuff. To just start the application off, it's all just going to be saved between a json file and a sqlite database since the json file wont be of a big size whatsoever and the database is more efficient for holding large entries of text. I appreciate your help. I'll be back if I have anymore questions ❤️