Parse embed from Json into EmbedBuilderUtils
Hello! :D
For my own convenience and to not clog up my entire class file with endless amounts of lines. I created a json file that holds numerous of preset embeds that I wanna use. However, I'm stuck on an issue and I certainly cannot seem to figure out the issue whatsoever.
i simply want to parse the embed into a utility that is named EmbedBuilderUtils which is provided by Discord.NET. It basically goes like this
This should technically speaking work? unless there is another way to parse first_embed's object entirely. I'm quite lost on this. Any help is appreciated!
19 Replies
To give an insight on the error,
there could be two possibilities, something inside the embed is missing a field OR the parsing is not done correctly and it isn't retrieving the entire object which I assume is GetValue.
first_embed
value is not a string
It's an objectCorrect, in my previous code, one that actually worked, I did something similar by reading the json and then covnerting it to an object like
ToObject<string>()
Why not use a
Dictionary<string, Embed>
for the embeds
type?
Then you can re-serialize the selected embed
Or just send it straight with PostAsJsoNAsync()
I had actually tried that but presumably not correctly
wouldn't it be
You could bind the
embeds
section of the configuration to Dictionary<string, Embed>
ah, so
var embeds = JsonSerializer.Deserialize<Dictionary<string, Embed>>(Configuration.GetSection("embeds");
I see,
Something like that
so ultimately, this is what I shoud have
im not sure if
Get
can be directly used as a dictionary
instead of using bind?If it works, then it works
I'd get rid of Newtonsoft
And I'd use the return of your
TryParse
method
But besides that LGTMthe TryParse expects a string (json)
So?
Use System.Text.Json is what I mean
assuming that I deserialize everything and pass it through as is
Get rid of Newtonsoft.Json
ah
ah, i have never worked with System.Text.Json as much
but I am sure it isn't much of a difference
so switching over shouldn't be too bothersome
You seem to have used STJ here just fine
yeah.. i have been constantly mixing them up since I had both depedencies in the class
but i ultimately got rid of newtonsoft because text.json had asynchronous functions I feel like would be better at not blocking a handler
it seems that the first_embed entry is not found at all despite existing
resolved the issue, however, the issue that I absolutely hate is