Json string to a class object but not {get; set; } !

So I have a class that I did and I did some constructors to avoid the {get; set; } And I have a Json string that I want to use to fill my class, can I like put the Json to a dictionnary and then fill my variables with it ?
56 Replies
Angius
Angius2w ago
JsonSerializer serializes and deserializes properties It can serialize fields with a proper setting, sure But I can't see why would you want to avoid properties in the first place
Alexis 🫐
Alexis 🫐OP2w ago
for sure I tried multiple things with JsonSerializer but it seems to be null at every of my attemps
Angius
Angius2w ago
Show the class, show the json By default, for example, the naming must match exactly
Alexis 🫐
Alexis 🫐OP2w ago
No description
Angius
Angius2w ago
So a json property "foo" will not be serialized into a class property Foo
Alexis 🫐
Alexis 🫐OP2w ago
then there if the json
No description
Angius
Angius2w ago
Right, names seem to match So just turn those fields into properties And it will work
Alexis 🫐
Alexis 🫐OP2w ago
they match for sure
Jimmacle
Jimmacle2w ago
i don't suggest using public fields
Angius
Angius2w ago
Rule of thumb is, private fields — public properties
Alexis 🫐
Alexis 🫐OP2w ago
hm private fields instead ?
Angius
Angius2w ago
No, public properties
Jimmacle
Jimmacle2w ago
i mean, yes but then how do you access the data outside the class
Angius
Angius2w ago
Unless you don't want those values accessible from the outside of the class
Alexis 🫐
Alexis 🫐OP2w ago
hmm I guess I can just do like a "public string GetDesc" by exemple
Jimmacle
Jimmacle2w ago
no C# has properties for this purpose that's what the { get; set; } does
Alexis 🫐
Alexis 🫐OP2w ago
yeah ik ok right so if I do like private string Description {get; set;} it'll be accessible ?
Angius
Angius2w ago
What do you suppose private means?
Alexis 🫐
Alexis 🫐OP2w ago
uh im kinda dumb forget that
Jimmacle
Jimmacle2w ago
also, unrelated but i don't like the HTTP request in a constructor constructors should be very simple and usually just assign arguments to class members your code is also not properly async
Angius
Angius2w ago
Definitely shouldn't be doing any i/o
Jimmacle
Jimmacle2w ago
the constructor will return before the http request has completed and you may access the object while it's still in an "invalid" state as far as your logic is concerned
Alexis 🫐
Alexis 🫐OP2w ago
hmmm
Jimmacle
Jimmacle2w ago
the nice way to do this would be to have a static factory method that does the request then constructs the object using the response then you can properly await it in the calling code
Angius
Angius2w ago
Or a separate class, and keep Alliance just a plain ol' model
Jimmacle
Jimmacle2w ago
yeah, depending on how many API endpoints you use it might make more sense to create a service class around the API
Alexis 🫐
Alexis 🫐OP2w ago
did in an old version of that code hm It works but I really tought I could upgrade the code doing this
Alexis 🫐
Alexis 🫐OP2w ago
it was like this
No description
Alexis 🫐
Alexis 🫐OP2w ago
work well
Jimmacle
Jimmacle2w ago
i think that makes a lot more sense than what you have now, personally
Angius
Angius2w ago
Aside from the Newtonsoft usage, yeah, that looks good
Alexis 🫐
Alexis 🫐OP2w ago
but need to remove Task<User> and just put public User
Angius
Angius2w ago
Why?
Jimmacle
Jimmacle2w ago
if it's async you need the Task<> part
Angius
Angius2w ago
$rulesofasync
MODiX
MODiX2w ago
TLDR on async/await: * every .net API that is suffixed with Async (eg: .Read() and .ReadAsync()) => use the Async version * if the API name ends with Async => await it * if the API returns a Task => await it * if you have to await in a method: * that method must have the async keyword (you could even suffix your function name with Async, up to you) * if it was returning T (eg:string) => it should now return Task<T> (eg: Task<string>) * APIs to ban and associated fix:
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
var r = t.Result; /* ===> */ var r = await t;
t.Wait(); /* ===> */ await t;
var r = t.GetAwaiter().GetResult(); /* ===> */ var r = await t;
Task.WaitAll(t1, t2); /* ===> */ await Task.WhenAll(t1, t2);
Alexis 🫐
Alexis 🫐OP2w ago
oh yeah right Http needs to be async yeah right was wrong hm I guess that should be better you're right
Angius
Angius2w ago
Just ditch the Newtonsoft
Jimmacle
Jimmacle2w ago
yeah there is a built in json library now (System.Text.Json)
Angius
Angius2w ago
change JsonConvert.DeserializeObject<T>(str) into JsonSerializer.Deserialize<T>(str) and you're set
Alexis 🫐
Alexis 🫐OP2w ago
right ?
No description
Angius
Angius2w ago
LGTM Though I'd probably just return a null if the alliance hasn't been found
Alexis 🫐
Alexis 🫐OP2w ago
hm like just null
Angius
Angius2w ago
And I'd see if the API maybe returns 404 properly when the alliance hasn't been found If so, I'd check for that, not for a magic string ye
Alexis 🫐
Alexis 🫐OP2w ago
never returns 404
Angius
Angius2w ago
Ooof
Alexis 🫐
Alexis 🫐OP2w ago
just returns this
No description
Jimmacle
Jimmacle2w ago
i love non-standard apis
Alexis 🫐
Alexis 🫐OP2w ago
that's a silly coded API it returns messages but no error code
leowest
leowest2w ago
but does it give u a 404 response status or something? ah u said above it does not yeah that is a weird ass api
Alexis 🫐
Alexis 🫐OP2w ago
'P' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0. hmmmmm alliance = JsonSerializer.Deserialize<Alliance>(response); this line is the problem wtf i don't even understand this silly error I guess I did something wrong bruh
leowest
leowest2w ago
can you $paste the json to the site below?
MODiX
MODiX2w ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Jimmacle
Jimmacle2w ago
it's saying the JSON is invalid because the first character in it is a P so check what's actually in response
Alexis 🫐
Alexis 🫐OP2w ago
was actually my fault was doing dumb thing providing "" as string for the query everything good now thx
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server