✅ Writing a dictionary to json file
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
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
leowest
REPL Result: Success
Console Output
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 524.933ms | Execution: 85.996ms | React with ❌ to remove this embed.
tyvm
if you have control over the format
it would be much easier if u just use a simple class
well I use a style model
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
well your example was from a dictionary
so that after the user logs in, it can use their username to pull their desired styles from the styles file
without a dictionary I would use say
I want a dictionary
I prefer that honestly
calm down
calm what down? I'm not upset?
leowest
REPL Result: Success
Console Output
Compile: 547.783ms | Execution: 51.423ms | React with ❌ to remove this embed.
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
leowest
REPL Result: Success
Console Output
Compile: 508.001ms | Execution: 45.180ms | React with ❌ to remove this embed.
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
lol I appreciated it. so I decided to go with your list example. here's where I'm at
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
u prob want to use Equals for that username comparison tooinside 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
I went around it by doing this. Thoughts?ah yeah if u have multiple entries for the same username that would make sense
I thou u have only 1 entry per username
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 senseI 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
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
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 🙂
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
I think I'm just going to start it off with local host storage. I can work my way up from there
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:
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
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
I do for now. I could also use the database too and create a config table.
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
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 ❤️