127 Replies
more context please
i'm deserializing to a custom record type and i need to get a random entry
i think i did this right, im new to C#
but the http reponse returns an array
of safebooru image data(s)
ah
you need to deserialize to List<SafeBooruResponse>
then you can just do something like List.Count
then can i change the record to
a normal class
with properties?
and Random
show me the json
um it looks sum like this
so you have more properties?
directory, image, x??
yea but i dont want to account for those
do i have to?
no
okay
but
the json has more of these?
yep
it has multiple objects with more than 2 props
same record as you have
deserialize to List<T>
ees good
seems
i had another question about some code
since im new to this deserializer
i used newtonsoft but its not type safe
don't use newtonsoft
yea i learned that
earlier
what version of .net are u targeting?
how do i check my version?
.csproj
visual studio?
7
vscode
good
i dont like vs even if its
handicapping me
it's okay
what do you want to know
okay so
i have this
heres my
response class
so the returned json is
{"url": "link"}
but for some reason
Url
is returning as null
?
even if propertynamecaseinsensitive is on??try
using an extension method
elaborate?
also
as you see the api says url
not Url
yea but im turning off case insensitivity
good
yes
you can do that
or explicitly say so
in the class
is that a built in attribute from system?
no, it belongs to
System.Text.Json.Serialization
why are u using required?its always in the json
its the onlhy thing returned
only
itll never be null lol
that's not the definition of required
so is it only for
initialization?
i meant
itll always be provided
and should be
yes
but you don't need it
oh
for that purpose
will serialize all the properties it finds in the T
if they are not there they are skipped
same thing when deserializing
ah
another question
you can also use [JsonIgnore]
why is it necessary to provide the JsonPropertyName?
because the json is url
not Url
so whats the point of
insensitive casing
because you are dealing with 1 property json
but if you have a big big json
it's slower much slower
when you can just say what properties are different or not
so
hold on
what about this?
lol
how many differ
from the json
every single one of those
are capitalized while
json isnt
I would still use JsonPropertyName
still faster
wouldnt it be slow..
nope
whew..
let me do a fast test
okay so i did try thisss and it did not work š
okay
yea so its deserializing to this
{}
var response = JsonSerializer.Deserialize<List<NekosLifeResponse>>(raw);
why
its not a list
oh
my bad
np
why didn't u use what i told you
ReadFromJsonAsync<T>();
i didnt know what u meant
i cant find that method anywhere
_httpClient.ReadFromJsonAsync<T>();
that property doesn't exist on
HttpClient
it's an extension method
is this a console app?
it should be by default nonetheless
what is elfin?
var got = await elfin.HttpClient.GetAsync("https://nekos.life/api/v2/img/neko");
here
my custom client class
nooo
why
?
it includes a raw client lol
that's really bad
but it has more utilities since i cant extend the normal discord client class
well im fine with it
okay
it does what i need
but yea this is my response class
but the deserialized object is empty
set a breakpoint at var raw
and see what you get
well i get the raw json
when i print it it comes out normally
it*
however for some reason the converter isnt understanding the type
raw has the string correctly?
yea
its the string json response
from the request
if you type
elfin.HttpClient.GetFromJsonAsync<NekosLifeResponse>(url);
that doesn't work?
nope
no method found
can you please do
var _httpclient = new HttpClient();
and not use elfin
sure
whats the point though?
i want to see something
just do
var _httpclient = new HttpClient();
_httpClient.GetFromJsonAsync<NekosLifeResponse>(url);
<NekosLifeResponse>
oops
add this
using System.Net.Http.Json;
its present now
should work
try it
let me try
came out as an empty object wth
š
doesn't work for me
can you give me 5 minutes
gotta take the dog out
and we can fix it
ofc
ah lol
i know what it is
its a field
without a setter
try this
OH
LMAO
NO WONDER š
late here
not thinking straight xd
try it out i'll be back in 5
its 10pm for me
alright ty
works!
how can i d o this with a post request?
i need to do something like this
but the
payload
parameter is saying it needs to be of the same type im using for the request method
should i continue with my other method?
PostAsJsonAsync
is just
List<AnyListResponse> list = new();
add whatever to the list
then
elfin.HttpClient.PostAsJsonAsync<List<AniListResponse>>("https://graphql.anilist.co/", list);
no StrinContent needed
if you dont want to use that
just do what you did
and use SendAsync (see below)
or better yet PostAsync
well i cant use this then
ill just use my og method
what og method
it seems theres a prob tho š¤
but the response isnt deserializing properly
heres what
raw
looks like
and your class
AniListResponse
that looks fine
are u getting any errors?
none are being thrown nope
hold on
check response
it should deserialize correctly
nothing prints so
theres a break
after the deserialization
a breakpoint?
no
it stops worjing
an exception?
possibly but
its not killing my code and
it isnt being printed
so hold on
forgot
ids are integers for anilist
lol
but i had a question abt errors too
why arent they killing my program??
theyre just silent
is that inside a try catch?
just now it was
but only bc i needed
to see the error
then that's why
if you catch the exception, execution goes on
visual studio code is horrible at this sort of things
okay but i never catch it
i have no idea how it handles exceptions
it's a glorified text editor
im using
command prompt
i think its the way my cmd is handled
basically it goes like this
MessageEvent
=>
Command Handler Method =>
Run
i think i got an ide
idea
welp didnt work
i tried catchi ng it in the method's invoking
but its whatevercatch it in the request handling
not the method invoking
that would require a try catch for every method
that seems repetitive doesnt uit
then make it non repetitive
well clearly the errors cant be caught outside of the method
from what i'm seeing
i think you are on a good path now
try on your own
learn by doing!!
(and failing)
lol
ty
i got it from here lik u said
have a good night
if u have more questions ask
for sure
kk ofc
but try to practice
yap
šāāļø
good night
night